快速理解MySQL中主鍵與外鍵的執行個體教程_Mysql

來源:互聯網
上載者:User

主鍵與外鍵的關係,通俗點兒講,我現在有一個論壇,有兩張表,一張是主貼 thread,一張是回帖 reply

先說說主鍵,主鍵是表裡面唯一識別記錄的欄位,一般是文章id,體現在訪問的時候,例如是
thread.php?id=1   表示我要訪問的是文章id是1 的文章~

再來說說外鍵,當我們刪除某個文章的時候,需要執行另一個操作,就是刪除所有回帖,如果正常情況下,我們需要執行兩次delete操作(thread和 reply),這時候如果存在外鍵,例如,在reply 表裡面建立一個指向thread表的主鍵(id)的外鍵(這個外鍵綁的欄位,必須是對應文章的id),並指定響應 delete ,那你在刪除 thread 的時候,mysql 自己會幫你把 reply 表中這個文章的回複都刪掉,而不需要你手動再去執行一次reply表的delete操作~

至於兩者之間的關係,在剛才的例子中,reply 表的外鍵,指向的就是 thread 表的主鍵~~

搞個例子,簡單示範一下使用,做dage和xiaodi兩個表,大哥表是主鍵,小弟表是外鍵:
建表:

CREATE TABLE `dage` ( `id` int(11) NOT NULL auto_increment, `name` varchar(32) default '', PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;CREATE TABLE `xiaodi` ( `id` int(11) NOT NULL auto_increment, `dage_id` int(11) default NULL, `name` varchar(32) default '', PRIMARY KEY (`id`), KEY `dage_id` (`dage_id`), CONSTRAINT `xiaodi_ibfk_1` FOREIGN KEY (`dage_id`) REFERENCES `dage` (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;

插入個大哥:

mysql> insert into dage(name) values('銅鑼灣');
Query OK, 1 row affected (0.01 sec)
mysql> select * from dage;
+----+--------+| id | name  |+----+--------+| 1 | 銅鑼灣 |+----+--------+1 row in set (0.00 sec)

插入個小弟:

mysql> insert into xiaodi(dage_id,name) values(1,'銅鑼灣_小弟A');
Query OK, 1 row affected (0.02 sec)
mysql> select * from xiaodi;
+----+---------+--------------+| id | dage_id | name     |+----+---------+--------------+| 1 |    1 | 銅鑼灣_小弟A |+----+---------+--------------+

把大哥刪除:

mysql> delete from dage where id=1;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`bstar/xiaodi`, CONSTRAINT `xiaodi_ibfk_1` FOREIGN KEY (`dage_id`) REFERENCES `dage` (`id`))

提示:不行呀,有約束的,大哥下面還有小弟,可不能扔下我們不管呀!

插入一個新的小弟:

mysql> insert into xiaodi(dage_id,name) values(2,'旺角_小弟A');   

       

2ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`bstar/xiaodi`, CONSTRAINT `xiaodi_ibfk_1` FOREIGN KEY (`dage_id`) REFERENCES `dage` (`id`))


提示:小子,想造反呀!你還沒大哥呢!

把外鍵約束增加事件觸發限制:

mysql> show create table xiaodi;

CONSTRAINT `xiaodi_ibfk_1` FOREIGN KEY (`dage_id`) REFERENCES `dage` (`id`)

mysql> alter table xiaodi drop foreign key xiaodi_ibfk_1;

Query OK, 1 row affected (0.04 sec)Records: 1 Duplicates: 0 Warnings: 
mysql> alter table xiaodi add foreign key(dage_id) references dage(id) on delete cascade on update cascade;
Query OK, 1 row affected (0.04 sec)Records: 1 Duplicates: 0 Warnings: 0

再次試著把大哥刪了:

mysql> delete from dage where id=1;
Query OK, 1 row affected (0.01 sec)
mysql> select * from dage;
Empty set (0.01 sec)
mysql> select * from xiaodi;
Empty set (0.00 sec)


得,這回對應的小弟也沒了,沒辦法,誰讓你跟我on delete cascade了呢!

例子說明的應該蠻清楚了吧,其他功能對應手冊自己實踐吧!:-)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.