CREATE TRIGGER `after_customer_insert`
AFTER INSERT ON `customer`
FOR EACH ROW BEGIN
UPDATE `user`
SET
`customers_count` = `customers_count` + 1
WHERE `id` = NEW.`user_id`;
END$$
Instead of calling UPDATE user for each new row in customer, would be possible to have a trigger as a “whole”? I mean something like
CREATE TRIGGER `after_customer_insert`
AFTER INSERT ON `customer`
BEGIN
UPDATE `user`
SET
`customers_count` = (
SELECT COUNT(`id`)
FROM `customer`
WHERE `user_id` = `id`
)
END$$
Triggers are mainly intended to be used to execute actions related to a change on a single row, usually changing another single row, as Jake points out. Of course you can implement logic that (redundantly) carries out the same action on largely the same result set for each row updated, but the question is, why would you not issue a single
UPDATEstatement subsequent to your originalUPDATE? You may have a good reason for this, hence Jake’s question about the overall situation.e.g. (syntax not tested, but the general idea holds)