如何确定SQL Server中表的主键?

时间:2009-08-28 21:05:18

标签: sql sql-server sql-server-2005 primary-key

我希望能够在SQL Server 2005中以某种方式使用表名作为输入来确定构成主键的所有字段。 sp_columns似乎没有此字段。关于在哪里看的任何想法?

8 个答案:

答案 0 :(得分:7)

我在我编写的代码生成器中使用它来获取主键:

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

答案 1 :(得分:5)

实际上,主键不是表上的索引。除了聚集索引之外还有别的东西。是约束,因此查找它的正确位置是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

答案 2 :(得分:4)

- ANSI SQL兼容,从SQL70开始使用:

select kcu.TABLE_SCHEMA, kcu.TABLE_NAME, kcu.CONSTRAINT_NAME, tc.CONSTRAINT_TYPE, kcu.COLUMN_NAME, kcu.ORDINAL_POSITION
  from INFORMATION_SCHEMA.TABLE_CONSTRAINTS as tc
  join INFORMATION_SCHEMA.KEY_COLUMN_USAGE as kcu
    on kcu.CONSTRAINT_SCHEMA = tc.CONSTRAINT_SCHEMA
   and kcu.CONSTRAINT_NAME = tc.CONSTRAINT_NAME
   and kcu.TABLE_SCHEMA = tc.TABLE_SCHEMA
   and kcu.TABLE_NAME = tc.TABLE_NAME
 where tc.CONSTRAINT_TYPE in ( 'PRIMARY KEY', 'UNIQUE' )
 order by kcu.TABLE_SCHEMA, kcu.TABLE_NAME, tc.CONSTRAINT_TYPE, kcu.CONSTRAINT_NAME, kcu.ORDINAL_POSITION;

- 特定于SQL Server 2005:

select s.name as TABLE_SCHEMA, t.name as TABLE_NAME
     , k.name as CONSTRAINT_NAME, k.type_desc as CONSTRAINT_TYPE
     , c.name as COLUMN_NAME, ic.key_ordinal AS ORDINAL_POSITION
  from sys.key_constraints as k
  join sys.tables as t
    on t.object_id = k.parent_object_id
  join sys.schemas as s
    on s.schema_id = t.schema_id
  join sys.index_columns as ic
    on ic.object_id = t.object_id
   and ic.index_id = k.unique_index_id
  join sys.columns as c
    on c.object_id = t.object_id
   and c.column_id = ic.column_id
 order by TABLE_SCHEMA, TABLE_NAME, CONSTRAINT_TYPE, CONSTRAINT_NAME, ORDINAL_POSITION;

答案 3 :(得分:2)

试试这个:

SELECT * 
FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
WHERE table_name = 'your_table_name'
  AND constraint_name LIKE 'PK%'

答案 4 :(得分:2)

select *
from information_schema.Table_Constraints
where Table_Name = @tableName

请参阅此MSDN Listing for Table Constraints

答案 5 :(得分:1)

我通常会发现......

sp_help <table>

给了我关于表格的所有知识(包括索引信息)。

答案 6 :(得分:0)

在SQL2005中,这会返回一个命名主键的行,然后在“index_keys”下给出列的列表

sp_help myTable

答案 7 :(得分:0)

我最终使用了这个...

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