![](/img/trans.png)
[英]propel DOMDocument::schemaValidate(): Element 'foreign-key', attribute 'ondelete','onupdate','skipsql' The attribute is not allowing?
[英]Foreign Key advantage when not using ondelete or onupdate
如果沒有設置任何delete or update constraints
,擁有foreign key relationship
有什么好處?
假設我有一張表格,供用戶發布以轉售。
item_id item_product_id item_title other_fields
1 1 New TV for sale ...
1 1 Old TV for sale ...
1 2 Radio for sale ...
產品表將是:
product_id product_name
1 TV
2 Radio
現在,如果一個product
從取出product table
,我不希望被刪除的項目也是如此。 用戶可以看到他的商品不再存在,並且可以選擇另一個最接近的合適商品。 所以我的問題是-如果這是我的structure and logic
,那么使用foreign keys
鏈接項目和產品表有什么好處嗎?
或者,如果項目中有foreign key
輸入,我甚至可以從產品中刪除?
優點之一是您的RDMS不會讓您開始插入無效數據。 如果沒有ID為5的產品,那么您將絕對無法為其創建項目(無論是從腳本還是從mysql命令行)。 這屬於數據完整性的標題。
另外,您可能值得檢查這個問題
或者,如果項目中有外鍵輸入,我甚至可以從產品中刪除?
不,除非指定ON DELETE CASCADE
或ON DELETE SET NULL
,否則您不會。
無論您將能夠插入到items
,除非你有一個匹配的記錄products
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.