表字段的操作
1、语法 :alter table 表名 执行动作;
2、添加字段(add)
alter table 表名 add 字段名 数据类型;
alter table 表名 add 字段名 数据类型 first;
alter table 表名 add 字段名 数据类型 after 字段名;
3、删除字段(drop)
alter table 表名 drop 字段名;
4、修改数据类型(modify)
alter table 表名 modify 字段名 新数据类型;
5、表重命名(rename)
alter table 表名 rename 新表名;
6、练习
1、创建库 studb2
2、在库中创建表 t1 ,字段有3个:name、age、phnumber
use studb2;
create table t1(
name char(20),
age tinyint unsigned,
phnumber char(11)
);
3、查看表结构
desc t1;
4、在表中第一列添加一个 id 字段
alter table t1 add id int first;
5、把 phnumber 的数据类型改为 bigint
alter table t1 modify phnumber bigint;
6、在表中最后一列添加一个字段 address
alter table t1 add address varchar(50);
7、删除表中的 age 字段
alter table t1 drop age;
8、查看表结构
desc t1;
发表评论