We all know that
how to check primary key of table in SQL server using graphically(using object
explorer in SQL) but sometime you need to know how to find primary key of
specific table using SQL query. There are many ways to find it out, some of it
mention below:
Ex 1:-
SELECT column_name
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
OBJECTPROPERTY(OBJECT_ID(constraint_name), 'IsPrimaryKey') = 1
AND table_name =
'TableName'
Ex 2:-
SELECT * FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
JOIN
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu
ON
tc.CONSTRAINT_NAME = ccu.Constraint_name
WHERE
tc.CONSTRAINT_TYPE = 'Primry Key'
Ex 3(Using system stored procedure):-
EXEC sp_pkeys ‘TableName’