Tuesday, February 2, 2016

Tables that do not have a primary key

--How to  find all the tables that do not have a primary key

SELECT SCHEMA_NAME(t.schema_id) AS schema_name
    ,t.name AS table_name
FROM sys.tables t
WHERE object_id NOT IN
   (
    SELECT parent_object_id
    FROM sys.key_constraints
    WHERE type_desc = 'PRIMARY_KEY_CONSTRAINT' -- or type = 'PK'
    );


GO

No comments:

Post a Comment