Click here to Skip to main content
15,896,730 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table structure in my DB like this...

Table (FireExtinguishers)
Field ID

Table (Building)
Field ID

Table (BuildingInspection)
Field ID
Field FK_Building_ID ON DELETE CASCADE
Field FK_FireExtinguisher_ID ON DELETE CASCADE


So i can delete my building and all my BuildingInspections are also deleted wich is fine , I delete an extinguisher from table (FireExtinguishers) all building inspections for the specific extinguisher are also deleted which is fine too.

The problem is that when i delete a building , all the building inspections are deleted too , BUT THE Extinguishers are remaining i want to delete also all the extinguishers that they are in BuildingInspection table.



So My question is how can i delete the parent row (FireExtinguisher) if no child rows exist (BuildingInspection) table ???
Posted

1 solution

You can either do this by deleting fire extinguishers before deleting the building or (the better way) have each fire extinguisher belong to a building and have the inspection performed on just the fire extinguishers.

a:Each fire extinguisher must belong to a single building anyway.

b:When a fire extinguisher is deleted, the inspection is

c:When the building is deleted, the fire extinguisher is, and therefore 'b'


Table (Building)
Field ID

Table (FireExtinguishers)
Field ID
Field FK_Building_ID ON DELETE CASCADE

Table (BuildingInspection)
Field ID
Field FK_FireExtinguisher_ID ON DELETE CASCADE
 
Share this answer
 
v2
Comments
JNDIONYSIM 3-Jul-15 6:14am    
I must say that your second approach is very good , and i am feeling a little bit idiot right now becouse i didn't think it at the first place :) . I think its very difficult for me to change it right now becouse i have all my code working very good for the inserts ,updates and quering . But i will consider it very serious .

What do you think about adding a trigger or a function in my database and dont change anything in my code ?
Andy Lanng 3-Jul-15 6:19am    
Personally, I would make the change at the db level now. It will only get more difficult to come back to later. i wouldn't ever use triggers for anything but audit tables. I wouldn't even use cascade deletions.
But that's me (and probably a lot of top lvl devs). i try to keep my business logic out of the database.

If your serious about keeping the business logic in the db then you have to be clear about what you are going to do in the db and what you're not going to do.

If I took that approach, I would create a stored procedure to manage my deletions of any of the three entities in your question and I would not use Cascade deletions.

That's my 2cents ^_^
Sreekanth Mothukuru 3-Jul-15 6:21am    
Triggers always hide whatever logic you written and will be very difficult to trace even for its existence. Better option is to have cascade delete.
JNDIONYSIM 3-Jul-15 10:03am    
Of following your instructions i forget about triggers or procedures in my db , But becouse icant change all the code that i wrote now it will be a lot of work to do ... I decide to write a query for unmatched rows and delete them after i delete a building and i think iget a good result . But i am accepting Andy lanng solution becouse i had to design my db better in the first place... I am an amateur programmer and i am still learning ... Next time i will be more carefull . Thank you very much for your time and your help.
Andy Lanng 3-Jul-15 10:05am    
No probs.

It's always worth taking the time to think about the db first, or design your system in such a way that changes are minor, as I usually do.

I'm glad you found a solution. Good luck ^_^

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900