假設我們存在user表,user結構如下:
mysql> desc user;
+----------+---------------+------+-----+--------- +-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+--------- +-------+
| username | varchar(10) | YES | | NULL | |
| password | varchar(32) | YES | | NULL | |
| createtime | int(10) | YES | | NULL | |
| createip | int(10) | YES | | NULL | |
+----------+---------------+------+-----+--------- +-------+
4 rows in set (0.01 sec)
詳細解示 | |
---|---|
alter table 表名modify 欄位名稱varchar(20); | |
alter table user modify username varchar(20); | |
將user表的username的型別改為varchar(20) |
mysql> alter table user modify username varchar(20);增加表格欄位Query OK, 0 rows affected (0.48 sec)
mysql> desc user;
Records: 0 Duplicates: 0 Warnings: 0+----------+---------------+------+-----+--------- +-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+--------- +-------+
| username | varchar(20) | YES | | NULL | |
| password | varchar(32) | YES | | NULL | |
| createtime | int(10) | YES | | NULL | |
| createip | int(10) | YES | | NULL | |
+----------+---------------+------+-----+--------- +-------+
4 rows in set (0.01 sec)
#詳細解示 | |
---|---|
mysql> alter table emp add column age int(3);
Query OK, 0 rows affected (0.40 sec)
Records: 0 Duplicates: 0 Warnings: 0mysql> desc user;
+----------+---------------+------+-----+--------- +-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+--------- +-------+
| username | varchar(20) | YES | | NULL | |
| password | varchar(32) | YES | | NULL | |
| createtime | int(10) | YES | | NULL | |
| createip | int(10) | YES | | NULL | |
| age | int(3) | YES | | NULL | |
+----------+---------------+------+-----+--------- +-------+
5 rows in set (0.00 sec)
我們剛剛學會增加欄位。如果你仔細實驗發現每次都是增加在最後面,如何在第一個增加或指字字段之後增加呢?
類別 | 詳細解示 |
---|---|
基本語法 | #ALTER TABLE 表名ADD 欄位名字段類型AFTER 欄位名稱; |
#範例 | ALTER TABLE user ADD email VARCHAR(60) AFTER createip; |
範例說明 | user表中,createip後增加一個欄位為email,類型為varchar,長度為60 |
詳細解示 | |
---|---|
##ALTER TABLE 表格名稱ADD欄位名字段類型; | |
ALTER TABLE user ADD id INT(10) FIRST; | |
user表中在最開始的位置增加一個欄位為id,型別為int,長度為10 |
類別 | #詳細解示 |
---|---|
基本語法 | alter table 表名drop column 欄位名稱; |
範例 | alter table user drop columnage; |
範例說明 | 在user表中刪除欄位age |
mysql> alter table user drop column age;
Query OK, 0 rows affected (0.27 sec)
Records: 0 Duplicates: 0 Warnings: 0mysql> desc user;
+----------+---------------+------+-----+--------- +-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+--------- +-------+
| username | varchar(20) | YES | | NULL | |
| password | varchar(32) | YES | | NULL | |
| createtime | int(10) | YES | | NULL | |
| createip | int(10) | YES | | NULL | |
| email | varchar(60) | YES | | NULL | |
+----------+---------------+------+-----+--------- +-------+
5 rows in set (0.00 sec)
詳細解示 | |
---|---|
#基本語法 | alter table 表名change 欄位原始名稱段新名字段類型; |
範例 | alter table user change email em varchar(60); |
#範例說明 | 在user表中將欄位中的email欄位名稱為em |
詳細範例:
mysql> alter table user change email em varchar(60);
Query OK, 0 rows affected (0.38 sec)
Records: 0 Duplicates: 0 Warnings: 0 Warnings: 0 Warnings: 0 Warnings: 0 Warnings: 0 Warnings: 0 Warnings: 0 Warnings: 0 Warnings: 0 Warnings: 0 Warnings: 0 Warnings: 0 Warnings: 0 Warnings: 0.
mysql> desc user;
+----------+---------------+------+-----+--------- +-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+--------- +-------+
| username | varchar(20) | YES | | NULL | |
| password | varchar(32) | YES | | NULL | |
| createtime | int(10) | YES | | NULL | |
| createip | int(10) | YES | | NULL | |
| em | varchar(60) | YES | | NULL | |
+----------+---------------+------+-----+--------- +-------+
5 rows in set (0.00 sec)
在前的欄位增加和修改語句(add/change/modify)中,最後都可以加一個可選項first|after。
增加表格欄位時我們已經學過如何調整順序。我們現在在來看看另外的change或modify如何來調整順序。
我們用first做個小實驗。
mysql> alter table user modify em varchar(60) first;
Query OK, 0 rows affected (0.41 sec)
Records: 0 Duplicates: 0 Warnings: 0mysql> desc user;
+----------+---------------+------+-----+--------- +-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+--------- +-------+
| em | varchar(60) | YES | | NULL | |
| username | varchar(20) | YES | | NULL | |
| password | varchar(32) | YES | | NULL | |
| createtime | int(10) | YES | | NULL | |
| createip | int(10) | YES | | NULL | |
+----------+---------------+------+-----+--------- +-------+
5 rows in set (0.00 sec)
#詳細解示 | |
---|---|
基本語法 | alter table 舊表名rename 新的表名; |
範例 | alter table user rename new_user; |
範例說明 | 將user表名改為new_user |
下一節mysql> alter table user rename new_user;
Query OK, 0 rows affected (0.35 sec)mysql> desc new_user;
+----------+---------------+------+-----+--------- +-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+--------- +-------+
| em | varchar(60) | YES | | NULL | |
| username | varchar(20) | YES | | NULL | |
| password | varchar(32) | YES | | NULL | |
| createtime | int(10) | YES | | NULL | |
| createip | int(10) | YES | | NULL | |
+----------+---------------+------+-----+--------- +-------+
5 rows in set (0.00 sec)