学习网站: https://houdunren.gitee.io/note/
修改表
修改表名
alter table stu rename stus;
别一种操作方式
rename table stus to stu;
修改表字符集
alter table class3 charset gbk;
删除表所有数据
truncate class3;
删除数据表
drop table if exists class2;
字段管理
修改字段类型
alter table stu modify sname char(30) not null;
修改字段时同时更改字段名
alter table stu change sname name varchar(30) not null;
添加字段
alter table stu add sex smallint default null;
在学生名称后添加邮箱字段
alter table stu add email varchar(50) after sname;
将字段添加到最前面
alter table stu add qq varchar(30) first;
删除学生邮箱字段
alter table stu drop email;
主键操作
一般主键为自增字段,需要删除自增属性后才可以删除主键
alter table stu modify id int not null;
删除主键
alter table stu drop primary key;
添加表主键
alter table stu add primary key(id);
修改为自增列
alter table stu modify id int not null auto_increment;
主键与自增列一起添加
alter table stu modify id int not null auto_increment ,add primary key(id);