0

嗨,我有像这样的表结构

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结果时没有任何结果,因此得出的结论是数据库中不存在触发器。我不知道我在哪里犯错。请帮我找出错误。我在 MySQL Server 5.5 中使用 innodb 引擎。

提前致谢。

知道了。

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 ;

我不明白,但我执行了上面的表达式并且它有效。

4

1 回答 1

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 |
+-------------------+-------------------------+

所以,一切似乎都很好!您究竟发出了哪些不起作用的查询?

于 2012-07-14T12:34:58.087 回答