问题描述
你能帮我构建一个查询来检索所有表中的约束、每个表中的约束计数,并为没有任何约束的表显示 NULL
.
Could you help me frame a query that retrieves the constraints in all the tables, the count of constraints in each table, and also display NULL
for tables that do NOT have any constraints.
这是我目前所拥有的:
推荐答案
您应该使用当前的 sys
目录视图(如果您使用的是 SQL Server 2005 或更高版本- sysobjects
视图已弃用,应避免使用)- 查看 关于目录视图的大量 MSDN SQL Server 在线文档.
You should use the current sys
catalog views (if you're on SQL Server 2005 or newer - the sysobjects
views are deprecated and should be avoided) - check out the extensive MSDN SQL Server Books Online documentation on catalog views here.
您可能会对很多视图感兴趣:
There are quite a few views you might be interested in:
sys.default_constraints
用于列的默认约束sys.check_constraints
用于检查列的约束sys.key_constraints
用于键约束(例如主键)sys.foreign_keys
用于外键关系
sys.default_constraints
for default constraints on columnssys.check_constraints
for check constraints on columnssys.key_constraints
for key constraints (e.g. primary keys)sys.foreign_keys
for foreign key relations
还有更多 - 看看吧!
您可以查询并加入这些视图以获取所需的信息 - 例如这将列出表、列和在它们上定义的所有默认约束:
You can query and join those views to get the info needed - e.g. this will list the tables, columns and all default constraints defined on them:
这篇关于SQL Server 2008 - 获取表约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!