Database Reference
In-Depth Information
Let's not forget that following its addition to the table's structure, the total_page_
count column should initially be seeded with the correct total. (However, this is not
the purpose of our trigger.)
Manually creating a trigger
The current phpMyAdmin version does not have an interface for trigger creation.
Therefore, we input the trigger definition in a query box, taking special care of
entering // in the delimiter box:
CREATE TRIGGER after_book_insert AFTER INSERT ON book
FOR EACH ROW
BEGIN
UPDATE author
SET total_page_count = total_page_count + NEW.page_count
WHERE id = NEW.author_id;
END
Afterwards, the Structure page for our book table reveals under the Details slider
a new Triggers section that can be used the same way as routines, to edit or delete
a trigger:
Testing the trigger
Contrary to testing stored procedures or functions, there is neither a CALL
sequence nor a function inside a SELECT statement to execute the trigger. Any time
the defined operation (a book INSERT ) happens, the code will execute (in our case,
after the insertion). Therefore, we just have to insert a new book to see that the
author.total_page_count column is updated.
Of course, a completely automatic management of this column would involve
creating AFTER UPDATE and AFTER DELETE triggers on the book table.
 
Search WWH ::




Custom Search