dtqf81594 2014-07-09 20:41
浏览 41
已采纳

不使用ondelete或onupdate时的外键优势

What is the advantage of having a foreign key relationship if I am not having any delete or update constraints set?

Suppose I have a table for items that users can post for reselling.

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        ...

And the product table would be:

product_id   product_name
1            TV
2            Radio

Now if a product is removed from the product table, I would not want the items to be deleted as well. The user can see that his item's product exists no longer and can select another closest appropriate product. So my question is - if this is the structure and logic I have, is there any advantage I get by linking the items and product tables using foreign keys?

Or, will I even be able to delete from products if there is a foreign key entry in items?

  • 写回答

2条回答 默认 最新

  • douxidang9092 2014-07-09 20:45
    关注

    One advantage is that your RDMS won't let you insert invalid data to begin with. If there is no product with ID 5, then you will be absolutely unable to create an item for it (whether from a script or from the mysql command line). This goes under the heading of data integrity.

    Also, it might be worthwhile for you to checkout this question

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 matlab解优化问题代码
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?