How to find the Primary Key, Unique Key and Index Indexes in Postgres DB
// For Index
SELECT relname FROM pg_class
WHERE oid IN
(SELECT indexrelid FROM pg_index, pg_class
WHERE pg_class.relname='emp'
AND pg_class.oid=pg_index.indrelid
AND indisunique != 't' AND indisprimary != 't');
// For unique
SELECT relname FROM pg_class
WHERE oid IN
(SELECT indexrelid FROM pg_index, pg_class
WHERE pg_class.relname='emp'
AND pg_class.oid=pg_index.indrelid
AND indisunique = 't' AND indisprimary != 't');
// For primary
SELECT relname FROM pg_class
WHERE oid IN
(SELECT indexrelid FROM pg_index, pg_class
WHERE pg_class.relname='emp'
AND pg_class.oid=pg_index.indrelid
AND indisprimary = 't');
SELECT relname FROM pg_class
WHERE oid IN
(SELECT indexrelid FROM pg_index, pg_class
WHERE pg_class.relname='emp'
AND pg_class.oid=pg_index.indrelid
AND indisunique != 't' AND indisprimary != 't');
// For unique
SELECT relname FROM pg_class
WHERE oid IN
(SELECT indexrelid FROM pg_index, pg_class
WHERE pg_class.relname='emp'
AND pg_class.oid=pg_index.indrelid
AND indisunique = 't' AND indisprimary != 't');
// For primary
SELECT relname FROM pg_class
WHERE oid IN
(SELECT indexrelid FROM pg_index, pg_class
WHERE pg_class.relname='emp'
AND pg_class.oid=pg_index.indrelid
AND indisprimary = 't');
Comments
Post a Comment