MySQL外鍵的作用

來源:互聯網
上載者:User

標籤:font   efault   sql外鍵   cannot   str   ble   span   div   img   

MySQL外鍵的作用:

保持資料一致性,完整性,主要目的是控制儲存在外鍵表中的資料。使兩張表形成關聯,外鍵只能引用外表中列的值!

我們來建兩個表

CREATE TABLE `example1` (  `stu_id` int(11) NOT NULL DEFAULT ‘0‘,  `course_id` int(11) NOT NULL DEFAULT ‘0‘,  `grade` float DEFAULT NULL,  PRIMARY KEY (`stu_id`,`course_id`));CREATE TABLE `example2` (  `id` int(11) NOT NULL,  `stu_id` int(11) DEFAULT NULL,  `course_id` int(11) DEFAULT NULL,  PRIMARY KEY (`id`),  KEY `f_ck` (`stu_id`,`course_id`),  CONSTRAINT `f_ck` FOREIGN KEY (`stu_id`, `course_id`) REFERENCES `example1` (`stu_id`, `course_id`));insert into example1 (stu_id,course_id,grade)values(1,1,98.5),(2,2,89);insert into example2 (id,stu_id,course_id)values(1,1,1),(2,2,2);

我們建了

example1表,裡麵包含stu_id學號,course_id課程號,grade分數

example2表,裡麵包含id,stu_id學號,course_id課程號,然後建立外鍵

分別插入資料到兩個表中。

我們把example2中的stu_id和course_id稱為example2表的外鍵,example1是父表,example2是字表,兩個表形成關聯,必須字表的資料刪除後,才能刪除父表中的對應資料

現在我們來刪除example1中的一條資料

delete from example1 where stu_id=2;

會發現報錯

ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`test`.`example3`, CONSTRAINT `f_ck` FOREIGN KEY (`stu_id`, `course_id`) REFERENCES `example2` (`stu_id`, `course_id`))

因為example2中的資料關聯了example1的資料,這樣是刪不了的,達到了外鍵的作用;

 

然後我們來先刪除example2表中的資料,再刪除example1表中的資料

delete from example2 where stu_id=2;

delete from example1 where stu_id=2;

這樣就成功了;

 

事件觸發限制: 

on delete和on update , 可設參數cascade(跟隨外鍵改動), restrict(限制外表中的外鍵改動),set Null(設空值),set Default(設預設值),[預設]no action

我們來看看事件觸發限制是幹嘛的。。。

我們先刪除外鍵,然後重建立立外鍵帶上事件觸發限制

alter table example2 drop foreign key f_ck;alter table example2 add CONSTRAINT `f_ck` FOREIGN KEY (`stu_id`, `course_id`) REFERENCES `example1` (`stu_id`, `course_id`) ON DELETE CASCADE ON UPDATE CASCADE;

 

我們先查看一下資料

mysql> select * from example1;select * from example2;

+--------+-----------+-------+

| stu_id | course_id | grade |

+--------+-----------+-------+

|      1 |         1 |  98.5 |

+--------+-----------+-------+

1 row in set (0.00 sec)

+----+--------+-----------+

| id | stu_id | course_id |

+----+--------+-----------+

|  1 |      1 |         1 |

+----+--------+-----------+

1 row in set (0.00 sec)

這時example1和example2中的stu_id和course_id都是1,

再來修改example1表中的資料看看

update example1 set stu_id=3,course_id=3 where stu_id=1;

再來查看資料

mysql> select * from example1;select * from example2;

+--------+-----------+-------+

| stu_id | course_id | grade |

+--------+-----------+-------+

|      3 |         3 |  98.5 |

+--------+-----------+-------+

1 row in set (0.00 sec)

+----+--------+-----------+

| id | stu_id | course_id |

+----+--------+-----------+

|  1 |      3 |         3 |

+----+--------+-----------+

1 row in set (0.00 sec)

發現沒,example1和example2中的stu_id和course_id都變成了3

 

我們在來刪除example1表中的資料

delete from example1 where stu_id=3;

會發現可以刪除,而且example2中的資料也沒有了;

 

其實啊,外鍵就這個作用,保持資料一致性,完整性,是不讓改還是一起改,由事件觸發程序決定;

MySQL外鍵的作用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.