您的位置:首页 > 博客中心 > 数据库 >

SQL 语句大汇总

时间:2022-03-14 04:01

按照顺序来

//创建数据库

create database wenda charset utf8;

//删除数据库

drop database wenda;

//创建表

create table hd_ask(asid int unsigned primary key auto_increment,answer int not null default 0);

unsigned 非负 , primary key 主键 , auto_increment自增 , not null 非空 , default 默认值

//删除表

drop table hd_ask;

//追加字段

alter table 表名 add 字段 [first | after 字段]

//删除字段

alter table 表名 drop 字段名

//修改表名

alter table 表名 rename 新表名

例:alter table hd rename houdunwang;将表hd 更名为houdunwang

或 rename hd to houdunwang

//修改字段同时更名

alter table 表名 change 旧字段 新字段 [ first | after 字段]

例: alter table cb change id cid int(10) ;修改字段ID,同时更改字段名

//修改字段

alter table 表名 modify 字段[first | after 字段]

例: alter table cb modify id int(8) after name;

//查询数据的方法

select * from hd_ask;
select asid,answer from hd_ask;
select asid,answer from hd_ask where asid=1;
select asid,answer from hd_ask where asid>1 limit 0,10;

//填加主键

例: alter table cb add primary key (id);添加主键id

注: 一个表只能有一个主键,所以如果原来存在主键要删除,如何删除看下

//删除主键

例: alter table hd drop primary key;删除主键

热门排行

今日推荐

热门手游