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

后端 未结 6 809
甜味超标
甜味超标 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:15

    Something like this might work (untested):

    SELECT *
    FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
    WHERE TC.TABLE_NAME = 'MYTABLENAME'
    AND TC.CONSTRAINT_TYPE = 'UNIQUE'
    
    0 讨论(0)
  • 2020-12-30 04:17

    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

    0 讨论(0)
  • 2020-12-30 04:21

    Wouldn't DESCRIBE TABLE_NAME; do the trick?

    0 讨论(0)
  • 2020-12-30 04:21
    SELECT * 
    FROM mbiis.INFORMATION_SCHEMA.TABLE_CONSTRAINTS
    WHERE TABLE_NAME='TABLE_NAME' AND CONSTRAINT_TYPE='UNIQUE';
    
    0 讨论(0)
  • 2020-12-30 04:31
    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'
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题