• Is it bad to rely on foreign key cascading? 外键 级联操作


    Is it bad to rely on foreign key cascading?

    I'll preface前言 this by saying that I rarely delete rows period. Generally most data you want to keep. You simply mark it as deleted so it won't be shown to users (ie to them it appears deleted). Of course it depends on the data and for some things (eg shopping cart contents) actually deleting the records when the user empties his or her cart is fine.

    I can only assume that the issue here is you may unintentionally delete records you don't actually want to delete. Referential integrity should prevent this however. So I can't really see a reason against this other than the case for being explicit.

    When/Why to use Cascading in SQL Server?

    Summary of what I've seen so far:

    • Some people don't like cascading at all.

    Cascade Delete

    • Cascade Delete may make sense when the semantics of the relationship can involve an exclusive独有的 "is part of" description. For example, an OrderLine record is part of its parent order, and OrderLines will never be shared between multiple orders. If the Order were to vanish消失, the OrderLine should as well, and a line without an Order would be a problem.
    • The canonical权威的 example for Cascade Delete is SomeObject and SomeObjectItems, where it doesn't make any sense for an items record to ever exist without a corresponding main record.
    • You should not use Cascade Delete if you are preserving history or using a "soft/logical delete" where you only set a deleted bit column to 1/true.

    Cascade Update

    • Cascade Update may make sense when you use a real key rather than a surrogate key (identity/autoincrement column) across tables.
    • The canonical example for Cascade Update is when you have a mutable foreign key, like a username that can be changed.
    • You should not use Cascade Update with keys that are Identity/autoincrement columns.
    • Cascade Update is best used in conjunction with a unique constraint.

    When To Use Cascading

    • You may want to get an extra strong confirmation back from the user before allowing an operation to cascade, but it depends on your application.
    • Cascading can get you into trouble if you set up your foreign keys wrong. But you should be okay if you do that right.
    • It's not wise to use cascading before you understand it thoroughly. However, it is a useful feature and therefore worth taking the time to understand.
  • 相关阅读:
    PHP | 运算符优先级
    Docker配置PHP+Nginx+MySQL
    Windows下Mysql主从配置
    php-fpm重启配置修改无效
    MySQL事务
    hadoop伪集群搭建
    Springboot2.x源码下载安装
    微服务——服务之间访问,用Feign请求服务接口超时如何解决?
    Lua安装
    日期——计算每月第一天和最后一天
  • 原文地址:https://www.cnblogs.com/chucklu/p/10529571.html
Copyright © 2020-2023  润新知