April 11, 2014 at 6:17 am
I have an application that has been around for a long time. Most of the FKs are disabled. referential integrity is being handled through triggers. Why would someone have done this?
April 11, 2014 at 6:25 am
A guess, they couldn't get the delete of parent data to work even with a trigger in place while the FK constraints were in place. The fix, disable the constraints instead of setting up the application to delete the child records first, then the parent records.
To get the real answer you will have to ask the vendor of the application.
April 11, 2014 at 7:07 am
souLTower (4/11/2014)
I have an application that has been around for a long time. Most of the FKs are disabled. referential integrity is being handled through triggers. Why would someone have done this?
Perhaps it was so that they could TRUNCATE the table which works fine when triggers are in place but not when FKs are in place.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 11, 2014 at 8:07 am
They turned them off to speed up a data load but never turned them back on?
I don't know. Pretty much blazing away at their feet though.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
April 11, 2014 at 9:00 am
Thanks all. I think the "couldn't make it work with delete triggers" is probably the answer. I took a look at a bunch of other tables and found some that don't have any referential integrity handling at all. Disabled FK and no triggers.
This thing's been around for 10 years. There's some work to do.
Thanks
ST
April 11, 2014 at 9:53 am
Because they would rather have bad data in the database than track down those annoying FK errors?
April 11, 2014 at 12:29 pm
Another possibility:
It also provides documentation of the FK relationships.
I suspect they wanted their application to be able to run on many different dbs. Some don't (or didn't) directly support FK relationships defined to the DBMS. By defining but disabling the FK in those that do allow them to be specified and disabled, they can document the relationships in the DBMS itself without modifying the way the app works.
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
April 11, 2014 at 5:06 pm
At one time, I did this as well. The scenario was a complex environment with lots of FKs on a few 800+ million row tables, any update/insert/delete becomes slow and at the end, we have no choice but to disable FKs, and leaving data integrity to application layer logic.
April 12, 2014 at 7:55 am
jeffrey yao (4/11/2014)
At one time, I did this as well. The scenario was a complex environment with lots of FKs on a few 800+ million row tables, any update/insert/delete becomes slow and at the end, we have no choice but to disable FKs, and leaving data integrity to application layer logic.
I can understand why a lot of people might go that route but I've never actually seen it speed things up if the application is actually checking for data integrity properly.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 12, 2014 at 8:57 am
It's very possible that this happened when the application was converted from Access to SQL7. It was a good find because it caused me to look through the DB and identify some important tables that don't enforce referential integrity. I'll fix those in the same manner as the other tables. I'll create the FK relationships and disable them and will handle integrity through the triggers.
Are triggers faster than letting the DB handle the integrity through FK? I'll do a test but what has been your experiences?
April 12, 2014 at 10:11 am
souLTower (4/12/2014)
It's very possible that this happened when the application was converted from Access to SQL7. It was a good find because it caused me to look through the DB and identify some important tables that don't enforce referential integrity. I'll fix those in the same manner as the other tables. I'll create the FK relationships and disable them and will handle integrity through the triggers.Are triggers faster than letting the DB handle the integrity through FK? I'll do a test but what has been your experiences?
With the right indexes in place, usually, the internal referential integrity check performs much faster than triggers. Heck, I've seen it outperform triggers with no indexes.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
April 12, 2014 at 7:41 pm
souLTower (4/12/2014)
It's very possible that this happened when the application was converted from Access to SQL7. It was a good find because it caused me to look through the DB and identify some important tables that don't enforce referential integrity. I'll fix those in the same manner as the other tables. I'll create the FK relationships and disable them and will handle integrity through the triggers.Are triggers faster than letting the DB handle the integrity through FK? I'll do a test but what has been your experiences?
In general, I wouldn't ever use triggers for Referential Integrity unless you need DRI to another database, which can't be done with FKs. It reminds me of when someone wrote a CLR to do a modulus because they didn't know T-SQL.
That might be another reason why the FKs were disabled... tables may have been moved to another database somewhere in the history of the database.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply