Skip to content

Instantly share code, notes, and snippets.

View eugeneduvenage's full-sized avatar

Eugene Duvenage eugeneduvenage

View GitHub Profile
@eugeneduvenage
eugeneduvenage / Foreign_key_dependencies_PGSQL.sql
Created July 1, 2020 19:08 — forked from ststeiger/Foreign_key_dependencies_PGSQL.sql
List tables by foreign-key dependencies PostgreSQL (also works on MS-SQL with minor modifications)
DROP TABLE IF EXISTS CTE_AllTables;
-- CREATE TEMPORARY TABLE IF NOT EXISTS CTE_AllTables
CREATE TEMPORARY TABLE CTE_AllTables AS
SELECT
ist.table_schema AS OnTableSchema
,ist.table_name AS OnTableName
,tForeignKeyInformation.FkNullable
-- WARNING: TableSchema or Tablename can contain entry-separator (';' used)
,CAST(DENSE_RANK() OVER (ORDER BY ist.table_schema, ist.table_name) AS varchar(20)) AS OnTableId