【发布时间】:2014-07-09 20:41:49
【问题描述】:
如果我没有设置任何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 table 中删除product,我也不希望这些项目也被删除。用户可以看到他的项目的产品不再存在,并且可以选择另一个最接近的合适的产品。所以我的问题是 - 如果这是 structure and logic 我有,通过使用 foreign keys 链接项目和产品表有什么好处吗?
或者,如果项目中有 foreign key 条目,我什至可以从产品中删除吗?
【问题讨论】:
-
唯一的关键是在表之间建立一个简单的关系。除非你告诉它,否则影响一个不会影响另一个。
标签: php mysql sql foreign-keys rdbms