MySQL Lecture 5-Modify Table Structure
In MySQL, you can use the ALTER TABLE statement to change the structure of the original data table. The operations involved in modifying the table structure mainly include: (1) adding and deleting fields; (2) modifying the data type of the field; (3) modifying the field name; (4) modifying the arrangement position of the field; (5) adding and deleting complete Sexual constraints; (6) modify the table name; (7) change the storage engine of the table.
The syntax format is as follows:
ALTER TABLE <表名> [修改选项];
[修改选项]的格式为:
ADD COLUMN <列名> <类型>
| CHANGE COLUMN <旧列名> <新列名> <新列类型>
| ALTER COLUMN <列名> [ SET DEFAULT <默认值> | DROP DEFAULT ]
| MODIFY COLUMN <列名> <类型>
| DROP COLUMN <列名>
| RENAME TO <新表名>
The following example uses the employee table, which has the following structure:
mysql> desc employee;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | char(10) | YES | | NULL | |
| addr | varchar(100) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
One, add and delete fields
1. Add fields
The syntax format is as follows:
ALTER TABLE <表名> ADD COLUMN <列名> <类型> <完整性约束> [FIRST | AFTER 列名];
说明:使用 [FIRST | AFTER 列名] 选项可以设置新增字段的位置。
For example:
(1) Add a column phone
mysql> alter table employee add phone char(20);
Query OK, 0 rows affected (0.17 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc employee;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | char(10) | YES | | NULL | |
| addr | varchar(100) | YES | | NULL | |
| phone | char(20) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
(2) Add a column salary, the default value is 0, can not take a null value
mysql> alter table employee add salary decimal(10,2) default 0 not null;
Query OK, 0 rows affected (0.11 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc employee;
+--------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | char(10) | YES | | NULL | |
| addr | varchar(100) | YES | | NULL | |
| phone | char(20) | YES | | NULL | |
| salary | decimal(10,2) | NO | | 0.00 | |
+--------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
(3) Add a new field birth, and put it after the name field
mysql> ALTER TABLE employee ADD COLUMN birth datetime after name;
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc employee;
+--------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | char(10) | YES | | NULL | |
| birth | datetime | YES | | NULL | |
| addr | varchar(100) | YES | | NULL | |
| phone | char(20) | YES | | NULL | |
| salary | decimal(10,2) | NO | | 0.00 | |
+--------+---------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
2. Delete the field
The syntax format is as follows:
ALTER TABLE <表名> DROP COLUMN <列名>;
Example: Delete the salary field in the employee table
mysql> alter table employee drop column salary;
Query OK, 0 rows affected (0.17 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc employee;
+--------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | char(10) | YES | | NULL | |
| birth | datetime | YES | | NULL | |
| addr | varchar(100) | YES | | NULL | |
| phone | char(20) | YES | | NULL | |
+--------+---------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
Two, modify the data type, name and location of the field
1. Modify the data type of the field
Use the MODIFY option to modify the data type of the field. The syntax format is as follows:
ALTER TABLE <表名> MODIFY COLUMN <列名> <类型>;
说明:使用 MODIFY 选项不能更改字段的名称。
Example: Change the type of the field name to varchar, with a length of 20
mysql> alter table employee modify name varchar(20);
Query OK, 2 rows affected (0.05 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> desc employee;
+--------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
| birth | datetime | YES | | NULL | |
| addr | varchar(100) | YES | | NULL | |
| phone | char(20) | YES | | NULL | |
| salary | decimal(10,2) | NO | | 0.00 | |
+--------+---------------+------+-----+---------+-------+
6 rows in set (0.01 sec)
2. Modify the name of the field
Use the CHANGE option to modify the name of the field. The syntax format is as follows:
ALTER TABLE <表名> CHANGE COLUMN <旧列名> <新列名> <新列类型>;
说明:使用 CHANGE 选项时,如果新列名和旧列名相同,则作用和 MODIFY 相同。
Example: Change the name of the addr column to address and the type to varchar(200), and put it after the field phone
mysql> alter table employee change addr address varchar(200) after phone;
Query OK, 0 rows affected (0.13 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc employee;
+---------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
| birth | datetime | YES | | NULL | |
| phone | char(20) | YES | | NULL | |
| address | varchar(200) | YES | | NULL | |
| salary | decimal(10,2) | NO | | 0.00 | |
+---------+---------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
3. Change the position of the field
When using the ADD option to add fields, using the MODIFY option to modify the type and length of the field, and using the CHANGE option to modify the field name and type, you can use the [FIRST | AFTER column name] option to specify the location of the field. The syntax format is as follows:
--添加字段同时指定新增字段的位置
ALTER TABLE <表名> ADD COLUMN <列名> <类型> <完整性约束> [FIRST | AFTER 列名];
--修改字段的类型和长度同时指定字段的位置
ALTER TABLE <表名> MODIFY COLUMN <列名> <类型> [FIRST | AFTER 列名];
--修改字段的名称、类型和长度同时指定字段的位置
ALTER TABLE <表名> CHANGE COLUMN <旧列名> <新列名> <新列类型> [FIRST | AFTER 列名];
For example:
(1) Add a field comm, the type is varchar(500), the position is in the first place
mysql> alter table employee add comm varchar(500) first;
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc employee;
+---------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| comm | varchar(500) | YES | | NULL | |
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
| birth | datetime | YES | | NULL | |
| phone | char(20) | YES | | NULL | |
| address | varchar(200) | YES | | NULL | |
| salary | decimal(10,2) | NO | | 0.00 | |
+---------+---------------+------+-----+---------+-------+
7 rows in set (0.01 sec)
(2) Modify the length of the field comm to 1000 and put it before the salary field
mysql> alter table employee modify comm varchar(1000) after address;
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc employee;
+---------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
| birth | datetime | YES | | NULL | |
| phone | char(20) | YES | | NULL | |
| address | varchar(200) | YES | | NULL | |
| comm | varchar(1000) | YES | | NULL | |
| salary | decimal(10,2) | NO | | 0.00 | |
+---------+---------------+------+-----+---------+-------+
7 rows in set (0.01 sec)
(3) Change the name of the field comm to comme and place it at the end of the table
mysql> alter table employee change comm comme varchar(1000) after salary;
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc employee;
+---------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
| birth | datetime | YES | | NULL | |
| phone | char(20) | YES | | NULL | |
| address | varchar(200) | YES | | NULL | |
| salary | decimal(10,2) | NO | | 0.00 | |
| comme | varchar(1000) | YES | | NULL | |
+---------+---------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
Three, modify the table name
The syntax format is as follows:
ALTER TABLE <表名> RENAME [TO] <新表名>;
Example: Change the name of the data table employee to emp
mysql> alter table employee rename emp;
Query OK, 0 rows affected (0.10 sec)
mysql> show tables;
+-----------------+
| Tables_in_hist |
+-----------------+
| dept |
| emp |
| stu |
| student |
| user_permission |
+-----------------+
5 rows in set (0.00 sec)