(1)添加列

  • 单列

    ALTER TABLE tab_name ADD [COLUMN] col_name column_definition [FIRST | AFTER col_name]

  • 多列

    ALTER TABLE tab_name ADD [COLUMN] (col_name column_definition,…)

  • 例子

    alter table tab add password tinyint; alter table tab add (sex smallint,salary smallint); alter table tab add career varchar(20) first; alter table tab add age tinyint after password;

  1. mysql> create table tab(
  2. -> id tinyint primary key auto_increment,
  3. -> username varchar(20)
  4. -> );
  5. Query OK, 0 rows affected (0.03 sec)
  6. mysql> show columns from tab;
  7. +----------+-------------+------+-----+---------+----------------+
  8. | Field | Type | Null | Key | Default | Extra |
  9. +----------+-------------+------+-----+---------+----------------+
  10. | id | tinyint(4) | NO | PRI | NULL | auto_increment |
  11. | username | varchar(20) | YES | | NULL | |
  12. +----------+-------------+------+-----+---------+----------------+
  13. 2 rows in set (0.00 sec)
  14. mysql> alter table tab add password tinyint;
  15. Query OK, 0 rows affected (0.10 sec)
  16. Records: 0 Duplicates: 0 Warnings: 0
  17. mysql> alter table tab add (sex smallint,salary smallint);
  18. Query OK, 0 rows affected (0.04 sec)
  19. Records: 0 Duplicates: 0 Warnings: 0
  20. mysql> show columns from tab;
  21. +----------+-------------+------+-----+---------+----------------+
  22. | Field | Type | Null | Key | Default | Extra |
  23. +----------+-------------+------+-----+---------+----------------+
  24. | id | tinyint(4) | NO | PRI | NULL | auto_increment |
  25. | username | varchar(20) | YES | | NULL | |
  26. | password | tinyint(4) | YES | | NULL | |
  27. | sex | smallint(6) | YES | | NULL | |
  28. | salary | smallint(6) | YES | | NULL | |
  29. +----------+-------------+------+-----+---------+----------------+
  30. 5 rows in set (0.00 sec)
  31. mysql> alter table tab add career varchar(20) first;
  32. Query OK, 0 rows affected (0.03 sec)
  33. Records: 0 Duplicates: 0 Warnings: 0
  34. mysql> show columns from tab;
  35. +----------+-------------+------+-----+---------+----------------+
  36. | Field | Type | Null | Key | Default | Extra |
  37. +----------+-------------+------+-----+---------+----------------+
  38. | career | varchar(20) | YES | | NULL | |
  39. | id | tinyint(4) | NO | PRI | NULL | auto_increment |
  40. | username | varchar(20) | YES | | NULL | |
  41. | password | tinyint(4) | YES | | NULL | |
  42. | sex | smallint(6) | YES | | NULL | |
  43. | salary | smallint(6) | YES | | NULL | |
  44. +----------+-------------+------+-----+---------+----------------+
  45. 6 rows in set (0.00 sec)
  46. mysql> alter table tab add age tinyint after password;
  47. Query OK, 0 rows affected (0.05 sec)
  48. Records: 0 Duplicates: 0 Warnings: 0
  49. mysql> show columns from tab;
  50. +----------+-------------+------+-----+---------+----------------+
  51. | Field | Type | Null | Key | Default | Extra |
  52. +----------+-------------+------+-----+---------+----------------+
  53. | career | varchar(20) | YES | | NULL | |
  54. | id | tinyint(4) | NO | PRI | NULL | auto_increment |
  55. | username | varchar(20) | YES | | NULL | |
  56. | password | tinyint(4) | YES | | NULL | |
  57. | age | tinyint(4) | YES | | NULL | |
  58. | sex | smallint(6) | YES | | NULL | |
  59. | salary | smallint(6) | YES | | NULL | |
  60. +----------+-------------+------+-----+---------+----------------+
  61. 7 rows in set (0.00 sec)

(2)删除列

  • ALTER TABLE tab_name DROP [COLUMN] col_name column_definition,DROP …
  • 例子

    alter table tab drop career; alter table tab drop age,drop sex; alter table tab add sex int,drop salary;

