SQL Server 2005 Object Naming Standards - Part VI: Triggers
I was working on the new application database recently, and it came time to create some triggers to keep date/time and a few other fields up to date. Therefore, it seems like a good time to talk about trigger naming standards. They’re simple, because I’m convinced trigger use should be limited (see Trigger-holic, below). At most, I would expect to find three triggers on a given table; one each for the three triggering events, insert, update and delete. Also, the impact of the trigger should be fairly limited — at the very most — to the table with which the trigger is associated.
With that admonition, the standard trigger name consists of three parts; the name of the table with which the trigger is associated, followed by the event(s) that fire the trigger, followed by the letters Trg. For example, the trigger on the table Dog executed every time a record in inserted or updated, would be called DogInsertUpdateTrg. If the table was called Breed, and the trigger was fired on delete, then the trigger name would be BreedDeleteTrg. As with other naming standards, the three parts of the trigger name as distinguished through the use of camel case. As usual, you’ll see that I have traded a succinct name for one that is a little more verbose, but easy to understand.
Also, keep in mind triggers are associated with schema names, and it is suggested that the trigger schema and the table with which it is associated be the same. For example, if the schema was VetOMatic, then you would want the trigger(s) on this table to be associated with the same schema. The examples above, triggers would be fully specified as VetOMatic.DogInsertUpdateTrg and VetOMatic.BreedDeleteTrg. Don’t get me wrong — VetOMatic is a terrible schema name, but it works for illustrative purposes. More on SQL Server 2005 schemas in a future post.
I’m a Recovering Trigger-holic A long while back, when I first introduced to SQL Server 2005 triggers, I went kind of ‘trigger-crazy’, figuring they were the cure that virtually everything that ailed me. I even went so far as to have triggers create and delete records in related tables to ensure referential integrity was being maintained. It turned out to be a very bad idea. When I eventually handed over the database to someone else to maintain, they were totally flummoxed by records appearing out of, and/or disappearing into nowhere. What I learned from this experience is logic implemented by triggers is not self-evident. More specifically, they don’t show up on the database diagram on SQL Server Management Studio. That’s my primary method of communicating database structure, and the diagrams do nothing to communicate the trigger logic. So these days, I’m limiting trigger use to maintaining date and time stamps, and also, by linking changes to database content to a session record.
OK, that last part was really not much to do with the naming of triggers, but useful information anyway, hopefully. As always, thank you very much for reading, and by all means, let me know what you think.