DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Get Tables Without A Primary Key (PostgreSQL)
Gets all tables without a primary key
SELECT t.nspname, t.relname
FROM (
SELECT c.oid, c.relname, n.nspname
FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind = 'r' AND n.nspname IN('public') -- any other schemas in here
) AS t LEFT OUTER JOIN pg_constraint c ON c.contype = 'p' AND c.conrelid = t.oid
WHERE c.conname IS NULL





