更改表 [dbo].[MyTable] 检查约束 [FK_MyTable_SomeCol] [英] ALTER TABLE [dbo].[MyTable] CHECK CONSTRAINT [FK_MyTable_SomeCol]
问题描述
如果我用外键编写一个表,它看起来像这样:
If I script a table with a foreign key, it looks like this:
GO
ALTER TABLE [dbo].[MyTable] WITH CHECK ADD CONSTRAINT [FK_MyTable_SomeCol] FOREIGN KEY([SomeCol])
REFERENCES [dbo].[MyOtherTable] ([SomeCol])
GO
ALTER TABLE [dbo].[MyTable] CHECK CONSTRAINT [FK_MyTable_SomeCol]
GO
(ALTER TABLE [dbo].[MyTable] CHECK CONSTRAINT [FK_MyTable_SomeCol]
) 的第二部分是什么?
What is the second part for (ALTER TABLE [dbo].[MyTable] CHECK CONSTRAINT [FK_MyTable_SomeCol]
)?
推荐答案
这是约束脚本方式的产物 - 尽管没有必要指定这些选项(因为它们是新约束的默认值),同样生成器还可以以完全相同的方式生成 NOCHECK
选项.
It's an artifact of the way that the constraint is scripted - although it's unnecessary to specify these options (since they're the defaults for new constraints), the same generator can also generate NOCHECK
options in exactly the same manner.
文档 ALTER TABLE
表明CHECK
/NOCHECK
的两种不同用途:
Documentation for ALTER TABLE
indicates two distinct uses of CHECK
/NOCHECK
:
<代码>带检查 |没有检查
指定表中的数据是否根据新添加或重新启用的FOREIGN KEY
或CHECK
约束进行验证.如果未指定,则假定 WITH CHECK
用于新约束,并假定 WITH NOCHECK
用于重新启用的约束.
Specifies whether the data in the table is or is not validated against a newly added or re-enabled FOREIGN KEY
or CHECK
constraint. If not specified, WITH CHECK
is assumed for new constraints, and WITH NOCHECK
is assumed for re-enabled constraints.
还有:
<代码>{ 检查 |NOCHECK } 约束
指定启用或禁用constraint_name.
Specifies that constraint_name is enabled or disabled.
因此,一个选项是检查表的当前内容",另一个选项是在添加新数据时进行验证".
So one option is saying "check the current contents of the table", the other is saying "Validate new data as it is added".
这篇关于更改表 [dbo].[MyTable] 检查约束 [FK_MyTable_SomeCol]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!