Skip to content

Instantly share code, notes, and snippets.

@julp
Created September 18, 2010 17:19
Show Gist options
  • Save julp/585871 to your computer and use it in GitHub Desktop.
Save julp/585871 to your computer and use it in GitHub Desktop.
try to guess tables dependencies via information_schema/foreign keys
SELECT DISTINCT t.table_name, rc.referenced_table_name
FROM information_schema.tables t
JOIN information_schema.table_constraints tc USING (table_schema, table_name)
LEFT JOIN information_schema.referential_constraints rc ON tc.table_schema = rc.constraint_schema AND tc.table_name = rc.table_name
WHERE t.table_schema='$DB'
/*AND t.table_name IN ('$TABLE1', '$TABLE2')*/
AND (tc.constraint_type = 'FOREIGN KEY' OR (rc.table_name IS NULL AND rc.constraint_schema IS NULL))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment