18

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 AS Constraint_Name,
KCU.COLUMN_NAME AS COLUMN_NAME
FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE 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 = 'PRIMARY KEY'
ORDER BY
KCU.TABLE_SCHEMA, KCU.TABLE_NAME, KCU.CONSTRAINT_NAME
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Sam P
  • 515
  • 4
  • 8
  • 23

5 Answers5

46
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

Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
Kapil
  • 504
  • 3
  • 4
  • Thank you for this. I have a LOT of tables and needed the keys for all tables to use in a new table, and this was the code I needed. Thank you. – Wayne Barron Mar 13 '23 at 01:53
12

The following syntax give you all constraints in database in use.

select * from sys.key_constraints;
Ashraf Sada
  • 4,527
  • 2
  • 44
  • 48
  • 7
    To add to a response from @Ashraf, you can use `select OBJECT_NAME(parent_object_id), * from sys.key_constraints where type = 'PK'`. This will give you the table name and their primary key names for the tables that have a primary key. – nam Oct 26 '15 at 16:22
9

If you want the data type information as well:

SELECT 
    so.name 'Table Name',
    c.name 'Column Name',
    t.Name 'Data type',
    c.max_length 'Max Length',
    c.precision ,
    c.scale ,
    c.is_nullable,
    ISNULL(i.is_primary_key, 0) 'Primary Key'
FROM    
    sys.columns c
INNER JOIN 
    sys.types t ON c.user_type_id = t.user_type_id
LEFT OUTER JOIN 
    sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id
LEFT OUTER JOIN 
    sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id
INNER JOIN 
    sysobjects so ON c.object_id = so.id
WHERE
    i.is_primary_key = 1 and 
    so.xtype = 'U'
Order By 'Table Name', 'Column Name'
Jmoney38
  • 3,096
  • 2
  • 25
  • 26
  • 1
    Brilliant! I spent an hour trying to find an answer which would allow me to find all of the PKs which are NOT int – Kevon Dec 30 '19 at 19:32
6

look on link

EXEC sp_pkeys '<tablename>'
EXEC sp_helpconstraint '<tablename>'

sp_pkeys will return a row for each column that participates in the primary key for . The columns you are likely most interested in are COLUMN_NAME and PK_NAME.

sp_helpconstraint will list all constraints for , including foreign keys that reference the table. In the first recordset, there will only be a column called Object Name (kind of useless, since that's what you passed in). In the second resultset, there will be the following columns: constraint_type, constraint_name, and constraint_keys.

Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
0

If you are doing this from java you can also use the getPrimaryKeys method in the databasemetadata object. Perhaps other languages have similar ways to do it.

dan b
  • 1,172
  • 8
  • 20