![](/img/trans.png)
[英]Create mySQL junction table from single table with parent-child relationship
[英]how to create foreign keys for parent-child relations on a single table?
例:
id | name | parent_id |
可以想象,parent_id必須指向同一表中的有效ID。
我想-禁止刪除有任何子級的父行-禁止父級id的子級編輯-如果出於某種原因父級id會改變,則必須在字段parent_id上更新所有子級
如何創建外鍵?
認為應該像下面這樣
create contraint `my_foreign_key`
FOREIGN KEY (parent_id)
REFERENCES my_table(id)
ON DELETE RESTRICT
ON UPDATE CASCADE
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.