How to determine the primary key for a table in SQL Server?

后端 未结 8 1892
小蘑菇
小蘑菇 2020-12-16 07:13

What I\'d like to be able to do in SQL Server 2005 somehow is with a table name as input determine all the fields that make up the primary key. sp_columns doesn

相关标签:
8条回答
  • 2020-12-16 07:23
    select *
    from information_schema.Table_Constraints
    where Table_Name = @tableName
    

    See this MSDN Listing for Table Constraints.

    0 讨论(0)
  • 2020-12-16 07:24

    Try This:

    SELECT * 
    FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
    WHERE table_name = 'your_table_name'
      AND constraint_name LIKE 'PK%'
    
    0 讨论(0)
  • 2020-12-16 07:25

    Actually, the primary key is something else than the indexes on the table. Is also something else than the clustered index. Is a constraint, so the proper place to look for it is sys.key_constraints:

    select ic.key_ordinal, cl.name, ic.is_descending_key
    from sys.key_constraints c
    join sys.indexes i on c.parent_object_id = i.object_id
        and c.unique_index_id = i.index_id
    join sys.index_columns ic on ic.object_id = i.object_id
        and ic.index_id = i.index_id    
    join sys.columns cl on cl.object_id = i.object_id
        and ic.column_id = cl.column_id 
    where c.type = 'PK'
        and 0 = ic.is_included_column
        and i.object_id = object_id('<tablename>')
    order by ic.key_ordinal
    
    0 讨论(0)
  • 2020-12-16 07:26

    I ended up using this...

    select  cu.constraint_catalog, 
            cu.constraint_schema, 
            cu.table_name, 
            cu.constraint_name, 
            constraint_type, 
            column_name, 
            ordinal_position
    
    from    information_schema.key_column_usage cu 
    
            join information_schema.table_constraints as tc
                on tc.constraint_catalog = cu.constraint_catalog and 
                    tc.constraint_schema = cu.constraint_schema and 
                    tc.constraint_name   = cu.constraint_name and 
                    tc.table_name        = cu.table_name
    
    where   cu.table_name = 'table_name_goes_here'
    
    order by constraint_name, ordinal_position
    
    0 讨论(0)
  • 2020-12-16 07:29

    I use this in a code generator I wrote to get the primary key:

    SELECT i.name AS IndexName, 
        OBJECT_NAME(ic.OBJECT_ID) AS TableName, 
        COL_NAME(ic.OBJECT_ID,ic.column_id) AS ColumnName, 
        c.is_identity, c.user_type_id, CAST(c.max_length AS int) AS max_length, 
        CAST(c.precision AS int) AS precision, CAST(c.scale AS int) AS scale 
    FROM sys.indexes AS i 
    INNER JOIN sys.index_columns AS ic 
    INNER JOIN sys.columns AS c ON ic.object_id = c.object_id AND ic.column_id = c.column_id 
        ON i.OBJECT_ID = ic.OBJECT_ID AND i.index_id = ic.index_id 
    WHERE i.is_primary_key = 1 AND ic.OBJECT_ID = OBJECT_ID('dbo.YourTableNameHere')
    ORDER BY OBJECT_NAME(ic.OBJECT_ID), ic.key_ordinal
    
    0 讨论(0)
  • 2020-12-16 07:29

    I normally find that...

    sp_help <table>
    

    gives me all I need to know about a table (including index information).

    0 讨论(0)
提交回复
热议问题