May 17, 2026, 11:40 PM
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
-- Find All SQL Server Tables in Database With a Primary Key
|
||||
SELECT
|
||||
c.name as SchemaName,
|
||||
b.name as TableName,
|
||||
a.name as PKname
|
||||
FROM sys.key_constraints a
|
||||
INNER JOIN sys.tables b ON a.parent_object_id = b.OBJECT_ID
|
||||
INNER JOIN sys.schemas c ON a.schema_id = c.schema_id
|
||||
WHERE a.type = 'PK'
|
||||
|
||||
-- Find All SQL Server Tables in Database Without a Primary Key
|
||||
|
||||
SELECT
|
||||
c.name as SchemaName,
|
||||
b.name as TableName
|
||||
FROM sys.tables b
|
||||
INNER JOIN sys.schemas c ON b.schema_id = c.schema_id
|
||||
WHERE b.type = 'U'
|
||||
AND NOT EXISTS (SELECT a.name
|
||||
FROM sys.key_constraints a
|
||||
WHERE a.parent_object_id = b.OBJECT_ID
|
||||
AND a.schema_id = c.schema_id
|
||||
AND a.type = 'PK' )
|
||||
Reference in New Issue
Block a user