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.infoTRIGGER NAMING CONVENTIONSAlthough not a requirement, it’s a good idea to devise some sort of naming convention for your triggers sothat you can quickly determine the purpose of each. For example, you might consider prefixing eachtrigger title with one of the following strings, as has been done in the trigger-creation example:ad: Execute trigger after a DELETE query has taken placeai: Execute trigger after an INSERT query has taken placeau: Execute trigger after an UPDATE query has taken placebd: Execute trigger before a DELETE query has taken placebi: Execute trigger before an INSERT query has taken placebu: Execute trigger before an UPDATE query has taken placeViewing Existing TriggersAs of <strong>MySQL</strong> version 5.0.10, it’s possible to view existing triggers in one of two ways: by using the SHOWTRIGGERS command or by using the information schema. Both solutions are introduced in this section.The SHOW TRIGGERS CommandThe SHOW TRIGGERS command produces several attributes for a trigger or set of triggers. Its prototypefollows:SHOW TRIGGERS [FROM db_name] [LIKE expr | WHERE expr]Because the output has a tendency to spill over to the next row, making it difficult to read, it’s usefulto execute SHOW TRIGGERS with the \G flag, like so:mysql>SHOW TRIGGERS\GAssuming only the previously created au_reassign_ticket trigger exists in the present database, theoutput will look like this:*************************** 1. row ***************************Trigger: au_reassign_ticketEvent: UPDATETable: techniciansStatement: beginif NEW.available = 0 THENUPDATE tickets SET technician_id=0 WHERE technician_id=NEW.id;END IF;END654

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

Saved successfully!

Ooh no, something went wrong!