mysql> alter table tab drop career;
Query OK, 0 rows affected (0.09 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from tab;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | tinyint(4)  | NO   | PRI | NULL    | auto_increment |
| username | varchar(20) | YES  |     | NULL    |                |
| password | tinyint(4)  | YES  |     | NULL    |                |
| age      | tinyint(4)  | YES  |     | NULL    |                |
| sex      | smallint(6) | YES  |     | NULL    |                |
| salary   | smallint(6) | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

mysql> alter table tab drop age,drop sex;
Query OK, 0 rows affected (0.08 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from tab;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | tinyint(4)  | NO   | PRI | NULL    | auto_increment |
| username | varchar(20) | YES  |     | NULL    |                |
| password | tinyint(4)  | YES  |     | NULL    |                |
| salary   | smallint(6) | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql> alter table tab add sex int,drop salary;
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from tab;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | tinyint(4)  | NO   | PRI | NULL    | auto_increment |
| username | varchar(20) | YES  |     | NULL    |                |
| password | tinyint(4)  | YES  |     | NULL    |                |
| sex      | int(11)     | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

(3)修改列

修改列定义

  • 修改列定义(列名或者位置)
  • ALTER TABLE tb_name MODIFY [COLUMNS] col_name colum_definition [FIRST | AFTER col_name];

    alter table tb3 modify username smallint unsigned not null first;

mysql> create table tb3(
    -> id int primary key auto_increment,
    -> username varchar(20)
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> desc tb3;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(11)     | NO   | PRI | NULL    | auto_increment |
| username | varchar(20) | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> alter table tb3 modify username smallint unsigned not null first;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc tb3;
+----------+----------------------+------+-----+---------+----------------+
| Field    | Type                 | Null | Key | Default | Extra          |
+----------+----------------------+------+-----+---------+----------------+
| username | smallint(5) unsigned | NO   |     | NULL    |                |
| id       | int(11)              | NO   | PRI | NULL    | auto_increment |
+----------+----------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

修改列名称

  • 修改列名称
  • ALTER TABLE tb_name CHANGE [COLUMNS] old_col_name new_col_name colum_definition [FIRST | AFTER col_name];(不仅修改列的定义,还可以改名称)

    alter table tb3 change id p_id varchar(20) not null unique;

mysql> alter table tb3 change id p_id varchar(20) not null unique;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc tb3;
+----------+----------------------+------+-----+---------+-------+
| Field    | Type                 | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| username | smallint(5) unsigned | NO   |     | NULL    |       |
| p_id     | varchar(20)          | NO   | PRI | NULL    |       |
+----------+----------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

(4)修改约束

添加删除默认约束

  • 默认约束
  • ALTER TABLE ta_name ALTER[COLUMN] col_name {SET DEFAULT litera | DROP DEFAULT};

    alter table user alter sex set default 15; alter table user alter sex drop default;

mysql> alter table user alter sex set default 15;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from user;
+----------+----------------------+------+-----+---------+-------+
| Field    | Type                 | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| username | varchar(20)          | NO   | UNI | NULL    |       |
| sex      | smallint(6)          | YES  |     | 15      |       |
| id       | smallint(5) unsigned | NO   | PRI | NULL    |       |
| pid      | smallint(6)          | YES  | MUL | NULL    |       |
+----------+----------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> alter table user alter sex drop default;
Query OK, 0 rows affected (0.07 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from user;
+----------+----------------------+------+-----+---------+-------+
| Field    | Type                 | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| username | varchar(20)          | NO   | UNI | NULL    |       |
| sex      | smallint(6)          | YES  |     | NULL    |       |
| id       | smallint(5) unsigned | NO   | PRI | NULL    |       |
| pid      | smallint(6)          | YES  | MUL | NULL    |       |
+----------+----------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

添加外键约束

  • ALTER TABLE ta_name ADD [CONSTRAINT[symbol]] FOREIGN KEY (index_col_name,…) reference_definition;

    alter table user add foreign key (pid) references person(id);

mysql> create table person( 
    -> id smallint primary key auto_increment);
Query OK, 0 rows affected (0.08 sec)

mysql> alter table user add pid smallint;
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from user;
+----------+----------------------+------+-----+---------+-------+
| Field    | Type                 | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| username | varchar(20)          | NO   | UNI | NULL    |       |
| sex      | smallint(6)          | YES  |     | NULL    |       |
| id       | smallint(5) unsigned | NO   | PRI | NULL    |       |
| pid      | smallint(6)          | YES  |     | NULL    |       |
+----------+----------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> alter table user add foreign key (pid) references person(id);
Query OK, 0 rows affected (0.09 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table user;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                                                                                                                                                     |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| user  | CREATE TABLE `user` (
  `username` varchar(20) NOT NULL,
  `sex` smallint(6) DEFAULT NULL,
  `id` smallint(5) unsigned NOT NULL,
  `pid` smallint(6) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`),
  KEY `pid` (`pid`),
  CONSTRAINT `user_ibfk_1` FOREIGN KEY (`pid`) REFERENCES `person` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

删除外键约束

  • 删除外键约束
  • ALTER TABLE tb_name DROP FOREIGN KEY fk_symbol;删除外键约束
  • 先找到外键的symbol,按这个删除外键,再DROP INDEX

    alter table user drop foreign key user_ibfk_1;

mysql> show create table user;
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                                                                                                                               |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| user  | CREATE TABLE `user` (
  `username` varchar(20) NOT NULL,
  `id` smallint(5) unsigned NOT NULL,
  `pid` smallint(6) DEFAULT NULL,
  `sex` smallint(6) DEFAULT NULL,
  UNIQUE KEY `username` (`username`),
  KEY `pid` (`pid`),
  CONSTRAINT `user_ibfk_1` FOREIGN KEY (`pid`) REFERENCES `person` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> alter table user drop foreign key user_ibfk_1;
Query OK, 0 rows affected (0.08 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table user;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                                                    |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| user  | CREATE TABLE `user` (
  `username` varchar(20) NOT NULL,
  `id` smallint(5) unsigned NOT NULL,
  `pid` smallint(6) DEFAULT NULL,
  `sex` smallint(6) DEFAULT NULL,
  UNIQUE KEY `username` (`username`),
  KEY `pid` (`pid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)