Select comments on all objects of a table and object informations - postgresql

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;

Related

“Not exists” across multiple joins

I am learning sql (postgres) and id like to find values that do not exist.
I have a table, table 1 with ids and i want to find those ids that are not in table 4.
I have to join between 3 tables as table 1 holds id and table 4 contact_id (not the same number)
The tables 2,3 need to be joined as that connects the ids.
So how do i do that with “not exists”?
Select t1.id, table4.contact_id
From table1 t1
Join table2 using(id)
Join table3 using(id)
Join table4 using(contact_id)
Where not exists (
Select 1
From table4
Where table4.contact_id=t1.id
);
It returns no values, but should
No error msg…
I have thinking error i assume
Your query probably returns no values because you join table4 on contact_id and then you exclude in the WHERE clause the rows which come from this join.
To find values that don't exist, you can usually use LEFT JOIN or RIGHT JOIN or FULL OUTER JOIN and then filter the rows with NULL values in the WHERE clause.
Try this :
SELECT t1.id
FROM table1 t1
LEFT JOIN table2 t2 using(id)
LEFT JOIN table3 t3 using(id)
LEFT JOIN table4 t4 using(contact_id)
WHERE t4.contact_id IS NULL

How to list all constraints of all tables in 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>

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;

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.