Created
April 20, 2024 09:54
-
-
Save honzapav/61ffb11b5a381e346c85e40a5ab67033 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
cols.table_schema, | |
cols.table_name, | |
cols.column_name, | |
cols.data_type, | |
cols.column_default, | |
( | |
SELECT CASE | |
WHEN COUNT(*) > 0 THEN 'Yes' | |
ELSE 'No' | |
END | |
FROM information_schema.table_constraints AS tc | |
JOIN information_schema.constraint_column_usage AS ccu | |
ON ccu.constraint_schema = tc.constraint_schema | |
AND ccu.constraint_name = tc.constraint_name | |
WHERE tc.constraint_type = 'FOREIGN KEY' | |
AND ccu.table_schema = cols.table_schema | |
AND ccu.table_name = cols.table_name | |
AND ccu.column_name = cols.column_name | |
) AS is_foreign_key, | |
cols.is_nullable | |
FROM information_schema.columns AS cols | |
WHERE cols.table_schema NOT IN ('pg_catalog', 'information_schema') | |
AND cols.table_catalog = 'staging' -- Replace 'YourDatabaseName' with your actual database name | |
ORDER BY | |
cols.table_schema, | |
cols.table_name, | |
cols.ordinal_position; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment