資料欄位操作
假設我們存在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)
修改表格欄位類型modify
#我們執行一下,看看結果:
mysql> alter table user modify username varchar(20); Query OK, 0 rows affected (0.48 sec) Records: 0 Duplicates: 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 | | +----------+---------------+------+-----+---------+-------+ 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: 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 | | | age | int(3) | YES | | NULL | | +----------+---------------+------+-----+---------+-------+ 5 rows in set (0.00 sec)
增加字段時控製字段順序
我們剛剛學會增加字段。如果你仔細實驗發現每次都是增加在最後面,如何在第一個增加或指字字段之後增加呢?
ALTER TABLE user ADD email VARCHAR(60) AFTER createip; Query OK, 0 rows affected (0.40 sec) Records: 0 Duplicates: 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 | | | email | varchar(60) | YES | | NULL | | | age | int(3) | YES | | NULL | | +----------+---------------+------+-----+---------+-------+ 6 rows in set (0.00 sec)
刪除表格欄位
#mysql> alter table user drop column age; Query OK, 0 rows affected (0.27 sec) Records: 0 Duplicates: 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 | | | email | varchar(60) | YES | | NULL | | +----------+---------------+------+-----+---------+-------+ 5 rows in set (0.00 sec)
範例
#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
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做個小實驗。
使用modify調整順序
mysql> alter table user modify em varchar(60) first; Query OK, 0 rows affected (0.41 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> 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)
修改表名
#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)