创建外键而不检查现有数据

编程入门 行业动态 更新时间:2024-10-22 21:20:42
本文介绍了创建外键而不检查现有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是一个2部分的问题。

问题1:我试图在需要的表格上创建一个外键关闭检查现有数据创建或重新启用。我知道这是一个可视化的选项,但我正在寻找一种方法来编程。问题2:我有一个代码表和两个表A和B需要引用该代码表。我想要从关系表中引用这两个,但我想能够使用相同的列。我可以有两个外键指向同一列?

解决方案

是的,你可以在父表中有相同的列引用不同多个表中的列。

我不建议在创建时关闭检查FK。如果你现在有不好的数据,你现在需要修复它。否则,如果有人第一次编辑其中一条记录,那么FK检查将会失败。

如果您不想验证新的CHECK 或FOR $ SIGN KEY约束与现有数据,请使用WITH NOCHECK。我们做不建议这样做,除非罕见的情况。新的约束将在所有后来的数据更新中评估。 当添加约束条件时,由WITH NOCHECK抑制的的任何约束违规可能会导致将来的更新失败,如果它们更新了行且数据不符合是约束。

This is a 2 part question.

Question 1: I am trying to create a foreign key on a table where I need to turn off the "Check Existing Data on Creation or Re-Enabling". I know theres an option visually but I'm looking for a way to do it programmatically. Is there anyway to do this?

Question 2: I have a code table and two tables A and B that need to reference that code table. I want to have these both referenced from a relationship table but I want to able to use the same column. Can I have 2 foreign keys pointing to the same column?

解决方案

Yes you can have the same column inthe parent table refer to differnt columns in multiple tables.

I do not recommend turning off checking FK on creation. If you have bad data now, you need to fix it now. Otherwise the first time someone edits one of those records it will fail the FK check then.

From Books online as to why it is a bad idea to use nocheck:

If you do not want to verify new CHECK or FOREIGN KEY constraints against existing data, use WITH NOCHECK. We do not recommend doing this, except in rare cases. The new constraint will be evaluated in all later data updates. Any constraint violations that are suppressed by WITH NOCHECK when the constraint is added may cause future updates to fail if they update rows with data that does not comply with the constraint.

更多推荐

创建外键而不检查现有数据

本文发布于:2023-07-29 11:04:33,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!