1:查看表:
show tables;
2:创建表:
create table [if not exists] 表名(
列名1 类型 [限制],
列名2 类型 [限制],
...
列名n 类型 [限制] #最后⼀列没有逗号
)** [engine=myisam | innodb][ default charset=utf8];
primary key 主键 不允许有重复值,不允许为空
auto_increment 自增长,只对int型主键起作用
#复合主键
mysql> create table grade(
sid int ,
cid int,
score float,
primary key(sid,cid));
#字段限制
primary key 不允许空值,唯⼀
not null ⾮空
unique 唯⼀
default 缺省,默认值
例如
课程表:
mysql> create table course
-> (
-> cno varchar(5) comment '课程号',
-> cname varchar(5) not null comment '课程名',
-> tno varchar(10) not null comment '教师编号',
-> primary key (cno)
-> );
**3.**删除表
drop table 表名;
**4.**复制表结构
create table 表名 like 其他表名
**5.**查看表结构
desc 表名;
**6.**查看建表语句
show create table 表名;
**7.**修改表
#修改字段类型
alter table 表名 modify 字段名 类型 [限制] #增加字段
alter table 表名 add [column] 字段名 类型 [限制];
#删除字段
alter table 表名 drop [column] 字段名;
修改字段名和类型
alter table 表名 change [column] 旧字段名 新字段名 类型 [限制];
#修改表名
alter table 表名 rename 新表名
alter table 表名 [engine=myisam] [default charset=utf8];
# 可以通过first、after指定插⼊位置
alter table student add sno varchar(3) not null after sid; //在sid列后插⼊
alter table student add sid int primary key auto_increment first;//在第⼀列插⼊