SQL Server - List tables without a primary key
How do I list all tables WITHOUT a primary key?
Run the following SQL code:
SELECT T.TABLE_NAME AS 'Tables without PKs'
FROM INFORMATION_SCHEMA.TABLES AS T
WHERE NOT EXISTS
(SELECT *
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC
WHERE CONSTRAINT_TYPE = 'PRIMARY KEY'
AND T.TABLE_NAME = TC.TABLE_NAME)
AND T.TABLE_TYPE = 'BASE TABLE'
Another script used as part of my performance tuning, nice and simple and making use of the INFORMATION_SCHEMA views rather than the sys tables