-1

I need to delete records from relational database, where I attempt to start from the lowest children in the database. I'm not very strong on how to approach the task. I don't want to do CASCADE delete, I actually want to do the opposite of CASCADE. Is is correct that I have to find the entity that does not have child and start deleting the records there? and what if an entity has more that one foreign key, how do I decide on which parent table should I start to delete from?

Mindan
  • 979
  • 6
  • 17
  • 37

2 Answers2

0

You have to delete the child records first. If you try to delete a record that is referenced with a foreign key, you will get an error which should indicate which key has a conflict. You can then see which child table is impacted and delete the records that are referencing the foreign key, then try again.

You simply work your way up the chain. If more than one child record references a parent record, you simply delete all the child records first. If more than one parent record is referenced by a child record, it doesn't matter which parent is deleted first (or if they are deleted at all).

daShier
  • 2,056
  • 2
  • 8
  • 14
0

You don't give what database and what tools you have at hand.

You could manually diagram the database based on foreign keys or you could use a tool, such as visual studios to diagram your database.

As long as the multiple foreign relationships don't depend on one another it shouldn't matter where you start.

Dale K
  • 25,246
  • 15
  • 42
  • 71
Gina Marano
  • 1,773
  • 4
  • 22
  • 42