-
Website
http://awads.net/wp/ -
Original page
http://awads.net/wp/2008/05/20/the-lazy-developer%e2%80%99s-way-to-populate-a-surrogate-key/ -
Subscribe
All Comments -
Community
-
Top Commenters
-
jgarry
3 comments · 1 points
-
Andy C
22 comments · 47 points
-
dahowlett
1 comment · 2 points
-
Don Seiler
9 comments · 1 points
-
davidhaimes
4 comments · 3 points
-
-
Popular Threads
This way, you can ensure that the sequence is used every time. Plus you get the added bonus of not having to use a database trigger. I've tested it as being 25-30% faster than using a database trigger.
Just my two cents.
Regards,
Rob.
I'm sure the developer would have wasted a few hours on figuring out why the keys used was +1 relative to the value he/she had provided. The only use of triggers I've found that is usable has been to work around software bugs while the software is bing fixed.
Triggers is one feature I've never liked. It hides things, and further slows things down. In this case, what happens the day you need to delete and reinsert the row? The key will change. It may not be likely that you would need to do a delete+insert, but it could happen.
@Mathias: Yes, developers should be educated, but also rules, standards and security should be enforced. I have wasted time figuring out what the reason was behind the ORA-00001, where it could have been avoided by simply having a trigger or enforcing security as Rob suggested or both. I do not like triggers either as I stated in the post above.
Eddie, i agree with you here, perfectly!
My "excuse" for allowing TRIGGERs is:
I like TRIGGERs for DB maintenance. For example, if a history is maintained, a change-log, or the like, items that are in a sense part of the DB model, i believe the DB should do it. Hmm.. it isn't so cut and dry, i guess i need to work on a more specific rule.
Either way, in this case, the PK has no intrinsic value, and is just being used to have a good way to uniquely refer to the record.
I have made a recent change in my policy, however, to allow overrides. In the TRIGGER i started to as WHEN (Old.Id IS NULL). This way, the TRIGGER is helpful, but doesn't overwrite what the developer wants to do.
As for triggers - I have a different opinion from Eddie - I LIKE to hide logic in triggers. That way developers can screw up their applications, but they CAN'T screw up the database. And it isn't really hidden - SQL Developer or TOAD shows you what triggers are there. I comment the heck out of them so you know WHY they are there.
@John:
> but we have a procedure to re-set the sequence to one higher than the maximum PK
That's what I did to fix the problem.
> And it isn’t really hidden
Right, if you know what tables have them and what events will trigger them.
I understand your point. Personally, i don't like to force the developers to do one way or another, unless it is a question of breaking data integrity.
IMHO, developers make up their own numbers because it is not done for them. However, if a TRIGGER automagically pops the SEQUENCE for them, they won't bother, unless they need to test a specific case.
Also, there may be the situation where a TRIGGER is preffered, but now and then needs to be overrided because a query will not allow the use of a RETURNING clause. In which case, the SEQUENCE needs to be popped manually and the TRIGGER overridden.
As for the IF, i thought WHEN is faster as it is evaulated before the TRIGGER is executed.
@John
I agree with Eddie on most TRIGGERs. It is a real pain trying to figure out who fired what and when.
An then when people *rely* on the TRIGGERs, but some TRIGGERs have not been updated with the new rules, and so on, figuring out the logic (which may include firing order) is more trouble than its worth.
If you want to control what developers can do to the DB, remove access to the TABLEs, and use PROCEDUREs instead to implement the approved methods. Using TRIGGERs is more of a way to cleanup, not prevent.