May 27, 2010 at 8:28 am
May 27, 2010 at 8:32 am
you can't without breaking referential integrity.
Otherwise you'd be dropping the foreign key constraint, or disabling the foreign key constraint so it would be invalid after you deleted the parent record.
maybe they were trying to get you to say you'd have to set the child FK to NULL before you delete the parent?
Lowell
May 27, 2010 at 8:44 am
May 27, 2010 at 8:54 am
You'd have to move the child, disable the FK, or drop the FK. My guess is they are moving the child to another record, or a holder record.
Or they don't have FKs defined and are enforcing them in the app only.
May 27, 2010 at 9:01 am
Cascading DELETE with an action of SET NULL or SET DEFAULT
May 27, 2010 at 9:03 am
Paul White NZ (5/27/2010)
Cascading DELETE with an action of SET NULL or SET DEFAULT
Right answer. 10 points.
[font="Courier New"]ZenDada[/font]
May 27, 2010 at 9:08 am
ZenDada (5/27/2010)
Right answer. 10 points.
Steve!!! Ten points please!
May 27, 2010 at 9:09 am
May 27, 2010 at 9:09 am
Did not know that, though I've avoided cascading deletes and updates for years.
Does anyone use this?
May 27, 2010 at 9:17 am
ShekharNaidu (5/27/2010)
hey paul..if you dont mind...can you please elobarate...
Sure, there's a whole section of Books Online about it:
May 27, 2010 at 9:17 am
I've never used Cascading updates or deletes. I think that there is a reason for the FK and that I shouldn't just blindly update or delete child information. If there is a business reason for deleting the parent then there should be business rules enforced in the application for it. I don't think I should be able to do the delete outside the application in that case.
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
May 27, 2010 at 9:19 am
Jack Corbett (5/27/2010)
I've never used Cascading updates or deletes. I think that there is a reason for the FK and that I shouldn't just blindly update or delete child information. If there is a business reason for deleting the parent then there should be business rules enforced in the application for it. I don't think I should be able to do the delete outside the application in that case.
I tend to agree, but this was an interview question to check technical knowledge and/or lateral thinking.
I've only ever used SET NULL, and both times were a work around in a 3rd party system.
May 27, 2010 at 9:26 am
Paul White NZ (5/27/2010)
Jack Corbett (5/27/2010)
I've never used Cascading updates or deletes. I think that there is a reason for the FK and that I shouldn't just blindly update or delete child information. If there is a business reason for deleting the parent then there should be business rules enforced in the application for it. I don't think I should be able to do the delete outside the application in that case.I tend to agree, but this was an interview question to check technical knowledge and/or lateral thinking.
I've only ever used SET NULL, and both times were a work around in a 3rd party system.
I should have quoted Steve's post asking if anyone's used it.
Yes, for an interview answer cascading deletes with SET NULL would be the best answer. Even with this I'd rather enforce it in a stored procedure or business layer, if not using stored procedures. Of course both assume that the column in the child is nullable.
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
May 27, 2010 at 9:30 am
Jack Corbett (5/27/2010)
I should have quoted Steve's post asking if anyone's used it.
Oh, right, gotcha 🙂
Yes, for an interview answer cascading deletes with SET NULL would be the best answer. Even with this I'd rather enforce it in a stored procedure or business layer, if not using stored procedures. Of course both assume that the column in the child is nullable.
It also assumes that NULL and the DEFAULT value are valid parent keys...this usually prompts a long discussion about whether RI is violated by allowing this behaviour. My view is that if the FK relationship stays valid, there's RI. Not everyone agrees with me.
May 27, 2010 at 9:35 am
My view is the PKs, FKs, are based on business rules. You can't define an OrderItem to be definitely linked to an Order without a business rule. So if the business rule allows defaults or NULLs for the FK, then it doesn't violate RI.
However, that somehow assumes that the person performing the update really understands the business rules, and has correctly explained this to the business and gotten agreement. That's where I'd be concerned.
Viewing 15 posts - 1 through 15 (of 16 total)
You must be logged in to reply to this topic. Login to reply