我有像
这样的表结构CREATE TABLE IF NOT EXISTS `history` (
`order_product_id` varchar(20) NOT NULL,
`Details` text NOT NULL,
PRIMARY KEY (`order_product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `order_product` (
`order_product_id` varchar(64) NOT NULL,
`porder_status` int(11) NOT NULL,
PRIMARY KEY (`order_product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `order_product` (`order_product_id`, `porder_status`) VALUES
('1339789127O532877', 11),
('1339789127O532933', 2),
('1339868495O010300', 2),
('1339868495O010342', 0),
('1339869923O564839', 0);
现在我想编写一个触发器来触发更新 order_product.porder_status &的价值。将 order_product.porder_status 值插入/更新为历史记录表
DELIMITER $$
CREATE TRIGGER addtohistory
AFTER UPDATE ON order_product
FOR EACH ROW
BEGIN
INSERT INTO history (order_product_id,Details)
values
(NEW.order_product_id,CONCAT_WS(':',NEW.porder_status,CURRENT_TIMESTAMP))
ON DUPLICATE KEY UPDATE
Details =
CONCAT_WS(',',Details,CONCAT_WS(':',NEW.porder_status,CURRENT_TIMESTAMP));
END;$$
当我创建触发器时,它表示查询已成功执行,执行SHOW triggers
结果为Nothing,因此结论是数据库中没有触发器。我不知道我在哪里弄错了。请帮我查一下这个bug。我正在使用innodb engin和MySQL Server 5.5。
提前致谢。
知道了。
DROP TRIGGER IF EXISTS `addtohistory`;
DELIMITER $$
CREATE TRIGGER `addtohistory` AFTER UPDATE ON `order_product`
FOR EACH ROW BEGIN
INSERT INTO history (order_product_id,Details) values (NEW.order_product_id,CONCAT_WS(':',NEW.porder_status,CURRENT_TIMESTAMP)) ON DUPLICATE KEY UPDATE Details = CONCAT_WS(',',Details,CONCAT_WS(':',NEW.porder_status,CURRENT_TIMESTAMP));
END
$$
DELIMITER ;
我不明白,但我执行了以上表达&它有效。
答案 0 :(得分:2)
感谢您提供完整的架构代码。
你说“数据库中没有触发器”是什么意思?我跟着你的代码,然后发出:
SHOW TRIGGERS \G
*************************** 1. row ***************************
Trigger: addtohistory
Event: UPDATE
Table: order_product
Statement: BEGIN INSERT INTO history (order_product_id,Details) values (NEW.order_product_id,CONCAT_WS(':',NEW.porder_status,CURRENT_TIMESTAMP)) ON DUPLICATE KEY UPDATE Details=CONCAT_WS(',',Details,CONCAT_WS(':',NEW.porder_status,CURRENT_TIMESTAMP));END
Timing: AFTER
Created: NULL
sql_mode:
Definer: root@localhost
character_set_client: latin1
collation_connection: latin1_swedish_ci
Database Collation: latin1_swedish_ci
所以触发器似乎就在那里。
此外,它似乎正在起作用:
UPDATE order_product SET porder_status=123 WHERE order_product_id='1339869923O564839';
Query OK, 1 row affected (0.08 sec)
Rows matched: 1 Changed: 1 Warnings: 0
SELECT * FROM history;
+-------------------+-------------------------+
| order_product_id | Details |
+-------------------+-------------------------+
| 1339869923O564839 | 123:2012-07-14 15:33:05 |
+-------------------+-------------------------+
所以,一切似乎都很好!你究竟发出什么查询不起作用?