列出索引和约束


10

我正在查看我继承的应用程序的SQL Server数据库。我已经有大约10年没有研究SQL Server了,所以请多多包涵。

我正在查看的数据库表有一个bigint NOT NULL名为的列id,但是,当我检查约束时,没有看到任何约束,所有数据库表也是如此。

我是否假设这些表上没有主键并且没有索引(聚集或非聚集),对吗?

我运行了以下查询,结果似乎证实了我的怀疑:

//**returns 0**
select count(*) from INFORMATION_SCHEMA.TABLE_CONSTRAINTS;

//**returns no rows**
select * from sys.indexes
where object_id = (select object_id from sys.objects where name = 'NAME-OF-TABLE');

//**returns all tables in database**
SELECT name
FROM sys.tables 
WHERE OBJECTPROPERTY(object_id,'IsIndexed') = 0;

Answers:


9

这两个查询可能会对您有所帮助。第一个将列出数据库中所有表以及这些表上的索引。如果表未出现在列表中,则表上没有定义任何索引。这些查询假定使用SQL Server 2005版或更高版本。

SELECT 
    IndexName = QUOTENAME(I.name), 
    TableName =
        QUOTENAME(SCHEMA_NAME(T.[schema_id])) + 
        N'.' + QUOTENAME(T.name), 
    IsPrimaryKey = I.is_primary_key
FROM sys.indexes AS I
INNER JOIN sys.tables AS T
    ON I.[object_id] = T.[object_id]
WHERE
    I.type_desc <> N'HEAP'
ORDER BY 
    TableName ASC, 
    IndexName ASC;

第二个查询将为每个表报告标识列,如果数据库中的每个表都有该列。

SELECT
    TableName =
        QUOTENAME(SCHEMA_NAME(T.[schema_id])) + 
        N'.' + QUOTENAME(T.name), 
    IdentityColumn = COALESCE(QUOTENAME(C.name), N'No identity column')
FROM sys.tables AS T
LEFT OUTER JOIN sys.columns AS C
    ON T.[object_id] = C.[object_id]
    AND C.is_identity = 1
ORDER BY
    TableName ASC;

要将查询限制为特定表,请添加WHERE类似于以下内容的子句:

WHERE T.name = N'NAME-OF-TABLE'

2

不,有什么不对。

sys.indexes即使表没有索引,检查也应返回一行。堆仍然有一个纪录sys.indexestype_desc“堆”和type0。

我认为您可能需要确保自正确的数据库上下文以来,OBJECT_ID()并且sys.objects特定于数据库。

尝试这个:

USE MyDatabase

SELECT *
FROM sys.indexes
WHERE object_id = OBJECT_ID('schema.MyTableName')

1

我不确定您是否对所有约束都感兴趣,但是INFORMATION_SCHEMA.TABLE_CONSTRAINTS似乎没有返回默认约束-TABLE_CONSTRAINTS(Transact-SQL)

检查,唯一,主键,外键

此查询将对sys.objects DMV进行简单计数:

select COUNT(*)
from sys.objects o
where o.type_desc like '%CONSTRAINT%';

如果您有兴趣列出这些表,则可以运行以下命令:

select distinct
   o.object_id
 , QUOTENAME(s.name) + '.' + QUOTENAME(o.name) as [object_name]
 , o.type_desc
 , case when dc.parent_object_id is null then 'No' else 'Yes' end as has_default_constraint
 , case when cc.parent_object_id is null then 'No' else 'Yes' end as has_check_constraint
 , case when fk.parent_object_id is null then 'No' else 'Yes' end as has_foreing_key
 , case when kc.parent_object_id is null then 'No' else 'Yes' end as has_primary_key
from sys.objects o
inner join sys.schemas s on s.schema_id = o.schema_id
left outer join sys.default_constraints dc on dc.parent_object_id = o.object_id and dc.schema_id = o.schema_id
left outer join sys.check_constraints cc on cc.parent_object_id = o.object_id and cc.schema_id = o.schema_id
left outer join sys.foreign_keys fk on fk.parent_object_id = o.object_id and fk.schema_id = o.schema_id
left outer join sys.key_constraints kc on kc.parent_object_id = o.object_id and kc.schema_id = o.schema_id
where o.is_ms_shipped = 0
  and o.type = 'U'
order by [object_name];

这应该为您提供有关索引的信息:

select o.name
 , i.*
from sys.objects o
inner join sys.indexes i on i.object_id = o.object_id
where o.is_ms_shipped = 0
  and i.object_id > 100
  and i.index_id > 0
order by o.name
   , i.index_id;
  • Index_Id = 0-HEAP(不会显示)
  • Index_Id = 1-聚集
  • Index_Id> 1-已禁用

你能解释一下为什么object_id > 100吗?
brianc

@ bluevoodoo1-不是强制性的,但系统对象<100,但是由于使用o.is_ms_shipped = 0,因此无论如何都不应包含它们。只是安全起见,仅此
而已
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.