我有这个数据库结构:
CREATE TABLE `user` (
`usr_id` bigint(20) NOT NULL AUTO_INCREMENT,
`usr_mail` varchar(64) NOT NULL,
`usr_creationdate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`usr_id`)
);
CREATE TABLE `user_activation` (
`usract_id` bigint(20) NOT NULL AUTO_INCREMENT,
`usr_id` bigint(20) NOT NULL,
`usract_key` text NOT NULL,
`usract_used` int(11) NOT NULL DEFAULT '0',
`usract_creationdate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`usract_usagedate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`usract_expiredate` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TRIGGER `USER_INSERT_ADD_ACTIVATION` AFTER INSERT ON `user`
FOR EACH ROW
INSERT INTO user_activation (usr_id, usract_key)
VALUES (NEW.usr_id, UUID());
CREATE TRIGGER `USRACT_INSER_SET_EXPIREDATA` BEFORE INSERT ON `user_activation`
FOR EACH ROW
SET NEW.usract_expiredate = DATE_ADD(NOW(), INTERVAL 1 DAY);
当我将数据插入表格user_activation
时,我的user_activation.usract_expiredate
会更新到第二天。
如果我将数据插入表user
,则会在user_activation
中插入一行,但user_activation.usract_expiredate
将不会设置为第二天。
似乎第二个触发器没有被执行。
我是否对触发器做错了什么?
答案 0 :(得分:0)
嗯,你只需要一个触发器即可实现这一目标。
DROP TRIGGER `USRACT_INSER_SET_EXPIREDATA`;
DROP TRIGGER `USER_INSERT_ADD_ACTIVATION`;
CREATE TRIGGER `USER_INSERT_ADD_ACTIVATION` AFTER INSERT ON `user`
FOR EACH ROW
INSERT INTO user_activation (usr_id, usract_key,usract_expiredate)
VALUES (NEW.usr_id, UUID(),DATE_ADD(NOW(), INTERVAL 1 DAY));