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;
posted on 2017-06-05 16:51  Harry.Yang  阅读(5894)  评论(0编辑  收藏  举报