如何查找哪些索引和约束包含特定列?

时间:2013-10-04 08:16:16

标签: sql sql-server sql-server-2008 tsql

我正在计划数据库更改,并且我有一个包含在进程中的列列表。我可以列出包含特定列的所有索引吗?

修改

到目前为止,我(从答案中得出):

declare @TableName nvarchar(128), @FieldName nvarchar(128)
select  @TableName= N'<<Table Name>>', @FieldName =N'<<Field Name>>'
(SELECT distinct systab.name AS TABLE_NAME,sysind.name AS INDEX_NAME, 'index' 
FROM sys.indexes sysind 
INNER JOIN sys.index_columns sysind_col 
    ON  sysind.object_id = sysind_col.object_id and sysind.index_id = sysind_col.index_id 
INNER JOIN sys.columns sys_col 
    ON sysind_col.object_id = sys_col.object_id and sysind_col.column_id = sys_col.column_id 
INNER JOIN sys.tables systab
    ON sysind.object_id = systab.object_id

WHERE systab.is_ms_shipped = 0 and sysind.is_primary_key=0 and sys_col.name  =@FieldName and systab.name=@TableName

union
select t.name TABLE_NAME,o.name, 'Default' OBJ_TYPE
 from sys.objects o 
inner join sys.columns c on o.object_id  = c.default_object_id
inner join sys.objects t on c.object_id  = t.object_id 
where o.type  in ('D') and c.name  =@FieldName and t.name=@TableName
union

SELECT u.TABLE_NAME,u.CONSTRAINT_NAME,  'Constraint' OBJ_TYPE  
FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE u 

where u.COLUMN_NAME  = @FieldName and u.TABLE_NAME = @TableName
) order by 1

但我对sys.和'INFORMATION_SCHEMA'的组合感到不满意。 可以避免吗?

4 个答案:

答案 0 :(得分:3)

SELECT * FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE

USE TABLE_NAME IN WHERE如果你知道表上的约束。

如果您知道列名,请使用column_name。

答案 1 :(得分:3)

---Using sp_helpindex and your TableName
exec sp_helpindex YourTableName

---Using sys.tables with your TableName and ColumnName
select distinct c.name, i.name, i.type_desc,...
from sys.indexes i
        join sys.index_columns ic on i.index_id = ic.index_id 
        join sys.columns c on ic.column_id = c.column_id
where i.object_id = OBJECT_ID(N'YourTableName') and c.name = 'YourColumnName'

编辑:根据评论,您还可以在不使用distinct的情况下加入object_Ids

select c.name, i.name, i.type_desc
from sys.indexes i
        join sys.index_columns ic on i.index_id = ic.index_id and i.object_id = ic.object_id
        join sys.columns c on ic.column_id = c.column_id and ic.object_id = c.object_id
where i.object_id = OBJECT_ID(N'YourTableName') and c.name = 'YourColumnName'

答案 2 :(得分:2)

要获取有关包含特定列的所有索引的信息,可以使用以下两个目录视图:

sys.indexes , sys.index_columns

查询:

SELECT 
     sysind.name AS INDEX_NAME
    ,sysind.index_id AS  INDEX_ID
    ,sys_col.name AS COLUMN_NAME
    ,systab.name AS TABLE_NAME
FROM sys.indexes sysind 
INNER JOIN sys.index_columns sysind_col 
    ON  sysind.object_id = sysind_col.object_id and sysind.index_id = sysind_col.index_id 
INNER JOIN sys.columns sys_col 
    ON sysind_col.object_id = sys_col.object_id and sysind_col.column_id = sys_col.column_id 
INNER JOIN sys.tables systab
    ON sysind.object_id = systab.object_id 
WHERE (1=1) 
      AND systab.is_ms_shipped = 0 
      AND sys_col.name  IN(specific column list for which indexes are to be queried)
ORDER BY 
    systab.name,sys_col.name, sysind.name,sysind.index_id

希望这有帮助!

答案 3 :(得分:0)

如果您可以访问数据库上的sys模式,则可以查询sys.indexes以获取索引ID,并将其与函数index_col一起使用以获取列。 index_col的最后一个参数是索引中列的索引(即,如果索引中有3列,则必须在最后一个参数中使用1,2,3来调用index_col 3次)

select index_id from sys.indexes
 where object_id = object_id(@objectname)

select index_col(@objectname, @indexid, 1)

这应该给你约束,它们只是特殊的索引。