List all User Tables With Primary Key Constraint in a SQL Server Database

To list all user tables in a SQL Server database that have a Primary Key constraint, you can use the following query:

SELECT DB_NAME() AS Database_Name
, AS Schema_Name
, AS Table_Name
, AS Index_Name
, i.type_desc AS Index_Type
FROM sys.indexes i
INNER JOIN sys.objects o ON i.object_id = o.object_id
INNER JOIN sys.schemas sc ON o.schema_id = sc.schema_id
WHERE i.is_primary_key = 1
AND o.type = 'U'
ORDER BY, i.type

There can only be one primary key in a table, but it is possible to have a composite primary key - a primary key made from two or more columns.

If the post helped you, please share it:
Pin It

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">