patternsqlMajor
List primary keys for all tables - Postgresql
Viewed 0 times
postgresqltablesprimaryallkeysforlist
Problem
Is there a query that will do that?
I found some queries that can do this for one table, but I wasn't able to modify it so I can see:
I found some queries that can do this for one table, but I wasn't able to modify it so I can see:
tablename | column | typeSolution
This is more accurate answer:
You missed the
select tc.table_schema, tc.table_name, kc.column_name
from
information_schema.table_constraints tc,
information_schema.key_column_usage kc
where
tc.constraint_type = 'PRIMARY KEY'
and kc.table_name = tc.table_name and kc.table_schema = tc.table_schema
and kc.constraint_name = tc.constraint_name
order by 1, 2;You missed the
and kc.constraint_name = tc.constraint_name part, so it lists all constraints.Code Snippets
select tc.table_schema, tc.table_name, kc.column_name
from
information_schema.table_constraints tc,
information_schema.key_column_usage kc
where
tc.constraint_type = 'PRIMARY KEY'
and kc.table_name = tc.table_name and kc.table_schema = tc.table_schema
and kc.constraint_name = tc.constraint_name
order by 1, 2;Context
StackExchange Database Administrators Q#11032, answer score: 22
Revisions (0)
No revisions yet.