1. alter 命令:
-
1. 1 添加表字段 (add)
// 添加表字段 (i) 为字段名 int为字段数据类型mysql> alter table test2 add i int;mysql> alter table test2 add name varchar(20);mysql> alter table test2 add age int;
1.2 删除表字段 (drop)
alter table test2 drop age;alter table test2 drop i;

如果需要指定新增字段的位置:可以使用关键字 First / 或者 after R , 在 字段 R 后alter table test2 add i int after id;

如果不指定,则默认从最后开始alter table test2 add c varchar(20);
2. 修改字段
使用 modify 或者 change
// 使用 modify 只能修改对应字段的数据类型alter table test2 modify c int;

使用 change 可修改 字段名以及字段的数据类型
alter table test2 change c(原字段名) age(新字段名与数据类型) int;
3. 为字段添加默认值 / 指定是否为 Null
这里存在一个问题,就是我们为前面的数据添加 字段的时候,并没有为字段添加值,所以存在问题就是,加入我们要设计 not null ,表里面的 字段都为 null ,这样数据和约束存在冲突,我们需要在其中加入数据

看到我们需要修改的 i 列 存在 null 值,所以无法设置为 not null 约束
// 先将 i 列 的值全部设置为非空update test2 set i = 12;

// 然后将 i 设置为 not null 约束, 并且设置默认值alter table test2 i bigint not null default 20;
4. 修改默认字段的默认值
1.1修改
alter table test2 alter i default 100;
1.2 删除默认值
alter table test2 alter i drop default;
5. 查看数据库状态:
show table status \G;
6. 修改表名
// 更改表名alter table test2 rename to test;

