3、数据表管理

1、表的维护

//1 修改表名称
alter table old_name rename new_name; // 方法1
rename table old_name to new_name; // 方法2

//2 修改表的字符集
alter table table_name utf8;

//3 修改存储引擎
alter table table_name engine innodb/myisam;

//4 删除数据表
delect from table_name;//一条一条删 可以回滚
truncate table_name; //清空表 不能回滚

//5 删除表
drop table if exists table_name;

 

2、字段的维护

//1 修改字段
alter table student_bak modify name char(60);//修改数据类型 modify本意:调整 稍作修改
alter table student_bak change name ename varchar(100);//修改字段名和类型 change 本意:改变 变化

//2 添加字段
alter table student_bak add sex int default null;// 正常添加
alter table student_bak add height int default null after id; //指定位置添加
alter table student_bak add qq varchar(20) default null first;//添加到最前面

//3 删除字段
alter table student_bak drop qq;

 

3、主键的维护

复制的表是没有主键的

//1 添加表主键
alter table student_bak add primary key(id);

//2 添加自增列
alter table student_bak modify id int not null auto_increment;

//3 添加主键和自增
alter table demo modify id int primary key auto_increment;//语法1
alter table demo modify id int not null auto_increment,add primary key(id);//语法2

//4 删除主键
alter table demo modify id int not null; //1 先删除自增
alter table demo drop primary key; //2 再删除主键

 

posted @ 2020-12-02 10:48  棉花糖88  阅读(73)  评论(0)    收藏  举报