MySQL高效编程学习笔记(五)--表的维护和改造
- 修改表的列结构
若表中有数据最好先备份,注意转换前后的字符长度、以及是否可以互相转换等问题。
- 改变列数据类型
ALTER TABLE visitor MODIFY nam VARCHAR(30)
Eg: alter table goods modify name varchar(20);//出现字符类型的错误
将参数latin1都修改为utf8
或者直接alter table convert to charset gbk或utf8;
- 追加/删除 列
alter table goods add price INT;
alter table goods drop price;
- 改变列的位置
alter table student modify sex int after name;
- 改变列名与类型
Eg:alter table student change phonenumber phone varchar(20);
- 复制表和删除表
- 表的列构造+数据复制
Eg: create table studentcopy select * from student;
-
复制表的列构造
create table studentcopybody like student; -
数据的复制(从其他表复制部分列的数据)
Eg: insert into studentcopybody(ID,goodsID) select ID,classname from classname;
- 表的删除
Eg: drop table studentcopy,studentcopy2;