Skip to content

Instantly share code, notes, and snippets.

View MFinn87's full-sized avatar

Mike MFinn87

  • Boulder, CO / Denver, CO
View GitHub Profile
@ststeiger
ststeiger / Foreign_key_dependencies_PGSQL.sql
Last active September 23, 2024 06:16
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