你有沒有演示這種行爲的例子?
以下示例更新row_count_line_items
列中的items
表,從BEFORE TRIGGER
開始,對line_items
表中的行數進行計數。因爲它是一個BEFORE TRIGGER
,row_count_line_items
列爲零(0)(INSERT
沒有在那一刻做):
mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)
mysql> DROP TRIGGER IF EXISTS `line_item_trigger`;
Query OK, 0 rows affected (0.00 sec)
mysql> DROP TABLE IF EXISTS `line_items`, `items`;
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE TABLE IF NOT EXISTS `items` (
-> `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
-> `quantity_sold` int(11) unsigned DEFAULT 0,
-> `row_count_line_items` int,
-> PRIMARY KEY (`id`)
->) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `line_items` (
-> `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
-> `item_id` int(11) unsigned DEFAULT NULL,
-> `quantity` int(11) unsigned DEFAULT 1,
-> PRIMARY KEY (`id`),
-> CONSTRAINT `fk` FOREIGN KEY (`item_id`) REFERENCES items (`id`)
->) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO `items` (`id`) VALUES (1);
Query OK, 1 row affected (0.01 sec)
mysql> CREATE TRIGGER `line_item_trigger` BEFORE INSERT ON `line_items`
-> FOR EACH ROW
-> UPDATE `items`
-> SET `quantity_sold` = `quantity_sold` + NEW.`quantity`,
-> `row_count_line_items` = (SELECT COUNT(`id`)
-> FROM `line_items`)
-> WHERE `id` = NEW.`item_id`;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO `line_items` (`item_id`) VALUES (1);
Query OK, 1 row affected (0.01 sec)
mysql> COMMIT;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT `id`, `quantity_sold`, `row_count_line_items`
-> FROM `items`;
+----+---------------+----------------------+
| id | quantity_sold | row_count_line_items |
+----+---------------+----------------------+
| 1 | 1 | 0 |
+----+---------------+----------------------+
1 row in set (0.00 sec)
mysql> SELECT `id`, `item_id`, `quantity`
-> FROM `line_items`;
+----+---------+----------+
| id | item_id | quantity |
+----+---------+----------+
| 1 | 1 | 1 |
+----+---------+----------+
1 row in set (0.00 sec)
見db-fiddle。
如果我們改變了事件AFTER
(AFTER TRIGGER
),然後row_count_line_items
列將有一個(1)(因爲,INSERT
已經完成)值:
mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)
mysql> DROP TRIGGER IF EXISTS `line_item_trigger`;
Query OK, 0 rows affected (0.00 sec)
mysql> DROP TABLE IF EXISTS `line_items`, `items`;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `items` (
-> `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
-> `quantity_sold` int(11) unsigned DEFAULT 0,
-> `row_count_line_items` int,
-> PRIMARY KEY (`id`)
->) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `line_items` (
-> `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
-> `item_id` int(11) unsigned DEFAULT NULL,
-> `quantity` int(11) unsigned DEFAULT 1,
-> PRIMARY KEY (`id`),
-> CONSTRAINT `fk` FOREIGN KEY (`item_id`) REFERENCES items (`id`)
->) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO `items` (`id`) VALUES (1);
Query OK, 1 row affected (0.00 sec)
mysql> CREATE TRIGGER `line_item_trigger` AFTER INSERT ON `line_items`
-> FOR EACH ROW
-> UPDATE `items`
-> SET `quantity_sold` = `quantity_sold` + NEW.`quantity`,
-> `row_count_line_items` = (SELECT COUNT(`id`)
-> FROM `line_items`)
-> WHERE `id` = NEW.`item_id`;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO `line_items` (`item_id`) VALUES (1);
Query OK, 1 row affected (0.08 sec)
mysql> SELECT `id`, `quantity_sold`, `row_count_line_items`
-> FROM `items`;
+----+---------------+----------------------+
| id | quantity_sold | row_count_line_items |
+----+---------------+----------------------+
| 1 | 1 | 1 |
+----+---------------+----------------------+
1 row in set (0.00 sec)
mysql> COMMIT;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT `id`, `item_id`, `quantity`
-> FROM `line_items`;
+----+---------+----------+
| id | item_id | quantity |
+----+---------+----------+
| 1 | 1 | 1 |
+----+---------+----------+
1 row in set (0.00 sec)
見db-fiddle。
你在一個事務中執行多個插入'line_items'嗎? –
正如我所知,在他插入新行之前觸發器會首先更新您的查詢。根據我的經驗,這是觸發器的工作原理 – Noob
@ilya bursov這只是在每個事務中插入的單個行項目。 –