11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 33 • MYSQL TRIGGERSwww.it-ebooks.info• Audit trails: Suppose you are using <strong>MySQL</strong> to log Apache traffic (possibly usingthe Apache mod_log_sql module) but you also want to create an additional speciallogging table that lets you quickly tabulate and display the results to an impatientexecutive. Executing this additional insertion can be done automatically with atrigger.• Validation: You can use triggers to validate data before updating the database,such as to ensure that a minimum-order threshold has been met.• Referential integrity enforcement: Sound database administration practicedictates that table relationships remain stable throughout the lifetime of a project.Rather than attempt to incorporate all integrity constraints programmatically, itoccasionally may make sense to use triggers to ensure that these tasks occurautomatically.The utility of triggers stretches far beyond these purposes. Suppose you want to update thecorporate web site when the $1 million monthly revenue target is met. Or suppose you want to e-mailany employee who misses more than two days of work in a week. Or perhaps you want to notify amanufacturer when inventory runs low on a particular product. All of these tasks can be handled bytriggers.To provide you with a better idea of the utility of triggers, let’s consider two scenarios, the firstinvolving a before trigger, a trigger that occurs prior to an event, and the second involving an after trigger,a trigger that occurs after an event.Taking Action Before an EventSuppose that a food distributor requires that at least $10 of coffee be purchased before it will process thetransaction. If a customer attempts to add less than this amount to the shopping cart, that value willautomatically be rounded up to $10. This process is easily accomplished with a before trigger, which, inthis example, evaluates any attempt to insert a product into a shopping cart, and increases anyunacceptably low coffee purchase sum to $10. The general process looks like this:Shopping cart insertion request submitted.If product identifier set to "coffee":If dollar amount < $10:Set dollar amount = $10;End IfEnd IfProcess insertion request.Taking Action After an EventMost helpdesk support software is based upon the notion of ticket assignment and resolution.Tickets are both assigned to and resolved by helpdesk technicians, who are responsible for logging ticketinformation. However, occasionally even the technicians are allowed out of their cubicle to take avacation or recover from an illness. Clients can’t be expected to wait for the technician to return during650

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!