What SQL script in Postgres to return the fields that belong to the primary key of a specific bank table?
What SQL script in Postgres to return the fields that belong to the primary key of a specific bank table?
In my searches I found several ways to do this script, I want to show here a very simple way to return this data, this to help people who are starting
select kcu.table_schema,
kcu.table_name,
tco.constraint_name,
kcu.ordinal_position as position,
kcu.column_name as key_column
from information_schema.table_constraints tco
join information_schema.key_column_usage kcu
on kcu.constraint_name = tco.constraint_name
and kcu.constraint_schema = tco.constraint_schema
and kcu.constraint_name = tco.constraint_name
where tco.constraint_type = 'PRIMARY KEY' and
kcu.table_name = 'sua_tabela'
order by kcu.table_schema,
kcu.table_name,
position;