SQL - How to get the Unique Key's Column Name from Table

后端 未结 6 813
甜味超标
甜味超标 2020-12-30 03:36

I know how to get the columns from a table using the following SQL statement:

SELECT COLUMN_NAME 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE (TABLE_NAME = \'MYTA         


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-30 04:34

    Something like this:

    Select col.name From 
    sys.objects obj 
    Join sys.columns col on col.[object_id] = obj.[object_id]
    Join sys.index_columns idx_cols on idx_cols.[column_id] = col.[column_id] and idx_cols.[object_id] = col.[object_id]
    Join sys.indexes idx on idx_cols.[index_id] = idx.[index_id] and idx.[object_id] = col.[object_id]
    where obj.name = 'MYTABLENAME'
    and idx.is_unique = 1
    

提交回复
热议问题