How to list all constraints of all tables in PostgreSQL? - postgresql

How to list all constraints of all tables in PostgreSQL?
I need to search the unique constraint created a long time ago, the database has so many (600) tables. It's hard to find. Is there any query to list all constraints of all tables

You must refer to pg_constraint.
If you need to list just the unique constraint filter on contype, like this:
select * from pg_catalog.pg_constraint pc
where contype = 'u'
Documentation here: https://www.postgresql.org/docs/current/catalog-pg-constraint.html

The below query will List all the constraints of a table in the PostgreSQL database.
You can try below:-
SELECT con.*
FROM pg_catalog.pg_constraint con
INNER JOIN pg_catalog.pg_class rel ON rel.oid = con.conrelid
INNER JOIN pg_catalog.pg_namespace nsp ON nsp.oid = connamespace
WHERE nsp.nspname = '<schema name>'
AND rel.relname = '<table name>';
Replace with the name of your table & with the name of your schema.
It can be viewed in PSQL as using below:-
d+ <SCHEMA_NAME.TABLE_NAME>

Related

Select comments on all objects of a table and object informations

I have this query that can select all comments on objects of tables (+ the commet on the table itself):
SELECT c.table_schema, c.table_name,pgd.description, pgd.objsubid
FROM pg_catalog.pg_statio_all_tables as st
inner join pg_catalog.pg_description pgd on (pgd.objoid=st.relid)
inner join information_schema.tables c on (c.table_schema=st.schemaname and c.table_name=st.relname);
But this only shows the subid of the objects. Now I want to be able to know what object exactly this is (column, primary key, foreign key, ...) and what the name is.
How can I do this?
If you join with information_schema.tables, you will get only information about tables, views and foreign tables. These can be disambiguated by adding the table_type column from information_schema.tables.
To get comments about all kinds of objects, you'd have to UNION ALL queries for each object type.
For example, to get comments on functions and procedures, you'll have to join with pg_proc:
SELECT f.proname, d.description
FROM pg_description AS d
JOIN pg_proc AS f ON f.oid = d.objoid
WHERE d.classoid = 'pg_proc'::regclass;

Get all the column names having a Foreign Key contraint

I am using the PostgreSQL. I want to write a query that returns all the column names having foreign key constraint and also the name of the table these columns they refer to.
As far as I can see, the information_schema views don't give you the column names, so you'll have to use the catalog:
SELECT c.conrelid::regclass AS source_table,
a.attname AS column_name,
k.n AS position,
c.confrelid::regclass AS referenced_table
FROM pg_constraint AS c
CROSS JOIN LATERAL unnest(c.conkey) WITH ORDINALITY AS k(attnum, n)
JOIN pg_attribute AS a
ON k.attnum = a.attnum AND c.conrelid = a.attrelid
WHERE c.contype = 'f'
ORDER BY c.conrelid::regclass::text, k.n;
To get the data for only a specific table, add the following to the WHERE condition:
AND c.conrelid = 'mytable'::regclass

How to get the list of all constraints, their tables and their columns in postgres?

I want to get the list of all constraints, their tables and their columns, something like
constraint | table | columns
------------------------------|-------|---------------
table1_colum1_colum2_key table1 {colum1, colum2}
How can this be done?
The constraints can be queried via pg_constraint. Tables are included in pg_class and columns in pg_attributes. Schemas are in pg_namespace.
Something like the following may work for you.
SELECT con.conname "constraint",
concat(nsp.nspname, '.', rel.relname) "table",
(SELECT array_agg(att.attname)
FROM pg_attribute att
INNER JOIN unnest(con.conkey) unnest(conkey)
ON unnest.conkey = att.attnum
WHERE att.attrelid = con.conrelid) "columns"
FROM pg_constraint con
INNER JOIN pg_class rel
ON rel.oid = con.conrelid
INNER JOIN pg_namespace nsp
ON nsp.oid = rel.relnamespace;

postgresql drop table

I have two tables (tbl and tbl_new) that both use the same sequence (tbl_id_seq). I'd like to drop one of those tables. On tbl, I've removed the modifier "not null default nextval('tbl_id_seq'::regclass)" but that modifier remains on tbl_new. I'm getting the following error:
ERROR: cannot drop table tbl because other objects depend on it
DETAIL: default for table tbl_new column id depends on sequence tbl_id_seq
After reviewing http://www.postgresql.org/docs/9.1/static/sql-droptable.html
It looks like there is only CASCADE and RESTRICT as options.
You need to decouple the sequence and the table it "belongs" to:
ALTER SEQUENCE "tbl_id_seq" OWNED BY NONE;
I suppose it was created automatically (and "bound") by defining the tbl_id field of tbl as SERIAL.
To find sequences and all tables that depend on them via column default:
SELECT sn.nspname || '.' || s.relname AS seq
,tn.nspname || '.' || t.relname AS tbl
FROM pg_class s
JOIN pg_namespace sn ON sn.oid = s.relnamespace
LEFT JOIN pg_depend d ON d.refobjid = s.oid AND d.deptype <> 'i'
LEFT JOIN pg_attrdef ad ON ad.oid = d.objid
LEFT JOIN pg_class t ON t.oid = ad.adrelid
LEFT JOIN pg_namespace tn ON tn.oid = t.relnamespace
WHERE s.relkind = 'S'
AND s.relname ~~ '%part_of_seq_name%' -- enter search term here
ORDER BY 1,2;
Now with LEFT JOIN to show "free-standing" sequences as well.
You can then use the method #Milen posted to make the sequence "free-standing".
I posted a related answer a few days ago.

How do I look at column metadata in Sybase?

I have a list of columns a co-worker has given to me, but these columns reside in different tables in the DB. Is there some kind of tool in Sybase where I can query the table a column belongs to?
(I've tried Google-ing for this kind of tool, but no luck so far)
syscolumns holds column metadata.
select * from syscolumns where name = ;
The id column in syscolumns is the id of the column's table, in sysobjects;
select b.name as tablename, a.name as columnname
from syscolumns a join systables b on (a.id = b.id)
where b.type='U' and b.name = 'foo';
gets all columns for the table named 'foo'. The type = 'U' limits it to user tables.
select b.name as tablename, a.name as columnname
from syscolumns a join systables b on (a.id = b.id)
where b.type='U' and a.name = 'foo';
gets all columns named 'foo'.
Most current version of ASE will use sysbojects instead of systables
I had to make a few small change for it to work:
select b.name as tablename,
a.name as columnname
from dbo.syscolumns a
join sysobjects b on a.id = b.id
where b.type='U'
and upper(a.name) like '%FOO%' -- wildcard search for column name
and b.name = 'bar' -- exclude tables
order by b.name
You can find the information for any column in:
SELECT *
FROM sys.syscolumns
If you want to know to what table a column belongs:
SELECT cname, tname
FROM sys.syscolumns
WHERE tname IN ('col_1', 'col_2')
NOTE: I test this in Sybase ASA 9.