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.

Is it bad to rely on foreign key cascading? 外键 级联操作的更多相关文章

  1. 数据库六大约束用法:主键(primary key)、外键(foreign key)、非空(not null)、默认(default)、检查(check)、唯一(unique)

    1. 数据库有六大约束 主键(primary key) 外键(foreign key):被参照的键必须有唯一约束或是主键 非空(not null) 默认(default) 检查(check):orac ...

  2. SQLServer之修改FOREIGN KEY约束

    使用SSMS数据库管理工具修改FOREIGN KEY约束 1.连接数据库,选择数据表->右键点击->选择设计(或者展开键,选择要修改的外键,右键点击,选择修改,后面修改步骤相同). 2.在 ...

  3. python 存储引擎 mysql(库,表, 行) 单表多表操作 (foreign key) sql_mode pymysql模块讲解

    ##################总结############### mysql 常用数据类型 整型:tinyint  int(42亿条左右)  bigint 小数:float double dec ...

  4. 更改具有Foreign key约束的表

    1.Foreign key 说明: foreign key(外键) 建立起了表与表之间的约束关系,让表与表之间的数据更具有完整性和关联性.设想,有两张表A.B,A表中保存了许多电脑制造商的信息,比如联 ...

  5. {MySQL完整性约束}一 介绍 二 not null与default 三 unique 四 primary key 五 auto_increment 六 foreign key 七 作业

    MySQL完整性约束 阅读目录 一 介绍 二 not null与default 三 unique 四 primary key 五 auto_increment 六 foreign key 七 作业 一 ...

  6. MySQL完整性约束foreign key与表操作。

    一  MySQL中表的完整性约束: 我们首先知道约束条件跟类型的宽度一样,都是可选的,也就是说,我们在创建表的时候可以不指定,但是为了创建的表更加的完整,我们一般会加一些约束条件,name下面我们讲一 ...

  7. Constraint6:更新外键约束(Foreign Key Constraint)的引用列

    在SQL Server中,表之间存在引用关系,引用关系通过创建外键约束(Foreign Key Constraint)实现.如果一个Table中的column被其他Table引用,那么该表是参考表,或 ...

  8. MySQL主从复制中断,报“Error on master: message (format)='Cannot delete or update a parent row: a foreign key constraint fails' error code=1217” 错误

    前几天,发现从库挂了,具体报错信息如下: 分析思路 1. 因为我采用的是选择性复制,只针对以下几个库进行复制: card,upay,deal,monitor,collect.所以,不太可能出现对于sa ...

  9. SQL Server 2008 R2——TRUNCATE TABLE 无法截断表 该表正由 FOREIGN KEY 约束引用

    =================================版权声明================================= 版权声明:原创文章 禁止转载  请通过右侧公告中的“联系邮 ...

随机推荐

  1. PyQt5标准对话框

    很全的Qt的标准对话框,包含QInputDialog.QColorDialog.QFontDialog.QMessageBox.QOpenFileDialog... 全部是由官网的C++版本,转换成P ...

  2. Python描述器引导(转)

    原文:http://pyzh.readthedocs.io/en/latest/Descriptor-HOW-TO-Guide.html 1. Python描述器引导(翻译) 作者: Raymond ...

  3. hive-site.xml配置

    <?xml version="1.0" encoding="UTF-8" standalone="no"?><?xml-s ...

  4. MySQL字符集设置及字符转换(latin1转utf8)

    MySQL字符集设置及字符转换(latin1转utf8) http://blog.chinaunix.net/uid-25266990-id-3344584.html  MySQL字符集设置及字符转换 ...

  5. oracle 之创建用户,表空间,授权,修改用户密码

    1.创建表空间 create tablespace ilinkcargoagent logging datafile 'D:\app\Administrator\oradata\ilinkcargoa ...

  6. col-md-1

    .col-md-12 {    width: 100%;  }  .col-md-11 {    width: 91.66666666666666%;  }  .col-md-10 {    widt ...

  7. Groovy介绍

    关于 Groovy 这一节将学习 Groovy 的基础知识:它是什么,它与 Java 语言和 JVM 的关系,以及编写 Groovy 代码的一些要点. 一.什么是 Groovy? Groovy 是 J ...

  8. vue项目优化

    cross-env 包环境 静态文件分离 require 是置顶的 双斜杠   //baidu.com可以是http也可以是https require.ensure打包到不同的文件中 项目文件路径规范 ...

  9. 怎么把<li>标签里的内容付给文本框

    如果你不想使用jq,那就可以这样先写一个js函数,function val(a){alert(a);} 然后在li标签上添加点击事件,调用这个函数并将当前li里的文本当做参数一起发送给js函数< ...

  10. 第六章 数据库设计之ER模型

    在ER图中实体用方框表示 实体其实就相当于一个二维表,实体实例就相当于二维表中的一行 属性在二维表中用椭圆表示,属性就是描述实体特征的数据项 概念:键(也被成为候选键):1,属性集合K上的行唯一   ...