Is there any way to query the data dictionary to get a list of tables in the order in which they should be populated.
I have a project where I am doing bulk inserts on a large number of tables and it would be hugely helpful if I could get a list of the tables that was sorted by the order in which they should be populated.
My specific use case is MySql but it would be interesting to know if this capability also existed in Oracle, MS SqlServer etc.
EDIT: For example, if I create these tables (psudo code)
person_type (
person_type_id
);
person (
person_id,
person_type_id references person_type.person_type_id
);
address (
address_id,
street1,
street2,
city,
state,
zip
);
person_address (
person_id references person_id
address_id references address.address_id
);
I would expect the sort order to be something like this:
0 address
0 person_type
1 person
2 person_address
- address has no dependencies so can be populated first (sort rank = 0)
- person_type has no dependencies so can also be populated first (sort rank = 0)
- person depends on person_type so it cannot be populated until an appropriate person_type record is created (sort rank = 1)
- person_address depends on person_type, person, and address so it can only be populated after records exist in those tables (sort rank = 2)