views:

91

answers:

1

For instance, suppose I have table A. Then I have tables B-Z that have a foreign key to table A's primary key. Then perhaps there are also some tables that have a foreign key constraint to a table in B-Z's primary key constraint. Is there any easy way to clear out table A and all of the tables that refer to A (or that refer to a table that refers to A) without having to explicitly delete from each table or add an ON CASCADE constraint to each foreign key?

Note that this is mainly for testing purposes, not to be used in production. I would just drop the entire schema and start over again, but that simply isn't feasible for every test (considering how long it takes to build the schema).

+1  A: 

I think the most efficient way to do this would be to drop all the FK's, truncate the tables, and then rebuild the FK's.

dpbradley
He won't be able to recreate all the FKs until the specific rows out of the child tables have been fixed, though. I think he only wants to remove some of the rows out of the child tables.
Rob
I was basing my answer on the phrase "clear out table A and all of the tables that refer to A (or that refer to a table that refers to A)", which I assume meant that the child tables should be completely emptied as well.
dpbradley