Skip to main content
All CollectionsText-To-SQL Website
How to get db Schema & Constraints?
How to get db Schema & Constraints?
Aleks Tiupikov avatar
Written by Aleks Tiupikov
Updated over 10 months ago

To get Schema depending on your db type you should run a query in your SQL client and then export the results in CSV.

Keep in mind that this will only return the information about your tables structure and will NOT include any actual data from these tables.

Postgres Query

Schema

Select table_name, column_name, data_type from information_schema.columns where table_schema not in ('information_schema', 'pg_catalog', 'mysql', 'performance_schema', 'sys');

Constraints

SELECT tc.constraint_name, tc.table_name, kcu.column_name, ccu.table_name AS foreign_table_name, ccu.column_name AS foreign_column_name FROM information_schema.table_constraints AS tc JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name AND tc.table_schema = kcu.table_schema JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name WHERE tc.constraint_type != 'CHECK';

If this constraints query above returned nothing, you can try the following one:

SELECT
con.conname AS constraint_name,
tbl.relname AS table_name,
a.attname AS column_name,
CASE
WHEN con.contype = 'p' THEN 'PRIMARY KEY'
WHEN con.contype = 'u' THEN 'UNIQUE'
WHEN con.contype = 'f' THEN 'FOREIGN KEY'
WHEN con.contype = 'c' THEN 'CHECK'
END AS constraint_type,
CASE
WHEN con.confupdtype = 'a' THEN 'NO ACTION'
WHEN con.confupdtype = 'r' THEN 'RESTRICT'
WHEN con.confupdtype = 'c' THEN 'CASCADE'
WHEN con.confupdtype = 'n' THEN 'SET NULL'
WHEN con.confupdtype = 'd' THEN 'SET DEFAULT'
END AS on_update,
CASE
WHEN con.confdeltype = 'a' THEN 'NO ACTION'
WHEN con.confdeltype = 'r' THEN 'RESTRICT'
WHEN con.confdeltype = 'c' THEN 'CASCADE'
WHEN con.confdeltype = 'n' THEN 'SET NULL'
WHEN con.confdeltype = 'd' THEN 'SET DEFAULT'
END AS on_delete
FROM
pg_constraint con
INNER JOIN pg_class tbl ON con.conrelid = tbl.oid
INNER JOIN pg_attribute a ON a.attrelid = tbl.oid AND a.attnum = ANY(con.conkey)
WHERE
tbl.relkind = 'r'
AND tbl.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname = 'YOUR-SCHEMA-NAME')
ORDER BY
table_name;

Make sure to replace the YOUR-SCHEMA-NAME part in the end with your actual schema name.

MySQL Query

Schema

select
table_name,
column_name,
data_type
from
information_schema.columns
where
table_schema not in ('information_schema', 'pg_catalog', 'mysql', 'performance_schema', 'sys');

Constraints

select
CONSTRAINT_NAME,
TABLE_NAME,
COLUMN_NAME,
REFERENCED_TABLE_NAME,
REFERENCED_COLUMN_NAME
from
information_schema.key_column_usage
where
table_schema not in ('information_schema', 'pg_catalog', 'mysql', 'performance_schema', 'sys');

Oracle Query

Schema

SELECT 
table_name,
column_name,
data_type
FROM
all_tab_columns
WHERE
owner NOT IN ('SYS', 'SYSTEM');

Constraints

SELECT 
a.constraint_name,
a.table_name,
a.column_name,
c_pk.table_name AS referenced_table_name,
b.column_name AS referenced_column_name
FROM
all_cons_columns a
JOIN
all_constraints c ON a.owner = c.owner
AND a.constraint_name = c.constraint_name
JOIN
all_constraints c_pk ON c.r_owner = c_pk.owner
AND c.r_constraint_name = c_pk.constraint_name
JOIN
all_cons_columns b ON c_pk.owner = b.owner
AND c_pk.constraint_name = b.constraint_name
WHERE
c.constraint_type = 'R'
AND
a.owner NOT IN ('SYS', 'SYSTEM');
Did this answer your question?