学习网站: 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);
本文详细介绍了SQL中对数据库表的各种操作,包括如何修改表名、改变字符集、清空数据、删除表以及字段的管理。涉及的命令包括ALTER TABLE用于修改表结构,TRUNCATE用于删除所有数据,DROP TABLE用于删除表,以及ALTER TABLE对字段类型、名称的修改和添加、删除字段等。此外,还讲解了主键的管理,包括删除和添加主键以及设置字段为自增。

3967

被折叠的 条评论
为什么被折叠?



