Skip to content

Instantly share code, notes, and snippets.

View julp's full-sized avatar

julp julp

View GitHub Profile
@julp
julp / foreign_keys.sql
Created September 18, 2010 17:19
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))