Posted on November 24th, 2010
2098 views Addressing The Issue Our scenario has an issue that would soon become apparent once we began writing code. That is, it's a pain to have to take care of two separate tables when dealing with the comments system. Every single time a new comment is created the user table needs to be updated. And every time a comment is deleted, the same thing has to occur. Chances are this action would take the form of a method or function in your code, which you'd have to call every time you did an INSERT or DELETE. What if you or someone you're working with forgets to call this update method? Humans are error prone and it's not entirely out of the question that this mistake would be made. So let's solve it. The solution: takes form of triggers. A trigger is a stored routine, not unlike procedures or functions. But unlike procedures and functions, a trigger is called automatically. When it's called depends on how you configure it. But you may configure a trigger to activate on any of the three following actions: INSERT, UPDATE and DELETE. Triggers are table-specific. When writing a trigger you will declare the action that activates it (any of the three above) and the table on which that action needs to occur. Triggers may be used for any number of things, but they are commonly employed along side procedures and functions. |