Databases Reference
In-Depth Information
The idea here is that every time a book is created, its page count will be added to
the total page count of the topic from this author. Some people may advocate that
it would be better not to keep a separate column for the total here, and instead
compute the total every time we need it. In fact, a design decision must be made
when dealing with this situation in the real world. Do we need to retrieve the total
page count very quickly, for example, for web purposes? what is the response time
to compute this value from a production table with thousands of rows? Anyway,
since I need it as an example, the design decision is easy to make here.
Let us 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 enter the trigger definition in a query box taking special care to enter
// 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
//
Later, the Structure page for our book table reveals a new Triggers section that
can be used the same way as routines, to edit or delete a trigger, as shown in the
following screenshot:
 
Search WWH ::




Custom Search