[关闭]
@yanglt7 2018-06-06T06:42:29.000000Z 字数 4029 阅读 951

9 MySQL ALTER命令

MySQL


当我们需要修改数据表名或者修改数据表字段时,就需要使用到MySQL ALTER命令。

创建表:testalter_tbl

  1. mysql> create table testalter_tbl
  2. -> (
  3. -> i INT,
  4. -> c CHAR(1)
  5. -> );
  6. mysql> show columns from testalter_tbl;
  7. +-------+---------+------+-----+---------+-------+
  8. | Field | Type | Null | Key | Default | Extra |
  9. +-------+---------+------+-----+---------+-------+
  10. | i | int(11) | YES | | NULL | |
  11. | c | char(1) | YES | | NULL | |
  12. +-------+---------+------+-----+---------+-------+

删除、添加或修改表字段

  1. mysql> alter table testalter_tbl drop i;
  1. mysql> alter table testalter_tbl add i int;
  1. mysql> show columns from testalter_tbl;
  2. +-------+---------+------+-----+---------+-------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +-------+---------+------+-----+---------+-------+
  5. | c | char(1) | YES | | NULL | |
  6. | i | int(11) | YES | | NULL | |
  7. +-------+---------+------+-----+---------+-------+
  1. mysql> alter table testalter_tbl drop i;
  2. mysql> alter table testalter_tbl add i int first;
  3. mysql> show columns from testalter_tbl;
  4. +-------+---------+------+-----+---------+-------+
  5. | Field | Type | Null | Key | Default | Extra |
  6. +-------+---------+------+-----+---------+-------+
  7. | i | int(11) | YES | | NULL | |
  8. | c | char(1) | YES | | NULL | |
  9. +-------+---------+------+-----+---------+-------+
  10. mysql> alter table testalter_tbl drop i;
  11. mysql> alter table testalter_tbl add i int after c;
  12. mysql> show columns from testalter_tbl;
  13. +-------+---------+------+-----+---------+-------+
  14. | Field | Type | Null | Key | Default | Extra |
  15. +-------+---------+------+-----+---------+-------+
  16. | c | char(1) | YES | | NULL | |
  17. | i | int(11) | YES | | NULL | |
  18. +-------+---------+------+-----+---------+-------+

修改字段类型及名称

如果需要修改字段类型及名称, 你可以在ALTER命令中使用 MODIFY 或 CHANGE 子句 。

  1. mysql> alter table testalter_tbl modify c char(10);
  1. mysql> alter table testalter_tbl change i j bigint;
  2. mysql> alter table testalter_tbl change j j int;
  3. mysql> show columns from testalter_tbl;
  4. +-------+----------+------+-----+---------+-------+
  5. | Field | Type | Null | Key | Default | Extra |
  6. +-------+----------+------+-----+---------+-------+
  7. | c | char(10) | YES | | NULL | |
  8. | j | int(11) | YES | | NULL | |
  9. +-------+----------+------+-----+---------+-------+

ALTER TABLE 对 Null 值和默认值的影响

当你修改字段时,你可以指定是否包含或者是否设置默认值。

以下实例,指定字段 j 为 NOT NULL 且默认值为100 。

  1. mysql> alter table testalter_tbl modify j bigint not null default 100;
  2. mysql> show columns from testalter_tbl;
  3. +-------+------------+------+-----+---------+-------+
  4. | Field | Type | Null | Key | Default | Extra |
  5. +-------+------------+------+-----+---------+-------+
  6. | c | char(10) | YES | | NULL | |
  7. | j | bigint(20) | NO | | 100 | |
  8. +-------+------------+------+-----+---------+-------+

如果你不设置默认值,MySQL会自动设置该字段默认为 NULL。

修改字段默认值

  1. mysql> alter table testalter_tbl alter j set default 1000;
  2. mysql> show columns from testalter_tbl;
  3. +-------+------------+------+-----+---------+-------+
  4. | Field | Type | Null | Key | Default | Extra |
  5. +-------+------------+------+-----+---------+-------+
  6. | c | char(10) | YES | | NULL | |
  7. | j | bigint(20) | NO | | 1000 | |
  8. +-------+------------+------+-----+---------+-------+
  1. mysql> alter table testalter_tbl alter j drop default;
  2. mysql> show columns from testalter_tbl;
  3. +-------+------------+------+-----+---------+-------+
  4. | Field | Type | Null | Key | Default | Extra |
  5. +-------+------------+------+-----+---------+-------+
  6. | c | char(10) | YES | | NULL | |
  7. | j | bigint(20) | NO | | NULL | |
  8. +-------+------------+------+-----+---------+-------+
  1. mysql> show table status like 'testalter_tbl'\G
  2. *************************** 1. row ***************************
  3. Name: testalter_tbl
  4. Engine: MyISAM
  5. Version: 10
  6. Row_format: Fixed
  7. Rows: 0
  8. Avg_row_length: 0
  9. Data_length: 0
  10. Max_data_length: 10977524091715583
  11. Index_length: 1024
  12. Data_free: 0
  13. Auto_increment: NULL
  14. Create_time: 2017-08-11 10:27:27
  15. Update_time: 2017-08-11 10:27:27
  16. Check_time: NULL
  17. Collation: utf8_general_ci
  18. Checksum: NULL
  19. Create_options:
  20. Comment:

修改表名

如果需要修改数据表的名称,可以在 ALTER TABLE 语句中使用 RENAME 子句来实现。
尝试以下实例将数据表 testalter_tbl 重命名为 alter_tbl:

  1. mysql> alter table testalter_tbl rename to alter_tbl;
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注