Over a million developers have joined DZone.

Get Tables Without A Primary Key (PostgreSQL)

DZone's Guide to

Get Tables Without A Primary Key (PostgreSQL)

· ·
Free Resource
Gets all tables without a primary key

SELECT t.nspname, t.relname
	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

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}