Get a List of all Primary Keys in a Database

前端 未结 5 2057
囚心锁ツ
囚心锁ツ 2021-02-02 00:21

Is this the best way to - Get a List of all Primary Keys in a Database - or is there something better?

SELECT
KCU.TABLE_NAME AS Table_Name,
KCU.CONSTRAINT_NAME A         


        
5条回答
  •  没有蜡笔的小新
    2021-02-02 00:23

    USE databasename; 
    
    GO
    
    SELECT i.name AS IndexName, OBJECT_NAME(ic.OBJECT_ID) AS TableName, 
           COL_NAME(ic.OBJECT_ID,ic.column_id) AS ColumnName
    FROM sys.indexes AS i
    INNER JOIN sys.index_columns AS ic
    ON i.OBJECT_ID = ic.OBJECT_ID
    AND i.index_id = ic.index_id
    WHERE i.is_primary_key = 1
    

    This query will extract the all primary key constraints from the database... u just need to execute this query and type the database name in first line

提交回复
热议问题