MYSQL資料庫(八)- 修改資料表新增約束
阿新 • • 發佈:2019-01-25
本章目錄
- 新增約束目錄
一、新增id列,無主鍵無約束,準備工作
二、給city2表新增主鍵約束(任何一張表只能有一個主鍵)
三、新增唯一約束
四、新增外來鍵約束
五、新增和/刪除預設約束
- 刪除約束目錄
一、刪除主鍵約束
二、刪除唯一約束
三、刪除外來鍵約束
本章重要語句
本章案例:
準備工作,建立一個city2資料表,不新增任何主鍵和約束
建立一個city2資料表,如下
mysql> create table city2(
-> username varchar(20) NOT NULL,
-> pid smallint unsigned);
Query OK, 0 rows affected (0.12 sec)//建立成功
mysql> show columns from city2;//檢視資料表city2,而且沒有新增主鍵和約束。
+----------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| username | varchar(20) | NO | | NULL | |
| pid | smallint(5) unsigned | YES | | NULL | |
+----------+----------------------+------+-----+---------+-------+
——————————–新增約束————————
一、新增id列,還沒有新增主鍵
語句:alter table city2 add id smallint unsigned first;
解析:更改資料表city2,新增id列,型別smallint ,無符號型別,位於列頂
ysql> alter table city2 add id smallint unsigned first;
Query OK, 0 rows affected (0.30 sec)//新增成功
mysql> show columns from city2;//驗證插入結果
//插入前
+----------+ ----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| username | varchar(20) | NO | | NULL | |
| pid | smallint(5) unsigned | YES | | NULL | |
+----------+----------------------+------+-----+---------+-------+
//插入後
+----------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| id | smallint(5) unsigned | YES | | NULL | |
| username | varchar(20) | NO | | NULL | |
| pid | smallint(5) unsigned | YES | | NULL | |
+----------+----------------------+------+-----+---------+-------+
二、給city2表新增主鍵約束(任何一張表只能有一個主鍵)
語句:alter table city2 add constraint ky_city2_id PRIMARY KEY(id)
解析:更改列表city2,新增約束,約束名稱:ky_city2_id 主鍵列(id列表)
mysql> alter table city2 add constraint ky_city2_id PRIMARY KEY(id);
Query OK, 0 rows affected (0.32 sec)//新增主鍵成功
mysql> show columns from city2;//檢視city2表格
//插入前
+----------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| id | smallint(5) unsigned | YES | | NULL | |
| username | varchar(20) | NO | | NULL | |
| pid | smallint(5) unsigned | YES | | NULL | |
+----------+----------------------+------+-----+---------+-------+
//插入後,id主鍵新增成功,看key列。
+----------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| id | smallint(5) unsigned | NO | PRI | 0 | |
| username | varchar(20) | NO | | NULL | |
| pid | smallint(5) unsigned | YES | | NULL | |
+----------+----------------------+------+-----+---------+-------+
三、新增唯一約束
注意:和主鍵約束不一樣的是,主鍵約束只能有一個,而唯一約束可以為多個
語句:alter table city2 add unique(username)
- 解析:更改表格city2,新增唯一約束(username列)
mysql> alter table city2 add unique(username);
Query OK, 0 rows affected (0.25 sec)//新增唯一約束成功
mysql> show columns from city2;//檢視city2表格
//新增前
+----------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| id | smallint(5) unsigned | NO | PRI | 0 | |
| username | varchar(20) | NO | | NULL | |
| pid | smallint(5) unsigned | YES | | NULL | |
+----------+----------------------+------+-----+---------+-------+
//新增後,username列後面的唯一約束新增成功
+----------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| id | smallint(5) unsigned | NO | PRI | 0 | |
| username | varchar(20) | NO | UNI | NULL | |
| pid | smallint(5) unsigned | YES | | NULL | |
+----------+----------------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
四、新增外來鍵約束
語句:alter table city2 add foreign key(pid) references sheng(id)
解析:更改表格city2,新增外來鍵約束(pid列)參照資料表“sheng”(id)列
mysql> alter table city2 add foreign key(pid) references sheng(id);
Query OK, 0 rows affected (0.32 sec)//插入新增成功
mysql> show columns from city2;//檢視city2資料表
//插入前pid是沒有外來鍵約素
+----------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| id | smallint(5) unsigned | NO | PRI | 0 | |
| username | varchar(20) | NO | UNI | NULL | |
| pid | smallint(5) unsigned | YES | | NULL | |
+----------+----------------------+------+-----+---------+-------+
//插入前pid是外來鍵約素插入成功
+----------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| id | smallint(5) unsigned | NO | PRI | 0 | |
| username | varchar(20) | NO | UNI | NULL | |
| pid | smallint(5) unsigned | YES | MUL | NULL | |
+----------+----------------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
五、新增和/刪除預設約束
語句:alter table tab_name alter[column] col_name{set default literal|drop default}
解析:更改 資料 某某表,更改【列】 某某列名稱{新增預設約束或者刪除預設約束}
- 1、向資料表中新增一個新列age,不指定預設值
mysql> alter table city2 add age tinyint UNSIGNED NOT NULL;
Query OK, 0 rows affected (0.30 sec)
//插入成功
mysql> SHOW COLUMNS FROM CITY2;
//插入前
+----------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| id | smallint(5) unsigned | NO | PRI | 0 | |
| username | varchar(20) | NO | UNI | NULL | |
| pid | smallint(5) unsigned | YES | MUL | NULL | |
+----------+----------------------+------+-----+---------+-------+
//插入後,age列存在,預設為null
+----------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| id | smallint(5) unsigned | NO | PRI | 0 | |
| username | varchar(20) | NO | UNI | NULL | |
| pid | smallint(5) unsigned | YES | MUL | NULL | |
| age | tinyint(3) unsigned | NO | | NULL | |
+----------+----------------------+------+-----+---------+-------+
4 rows in set (0.02 sec)
- 2、新增預設值(age賦值為15)
mysql> alter table city2 alter age set default 15;
Query OK, 0 rows affected (0.15 sec)//新增成功
mysql> show columns from city2;
//插入前age預設值為null
+----------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| id | smallint(5) unsigned | NO | PRI | 0 | |
| username | varchar(20) | NO | UNI | NULL | |
| pid | smallint(5) unsigned | YES | MUL | NULL | |
| age | tinyint(3) unsigned | NO | | NULL | |
+----------+----------------------+------+-----+---------+-------+
//插入後預設值為15
+----------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| id | smallint(5) unsigned | NO | PRI | 0 | |
| username | varchar(20) | NO | UNI | NULL | |
| pid | smallint(5) unsigned | YES | MUL | NULL | |
| age | tinyint(3) unsigned | NO | | 15 | |
+----------+----------------------+------+-----+---------+-------+
- 三、刪除預設值
mysql> alter table city2 alter age drop default ;
Query OK, 0 rows affected (0.18 sec)
//成功
mysql> show columns from city2;
//刪除前,預設值為15
+----------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| id | smallint(5) unsigned | NO | PRI | 0 | |
| username | varchar(20) | NO | UNI | NULL | |
| pid | smallint(5) unsigned | YES | MUL | NULL | |
| age | tinyint(3) unsigned | NO | | 15 | |
+----------+----------------------+------+-----+---------+-------+
//刪除後,age的預設值為null
+----------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| id | smallint(5) unsigned | NO | PRI | 0 | |
| username | varchar(20) | NO | UNI | NULL | |
| pid | smallint(5) unsigned | YES | MUL | NULL | |
| age | tinyint(3) unsigned | NO | | NULL | |
+----------+----------------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
——————————–刪除約束————————
一、刪除主鍵約束
- 語句:alter table tal_name drop primary key
解析:更改 某某資料表 刪除 主鍵
注意:刪除住家的時候,drop後面為什麼不用新增列的名稱,因為任何一張表只有一個主鍵,所以我們無需去新增。
mysql> alter table city2 drop primary key;
Query OK, 0 rows affected (0.25 sec)//刪除主鍵成功
mysql> show columns from city2;//檢視資料表city2
//刪除主鍵前 id存在主鍵key存在
+----------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| id | smallint(5) unsigned | NO | PRI | 0 | |
| username | varchar(20) | NO | UNI | NULL | |
| pid | smallint(5) unsigned | YES | MUL | NULL | |
| age | tinyint(3) unsigned | NO | | NULL | |
+----------+----------------------+------+-----+---------+-------+
//刪除主鍵後,id的主鍵key已經被刪除
+----------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| id | smallint(5) unsigned | NO | | 0 | |
| username | varchar(20) | NO | PRI | NULL | |
| pid | smallint(5) unsigned | YES | MUL | NULL | |
| age | tinyint(3) unsigned | NO | | NULL | |
+----------+----------------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
二、刪除唯一約束
- 語句:alter table tal_name drop{index|key} index_name
- 解釋:更改 某某資料表 刪除{index|ke} 列名稱
//刪除username的唯一約束
mysql> alter table city2 drop index username;
Query OK, 0 rows affected (0.29 sec)//刪除username的唯一約束成功
mysql> show columns from city2;//查看錶結構
//刪除前,username的唯一約束存在
+----------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| id | smallint(5) unsigned | NO | | 0 | |
| username | varchar(20) | NO | PRI | NULL | |
| pid | smallint(5) unsigned | YES | MUL | NULL | |
| age | tinyint(3) unsigned | NO | | NULL | |
+----------+----------------------+------+-----+---------+-------+
//刪除前,username的唯一約束key已經不存在,但是username這個引數還存在。
+----------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| id | smallint(5) unsigned | NO | | 0 | |
| username | varchar(20) | NO | | NULL | |
| pid | smallint(5) unsigned | YES | MUL | NULL | |
| age | tinyint(3) unsigned | NO | | NULL | |
+----------+----------------------+------+-----+---------+-------+
三、刪除外來鍵約束
- 語句:alter table tbl_name drop forign key fk_symbol(fk_symbol:外來鍵名稱,自己定義的);
- 解析更改 某某資料表 刪除 外來鍵約束 外簡約束名稱
如何檢視我們之前建立外來鍵約束的名稱?
語句:show create table city2; //檢視建立的表格city2
| city2 | CREATE TABLE `city2` (
`id` smallint(5) unsigned NOT NULL DEFAULT '0',
`username` varchar(20) NOT NULL,
`pid` smallint(5) unsigned DEFAULT NULL,
`age` tinyint(3) unsigned NOT NULL,
KEY `pid` (`pid`),
CONSTRAINT `city2_ibfk_1` FOREIGN KEY (`pid`) REFERENCES `sheng` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
知道外來鍵約束的名稱之後,我們就可以刪除這個約束,如下:
alter table city2 drop forign key city2_ibfk_1;//更改資料表city2,刪除外來鍵約束city2_ibfk_1