Mysql基礎之 ALTER命令
阿新 • • 發佈:2018-10-11
16px 區別 一句話 nbsp sql基礎 ear 默認 原來 如何
ALTER命令:
作用:當我們修改數據庫的列屬性、列名稱、表名等,要使用ALTER命令
教程:
1、首先是我們創建一個數據庫以及一張表
mysql> create table exercise( -> id int, -> name varchar(15) -> );
1.1、查看表結構
mysql> SHOW COLUMnS FROM exercise; 或者使用 DESC erercise; +-------+-------------+------+-----+---------+-------+ | Field |Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
刪除、添加、修改表字段
1、刪除一個字段,比如id
mysql> ALTER TABLE exercise drop id; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> DESC exercise; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+| name | varchar(15) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 1 row in set (0.00 sec)
2、添加字段
2.1、添加一個字段,比如id_new
mysql> ALTER TABLE exercise ADD COLUMN id_new int; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> DESC exercise; +--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | name | varchar(15) | YES | | NULL | | | id_new | int(11) | YES | | NULL | | +--------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
2.2、將新添加的字段設置為第一列
mysql> ALTER TABLE exercise ADD COLUMN sex varchar(1) FIRST; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> DESC exercise; +--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | sex | varchar(1) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | id_new | int(11) | YES | | NULL | | +--------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
2.3、在某個列之後添加新的字段,比如在name列後添加
mysql> ALTER TABLE exercise ADD COLUMN address char(50) AFTER name; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> DESC exercise; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | sex | varchar(1) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | address | char(50) | YES | | NULL | | | id_new | int(11) | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
總結:添加字段,只有FIRST和AFTER關鍵字兩個
3、修改一個字段屬性及名稱
3.1、修改列屬性
使用modify修改列屬性
mysql> ALTER TABLE exercise modify id_new int(4); #將原來的int(11)修改為int(4) Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> DESC exercise; +--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | name | varchar(15) | YES | | NULL | | | id_new | int(4) | YES | | NULL | | +--------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
使用change修改列屬性
mysql> ALTER TABLE exercise CHANGE id_new id_new int(8); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> DESC exercise; +--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | name | varchar(15) | YES | | NULL | | | id_new | int(8) | YES | | NULL | | +--------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
總結:modify和change的區別:修改列屬性時,使用change比較麻煩,要寫兩次列名稱,而使用modify只需要寫一次列名稱就可以了。
3.2、修改列名字
mysql> ALTER TABLE exercise CHANGE id_new id_new_new; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘‘ at line 1 mysql> ALTER TABLE exercise CHANGE id_new id_new_new int(8); Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> DESC exercise;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| name | varchar(15) | YES | | NULL | |
| id_new_new | int(8) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
總結:修改列名稱需要使用CHANGE,而MODIFY不能修改列的名稱。如果要同時修改名稱和屬性還是CHANGE。使用CHANGE後面要跟上舊列、新列的名稱,以及列屬性。因此修改名稱前先查看列屬性。
一句話:字段名稱CHANGE;字段屬性MODIFY。即名稱又屬性還是CHANGE
4、修改表名稱
mysql> ALTER TABLE exercise rename exercise_new; Query OK, 0 rows affected (0.01 sec) mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | exercise_new | +----------------+ 1 row in set (0.00 sec)
5、ALTER 修改字段的默認值
我們往往在建表的時候給某個列的屬性上面設置默認值,比如“DEFAULT NULL”等等。如何修改默認值呢?首先來查看所有列的默認值
mysql> SHOW COLUMNS FROM exercise; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | sex | varchar(1) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | address | char(50) | YES | | NULL | | | id_new | int(11) | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
基本所有的列默認值DEFAULT都是NULL,現在我們指定“sex”列默認值是“M”
mysql> ALTER TABLE exercise ALTER sex SET DEFAULT ‘M‘; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
查看一下:
mysql> SHOW COLUMNS FROM exercise; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | sex | varchar(1) | YES | | M | | | name | varchar(15) | YES | | NULL | | | address | char(50) | YES | | NULL | | | id_new | int(11) | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
嘗試刪除表中一個列的默認值
mysql> ALTER TABLE exercise ALTER sex drop DEFAULT; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0
再來查看一下:
mysql> SHOW COLUMNS FROM exercise; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | sex | varchar(1) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | address | char(50) | YES | | NULL | | | id_new | int(11) | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
Mysql基礎之 ALTER命令