Mysql 列修改语句alter/changer/modify

column在语句中可以省略
alter column:
设置或删除列的默认值。该操作会直接修改.frm文件而不涉及表数据。所以,这个操作非常快
mySQL> desc example6;
+--------+-------------+------+-----+---------+----------------+
| Field  | Type        | Null | Key | Default | Extra          |
+--------+-------------+------+-----+---------+----------------+
| id     | int(11)     | NO   | PRI | NULL    | auto_increment |
| stu_id | int(11)     | YES  | UNI | NULL    |                |
| name   | varchar(20) | YES  |     | NULL    |                |
+--------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> alter table example6 alter column name set default 'Gary';
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc example6;
+--------+-------------+------+-----+---------+----------------+
| Field  | Type        | Null | Key | Default | Extra          |
+--------+-------------+------+-----+---------+----------------+
| id     | int(11)     | NO   | PRI | NULL    | auto_increment |
| stu_id | int(11)     | YES  | UNI | NULL    |                |
| name   | varchar(20) | YES  |     | Gary    |                |
+--------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> alter table example6 alter column name drop default;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc example6;
+--------+-------------+------+-----+---------+----------------+
| Field  | Type        | Null | Key | Default | Extra          |
+--------+-------------+------+-----+---------+----------------+
| id     | int(11)     | NO   | PRI | NULL    | auto_increment |
| stu_id | int(11)     | YES  | UNI | NULL    |                |
| name   | varchar(20) | YES  |     | NULL    |                |
+--------+-------------+------+-----+---------+----------------+
change column:
列的重命名、列类型的变更以及列位置的移动
mysql> desc example6;
+--------+-------------+------+-----+---------+----------------+
| Field  | Type        | Null | Key | Default | Extra          |
+--------+-------------+------+-----+---------+----------------+
| id     | int(11)     | NO   | PRI | NULL    | auto_increment |
| stu_id | int(11)     | YES  | UNI | NULL    |                |
| name   | varchar(20) | YES  |     | NULL    |                |
+--------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> alter table example6 change column name NAME varchar(20) not null first;
Query OK, 4 rows affected (0.07 sec)
Records: 4  Duplicates: 0  Warnings: 0
mysql> desc example6;
+--------+-------------+------+-----+---------+----------------+
| Field  | Type        | Null | Key | Default | Extra          |
+--------+-------------+------+-----+---------+----------------+
| NAME   | varchar(20) | NO   |     | NULL    |                |
| id     | int(11)     | NO   | PRI | NULL    | auto_increment |
| stu_id | int(11)     | YES  | UNI | NULL    |                |
+--------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> alter table example6 change column NAME name varchar(20) not null default 'Gary' after stu_id;
Query OK, 4 rows affected (0.09 sec)
Records: 4  Duplicates: 0  Warnings: 0
mysql> desc example6;
+--------+-------------+------+-----+---------+----------------+
| Field  | Type        | Null | Key | Default | Extra          |
+--------+-------------+------+-----+---------+----------------+
| id     | int(11)     | NO   | PRI | NULL    | auto_increment |
| stu_id | int(11)     | YES  | UNI | NULL    |                |
| name   | varchar(20) | NO   |     | Gary    |                |
+--------+-------------+------+-----+---------+----------------+
modify column
除了列的重命名之外,他干的活和CHANGE COLUMN是一样的
mysql> desc example6;
+--------+-------------+------+-----+---------+----------------+
| Field  | Type        | Null | Key | Default | Extra          |
+--------+-------------+------+-----+---------+----------------+
| id     | int(11)     | NO   | PRI | NULL    | auto_increment |
| stu_id | int(11)     | YES  | UNI | NULL    |                |
| name   | varchar(20) | NO   |     | Gary    |                |
+--------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> alter table example6 modify column name varchar(20) not null after stu_id;
Query OK, 4 rows affected (0.07 sec)
Records: 4  Duplicates: 0  Warnings: 0
mysql> desc example6;
+--------+-------------+------+-----+---------+----------------+
| Field  | Type        | Null | Key | Default | Extra          |
+--------+-------------+------+-----+---------+----------------+
| id     | int(11)     | NO   | PRI | NULL    | auto_increment |
| stu_id | int(11)     | YES  | UNI | NULL    |                |
| name   | varchar(20) | NO   |     | NULL    |                |
+--------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
修改表名的方法:
alter table t_old_name rename t_new_name;
rename table t_old_name to t_new_name;

转载于:https://www.cnblogs.com/okyoung188/p/6945782.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值