Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find which indexes and constraints contain a specific column?

I am planing a database change and I have a list of columns included in process. Can I list all indexes in which is a specific column included?

Edit

So far I have (combined from answers):

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

But I am not too happy with combining of sys. and 'INFORMATION_SCHEMA.' Can it be avoided?

like image 623
IvanH Avatar asked Oct 04 '13 08:10

IvanH


People also ask

How do you check constraints on a column?

The CHECK constraint is used to limit the value range that can be placed in a column. If you define a CHECK constraint on a column it will allow only certain values for this column. If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.

How do I find Constraints in a column in SQL?

Use the view table_constraints in the information_schema schema. The column table_name gives you the name of the table in which the constraint is defined, and the column constraint_name contains the name of the constraint.


3 Answers

---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'

EDIT: As per comment, you can also join object_Ids without using distinct

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'
like image 99
Kaf Avatar answered Oct 06 '22 19:10

Kaf


SELECT * FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE

USE TABLE_NAME IN WHERE if you what to know the constraint on a table.

and use column_name if you know the column name.

like image 41
Ashutosh Arya Avatar answered Oct 06 '22 19:10

Ashutosh Arya


To get information regarding all indexes in which is a specific column is included following two catalog views can be used:

sys.indexes , sys.index_columns

Query:

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

Hope this helps!

like image 23
Deepshikha Avatar answered Oct 06 '22 19:10

Deepshikha