MySQL第三課

來源:互聯網
上載者:User

標籤:技術分享   creat   使用者表   mysql   --   刪除   sql   特定   nod   

MySQL外鍵

1.什麼是外鍵

表與表之間的特定關係。保持資料的完整性和一致性。

user使用者表

Order訂單表

1.1 刪除使用者、沒有刪除訂單,資料就不一致

1.2 Order表中插入記錄

外鍵控制:讓使用者不更新資料,或者是使用者刪除資料的時候,讓訂單同步也刪除

2.外鍵的特點:

2.1查看是否存在外鍵:

2.1.1 innodb類型

show create table 表名:

 engine--儲存引擎

2.1.2 外鍵是兩張表的約束關係。

2.1.3外鍵的名稱唯一。

3.創鍵外鍵

create table 表名(列定義 索引定義 外鍵定義[constraint 約束名稱] foreign key[外鍵欄位]

references[外鍵名稱](外鍵欄位)

[on delete{retrict|cascade|set null | no action}]

[on update{restrict|cascade|set null|no action}])

restrict:拒絕對父表的刪除或更新操作

cascade:父表的刪除或更新,自動刪除或更新子表中對應的記錄。

set null 父表刪除、更新,設定字表外鍵欄位null

no action:不作為。

 

刪除外鍵:

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.