tags:

views:

56

answers:

5

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 = 'MYTABLENAME')

But how do I just return what the UNIQUE Key's Column name?

+2  A: 

Something like this might work (untested):

SELECT *
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
WHERE TC.TABLE_NAME = 'MYTABLENAME'
AND TC.CONSTRAINT_TYPE = 'UNIQUE'
ongle
This does not retrieve the column name as requested.
Christian Hayter
Sorry, not on my dev machine. But it's where to start. :)
ongle
+5  A: 
select CCU.CONSTRAINT_NAME, CCU.COLUMN_NAME
from INFORMATION_SCHEMA.TABLE_CONSTRAINTS as TC
inner join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE as CCU
    on TC.CONSTRAINT_CATALOG = CCU.CONSTRAINT_CATALOG
    and TC.CONSTRAINT_SCHEMA = CCU.CONSTRAINT_SCHEMA
    and TC.CONSTRAINT_NAME = CCU.CONSTRAINT_NAME
where TC.CONSTRAINT_CATALOG = 'MyCatalogName'
and TC.CONSTRAINT_SCHEMA = 'MySchemaName'
and TC.TABLE_NAME = 'MyTableName'
and TC.CONSTRAINT_TYPE = 'UNIQUE'

Bear in mind that a table may have multiple unique constraints, each containing multiple columns. You will need to apply some additional logic to select the right one.

UPDATE - based on other comments...

The above query will find all UNIQUE key constraints. However, it will not find PRIMARY KEY constraints, or UNIQUE indexes that were created outside a UNIQUE key constraint.

To find the primary key, replace the last line with:

and TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
Christian Hayter
A: 

Wouldn't DESCRIBE TABLE_NAME; do the trick?

Brian Hooper
Depends on the database.
Donnie
+1  A: 

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
DaveShaw
A: 

The two that I found to work are the following, the 2nd one was from the original poster but without the TC.CONSTRAINT_TYPE = 'UNIQUE'. That condition wasn't working

SELECT     col.name
FROM         sys.objects AS obj INNER JOIN
                      sys.columns AS col ON col.object_id = obj.object_id INNER JOIN
                      sys.index_columns AS idx_cols ON idx_cols.column_id = col.column_id AND idx_cols.object_id = col.object_id INNER JOIN
                      sys.indexes AS idx ON idx_cols.index_id = idx.index_id AND idx.object_id = col.object_id
WHERE     (obj.name = 'pluginUsers') AND (idx.is_unique = 1)

and also

SELECT     CCU.CONSTRAINT_NAME, CCU.COLUMN_NAME
FROM         INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC INNER JOIN
                      INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS CCU ON TC.CONSTRAINT_CATALOG = CCU.CONSTRAINT_CATALOG AND 
                      TC.CONSTRAINT_SCHEMA = CCU.CONSTRAINT_SCHEMA AND TC.CONSTRAINT_NAME = CCU.CONSTRAINT_NAME
WHERE     (TC.TABLE_NAME = 'pluginUsers')

Thank you all for your posts

eqiz