Reputation: 31
Background I wanted to migrate data with insert operation on multiple tables. The tables have foreign key relationships between themselves. If an INSERT operation is done on a table with a foreign key before the referenced table is being inserted to, the operation might fail due to violation of the foreign key.
Requirement Produce a list of tables within a database ordered according to their dependencies. Tables with no dependencies (no foreign keys) will be 1st. Tables with dependencies only in the 1st set of tables will be 2nd. Tables with dependencies only in the 1st or 2nd sets of tables will be 3rd. and so on...
Upvotes: 2
Views: 1076
Reputation: 4625
If you still want to know FK dependencies for other purpose:
SELECT master_table.table_name master_table_name,
master_table.column_name master_key_column,
detail_table.table_name detail_table_name,
detail_table.column_name detail_column
FROM all_constraints constraint_info
JOIN
all_cons_columns detail_table ON constraint_info.constraint_name = detail_table.constraint_name
JOIN
all_cons_columns master_table ON constraint_info.r_constraint_name = master_table.constraint_name
WHERE
detail_table.position = master_table.position
AND constraint_info.constraint_type = 'R'
AND constraint_info.owner = 'SCHEMA_OWNER'
From there, you can do a recursive query starting with detail_table_name not in the master_table_name list and connecting by prior master_table_name = detail_table_name.
Upvotes: 2
Reputation: 142958
From my point of view, that's wrong approach.
There's utility which takes care about your problem, and its name is Data Pump.
expdp
) will export everything you wantimpdp
) will then import data, taking care about foreign keysI suggest you use it.
If you don't want to (though, I can't imagine why), then consider
insert
statements - all of them should succeed (at least, regarding foreign keys; can't tell about other constraints)
Upvotes: 1