Sql Server:使用“强制外键约束”有什么好处?当它设置为“NO”时?

发布于 2024-10-10 17:54:05 字数 46 浏览 3 评论 0原文

我知道 RDBMS 中“强制外键约束”的目的。但是设置为“NO”有什么好处吗?

I know the purpose of "Enforce foreign key constraint" in RDBMS. But is there any benefit when it's set to "NO" ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

方觉久 2024-10-17 17:54:05

在正常生产中,此设置不应设置为NO

但是:当您正在开发或重组数据库时,或者当您需要“清理”(清理)大量数据时,那么关闭外键约束是有意义的允许将“无效”数据加载到表中。当然,正如我所说 - 您不应该长时间关闭该设置 - 然后您应该继续清理数据,要么删除那些违反 FK 约束的行,要么更新它们的值所以它们匹配父行。

再说一遍:在“正常”生产模式下,此设置永远不应该为“NO” - 但对于特定任务,它可能有助于更轻松地完成工作。谨慎使用它,并始终尽快重新打开 FK 约束!

In normal production, this setting should never be set to NO.

But: when you're developing, or restructuring a database, or when you do e.g. a large bulk load of data that you'll need to "sanitize" (clean up), then it can make sense to turn off foreign key constraints to allow "non-valid" data to be loaded into a table. Of course, as I said - you shouldn't keep that setting turned off for a long period of time - you should then proceed to clean up the data, either delete those rows that are in violation of the FK constraint, or update their values so they match a parent row.

So again: in "normal" production mode, this setting should never be NO - but for specific tasks, it might help get the job done more easily. Use it with caution, and always turn the FK constraints back on as soon as you can!

彼岸花似海 2024-10-17 17:54:05

据我所知,日常使用中没有。当数据出现问题并且通过关系检查隐藏修复这些问题时,我已经取消执行外键一段时间了。

在批量操作期间,为了提高性能,约束检查会暂时被忽略。

Not in everyday usage, as far as I know. The times I've de-enforced foreign keys for a while are when there are problems with data and fixing them is hidered by relationship checks.

During bulk operations constraint checks are temporarily ignored in order to increase performance.

帅哥哥的热头脑 2024-10-17 17:54:05

它在数据仓库临时环境中也很有用,在这种环境中,您不想在处理/清理数据之前强制执行约束,但您仍然希望能够使用 FK 关系来了解表如何链接到每个表其他。这些关系可以在第三方工具(例如 Visio、ERWIN 等)中获取和显示,因此即使没有严格执行,它也可以是有用的元数据。

It can also be useful in a data warehouse staging environment where you don't want to enforce the constraints before you have processed / cleansed the data, but you still want to be able to use the FK relationships to understand how the tables link to each other. These relationships can be picked up and displayed in third party tools (e.g. Visio, ERWIN, etc), so it can be useful metadata even though not strictly enforced.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文