SQL Server:如何使服务器检查其所有检查约束?

发布于 2024-07-26 16:00:43 字数 444 浏览 9 评论 0原文

似乎企业管理器* 生成的某些脚本(或不生成,这并不重要)创建了检查约束WITH NOCHECK

现在,当任何人修改表时,SQL Server 就会陷入困境跨越失败的检查约束,并抛出错误。

我可以让 SQL 通过其所有检查约束并检查它们吗?

运行:

sp_msforeachtable 'ALTER TABLE ? CHECK CONSTRAINT all'

仅启用之前禁用的检查约束,但实际上并不检查它们。

脚注

* SQL Server 2000

It seems that some scripts generated by Enterprise Manager* (or not, it doesn't matter) created check constraints WITH NOCHECK.

Now when anyone modifies the table, SQL Server is stumbling across failed check constraints, and throwing errors.

Can i make SQL go through all its check constraints, and check them?

Running:

sp_msforeachtable 'ALTER TABLE ? CHECK CONSTRAINT all'

only enables previously disabled check constraints, it doesn't actually check them.

Footnotes

* SQL Server 2000

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

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

发布评论

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

评论(3

卖梦商人 2024-08-02 16:00:43

DBCC CHECKCONSTRAINTS WITH ALL_CONSTRAINTS 实际上不会使您的约束受到信任。 它将报告任何违反约束的行。 要真正使所有约束都可信,您可以执行以下操作:

DBCC CHECKCONSTRAINTS WITH ALL_CONSTRAINTS --This reports any data that violates constraints.

--This reports all constraints that are not trusted
SELECT OBJECT_NAME(parent_object_id) AS table_name, name, is_disabled  
FROM sys.check_constraints 
WHERE is_not_trusted = 1
UNION ALL
SELECT OBJECT_NAME(parent_object_id) AS table_name, name, is_disabled  
FROM sys.foreign_keys
WHERE is_not_trusted = 1
ORDER BY table_name

在 SQL Server 2000 中,您可以通过以下方式查找任何不受信任的约束:

--Reports all constraints that are not trusted (SQL 2000)
SELECT name, type, status,
    (status & 2048) AS IsTrusted,
    (status & 256) AS IsEnabled,
    OBJECTPROPERTY(id,'CnstIsNotTrusted') as is_not_trusted,
    OBJECTPROPERTY(id,'CnstIsDisabled') as is_disabled
FROM sysobjects 
WHERE type IN ('C', 'F') --C=Constraint, F=Foreign Key
AND OBJECTPROPERTY(id,'CnstIsNotTrusted') <> 0
AND OBJECTPROPERTY(id,'CnstIsDisabled') = 0

然后通过检查重新启用约束:

--This makes all constraints trusted
-- but first anything reported by DBCC CHECKCONSTRAINTS WITH ALL_CONSTRAINTS must be fixed.
exec sp_msforeachtable 'ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all'

注意:在最后一条语句中,WITH CHECK CHECK 不是拼写错误。 “WITH CHECK”将检查所有表数据以确保不存在违规,并使约束可信,同时检查将确保启用约束。

也可以看看:
http://sqlblog.com/blogs/tibor_karaszi /archive/2008/01/12/non-trusted-constraints.aspx

http://sqlblog.com/blogs/tibor_karaszi/archive/2008/01/12/non-trusted-constraints-and-performance.aspx

DBCC CHECKCONSTRAINTS WITH ALL_CONSTRAINTS won't actually make your constraints trusted. It will report any rows that violate the constraints. To actually make all of your constraints trusted, you can do the following:

DBCC CHECKCONSTRAINTS WITH ALL_CONSTRAINTS --This reports any data that violates constraints.

--This reports all constraints that are not trusted
SELECT OBJECT_NAME(parent_object_id) AS table_name, name, is_disabled  
FROM sys.check_constraints 
WHERE is_not_trusted = 1
UNION ALL
SELECT OBJECT_NAME(parent_object_id) AS table_name, name, is_disabled  
FROM sys.foreign_keys
WHERE is_not_trusted = 1
ORDER BY table_name

In SQL Server 2000 you can find any untrusted constraints with:

--Reports all constraints that are not trusted (SQL 2000)
SELECT name, type, status,
    (status & 2048) AS IsTrusted,
    (status & 256) AS IsEnabled,
    OBJECTPROPERTY(id,'CnstIsNotTrusted') as is_not_trusted,
    OBJECTPROPERTY(id,'CnstIsDisabled') as is_disabled
FROM sysobjects 
WHERE type IN ('C', 'F') --C=Constraint, F=Foreign Key
AND OBJECTPROPERTY(id,'CnstIsNotTrusted') <> 0
AND OBJECTPROPERTY(id,'CnstIsDisabled') = 0

Constraints are then re-reenabled with check:

--This makes all constraints trusted
-- but first anything reported by DBCC CHECKCONSTRAINTS WITH ALL_CONSTRAINTS must be fixed.
exec sp_msforeachtable 'ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all'

Note: on the last statement, the WITH CHECK CHECK is not a typo. The "WITH CHECK" will check all table data to ensure there are not violations, and will make the constraint trusted, while the check will make sure the constraints is enabled.

See also:
http://sqlblog.com/blogs/tibor_karaszi/archive/2008/01/12/non-trusted-constraints.aspx

http://sqlblog.com/blogs/tibor_karaszi/archive/2008/01/12/non-trusted-constraints-and-performance.aspx

下雨或天晴 2024-08-02 16:00:43

找到它

检查所有表的所有约束当前数据库,无论约束是否启用:

DBCC CHECKCONSTRAINTS WITH ALL_CONSTRAINTS

仅检查启用的约束:

DBCC CHECKCONSTRAINTS

Found it:

Checks all constraints on all tables in the current database, whether the constraint is enabled or not:

DBCC CHECKCONSTRAINTS WITH ALL_CONSTRAINTS

To check only enabled constraints:

DBCC CHECKCONSTRAINTS
鹿港巷口少年归 2024-08-02 16:00:43

执行此操作:

ALTER TABLE dbo.Test
      WITH CHECK CHECK CONSTRAINT CK_Test;

说明: 你能相信你的约束吗?

do this:

ALTER TABLE dbo.Test
      WITH CHECK CHECK CONSTRAINT CK_Test;

Explanation: Can you trust your constraints?

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