Last active
May 23, 2020 20:25
-
-
Save lebalz/11ca4a64a52905c9f7948bbb6c6a6919 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 as 'database', | |
cols.TABLE_NAME as 'table', | |
cols.COLUMN_NAME as 'column', | |
cols.DATA_TYPE AS 'data_type', | |
cols.COLUMN_TYPE AS 'type', | |
COALESCE(cols.COLUMN_DEFAULT, cols.extra) AS 'default', | |
( | |
CASE WHEN COLUMN_KEY='PRI' | |
THEN 'YES' | |
ELSE 'NO' | |
END | |
) AS 'is_primary', | |
( | |
CASE WHEN COLUMN_KEY='MUL' | |
THEN 'YES' | |
ELSE 'NO' | |
END | |
) AS 'is_primary', | |
cols.IS_NULLABLE AS 'is_nullable', | |
constraints.CONSTRAINT_NAME AS 'constraint', | |
constraints.REFERENCED_TABLE_SCHEMA AS 'referenced_database', | |
constraints.REFERENCED_TABLE_NAME AS 'referenced_table', | |
constraints.REFERENCED_COLUMN_NAME AS 'referenced_column' | |
FROM INFORMATION_SCHEMA.COLUMNS cols | |
LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE constraints | |
ON cols.TABLE_SCHEMA = constraints.TABLE_SCHEMA | |
AND cols.TABLE_NAME = constraints.TABLE_NAME | |
AND cols.COLUMN_NAME = constraints.COLUMN_NAME | |
WHERE | |
cols.TABLE_SCHEMA != 'mysql' | |
AND cols.TABLE_SCHEMA != 'performance_schema' | |
AND cols.TABLE_SCHEMA != 'information_schema' | |
AND cols.TABLE_SCHEMA != 'sys'; |
This file contains hidden or 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_CATALOG AS "database", | |
cols.TABLE_SCHEMA AS "schema", | |
cols.TABLE_NAME AS "table", | |
cols.COLUMN_NAME AS "column", | |
cols.DATA_TYPE AS "data_type", | |
cols.COLUMN_DEFAULT AS "default", | |
cols.IS_NULLABLE AS "is_nullable", | |
COALESCE(constraints.is_foreign, 'NO') AS "is_foreign", | |
COALESCE(constraints.is_primary, 'NO') AS "is_primary", | |
constraints.constraint AS "constraint", | |
constraints.referenced_database AS "referenced_database", | |
constraints.referenced_schema AS "referenced_schema", | |
constraints.referenced_table AS "referenced_table", | |
constraints.referenced_column AS "referenced_column" | |
FROM INFORMATION_SCHEMA.COLUMNS cols | |
LEFT JOIN ( | |
SELECT | |
cols.TABLE_CATALOG AS "referenced_database", | |
cols.TABLE_SCHEMA AS "referenced_schema", | |
cols.TABLE_NAME AS "referenced_table", | |
cols.COLUMN_NAME AS "referenced_column", | |
refs.table_catalog AS "from_database", | |
refs.table_schema AS "from_schema", | |
refs.table_name AS "from_table", | |
refs.column_name AS "from_column", | |
refs.CONSTRAINT_NAME AS "constraint", | |
'YES' AS "is_foreign", | |
NULL AS "is_primary" | |
FROM INFORMATION_SCHEMA.COLUMNS cols | |
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE refs | |
ON cols.TABLE_CATALOG=refs.constraint_catalog | |
AND cols.TABLE_SCHEMA=refs.constraint_schema | |
AND cols.table_name=refs.table_name | |
AND cols.ordinal_position=refs.position_in_unique_constraint | |
UNION | |
SELECT | |
NULL AS "referenced_database", | |
NULL AS "referenced_schema", | |
NULL AS "referenced_table", | |
NULL AS "referenced_column", | |
refs.table_catalog AS "from_database", | |
refs.table_schema AS "from_schema", | |
refs.table_name AS "from_table", | |
refs.column_name AS "from_column", | |
refs.CONSTRAINT_NAME AS "constraint", | |
NULL AS "is_foreign", | |
'YES' AS "is_primary" | |
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE refs | |
WHERE refs.position_in_unique_constraint IS NULL | |
) constraints | |
ON cols.TABLE_CATALOG=constraints.from_database | |
AND cols.TABLE_SCHEMA=constraints.from_schema | |
AND cols.table_name=constraints.from_table | |
AND cols.column_name=constraints.from_column | |
WHERE | |
cols.TABLE_SCHEMA != 'pg_catalog' | |
AND cols.TABLE_SCHEMA != 'information_schema' | |
ORDER BY cols.TABLE_CATALOG, cols.TABLE_SCHEMA, cols.TABLE_NAME, cols.COLUMN_NAME; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment