sql - List of table names ordered by their foreign key dependency in MySQL -
given mysql database, how obtain list of table names ordered foreign key dependency can delete each table? understand foreign key's stored in key_column_usage
table i'm not sure if there utility method obtain table names in correct order. prefer 100% sql solution.
if foreign keys follow naming convention (as should :), simple like
select table_name, group_concat(distinct constraint_name order constraint_name) fk_list information_schema.key_column_usage constraint_name rlike 'fk' group table_name order fk_list, table_name;
or, if foreign keys don't follow naming convention, then
select table_name, group_concat(distinct constraint_name order constraint_name) fk_list information_schema.key_column_usage referenced_table_name not null group table_name order fk_list, table_name;
Comments
Post a Comment