当检查行数据值为1时,我需要创建将行从表传输到具有相同名称行的另一个表的触发器,以便检查它是否被移动。插入或更改某些内容时会发生这种情况。有任何想法吗?
答案 0 :(得分:0)
MySQL错误1442:
错误代码:1442。无法在存储的函数/触发器中更新表'xyz' 因为它已被调用此存储的语句使用 功能/触发。
由于服务器知道它在同一个调用堆栈中,会发生类似触发器调用的级联或递归情况。
关于您可以做的最好的事情是利用事件清理策略,您可以选择所选的频率。如果您使用它,请记住它利用了Global事件调度程序,您不能忘记它正在运行。此外,在大多数共享托管环境中,由于共享资源而不支持事件(人们倾向于滥用它们并且它会影响每个人)。
那就是说,
架构:
create table orig101
( id int auto_increment primary key,
thing1 varchar(100) not null,
checked int not null
);
create table copy101
( id int primary key,
thing1 varchar(100) not null,
checked int not null
);
事件:
DROP EVENT IF EXISTS cleanUpCopied;
DELIMITER $$
CREATE EVENT cleanUpCopied
ON SCHEDULE EVERY 2 MINUTE STARTS '2016-01-01 00:00:00'
ON COMPLETION PRESERVE
DO
BEGIN
DELETE FROM orig101 WHERE checked=1;
END $$
DELIMITER ;
-- -------------------------------------------------
select @@event_scheduler; -- mine is currently OFF
SET GLOBAL event_scheduler = ON; -- turn her on and confirm below
select @@event_scheduler; -- yep it is ON
触发器:
DROP TRIGGER IF EXISTS aft_ins_orig;
delimiter $$
CREATE TRIGGER aft_ins_orig AFTER INSERT ON orig101
FOR EACH ROW
BEGIN
IF NEW.checked=1 THEN
INSERT IGNORE copy101 (id,thing1,checked)
SELECT NEW.id,NEW.thing1,NEW.checked;
END IF;
END;$$
delimiter ;
-- -------------------------------------------------
DROP TRIGGER IF EXISTS aft_upd_orig;
delimiter $$
CREATE TRIGGER aft_upd_orig AFTER UPDATE ON orig101
FOR EACH ROW
BEGIN
IF NEW.checked=1 THEN
INSERT IGNORE copy101 (id,thing1,checked)
SELECT NEW.id,NEW.thing1,NEW.checked;
END IF;
END;$$
delimiter ;
测试:
truncate orig101;
truncate copy101;
INSERT orig101 (thing1,checked) values ('frog',0);
select * from orig101; -- 1 row
select * from copy101; -- 0 rows
INSERT orig101 (thing1,checked) values ('fish',1); -- when evt fires, cleanup occurs
select * from orig101; -- until evt fires, checked=1 row(s) still there
select * from copy101;
INSERT orig101 (thing1,checked) values ('fish2',1); -- when evt fires, cleanup occurs
INSERT orig101 (thing1,checked) values ('fish3',1); -- when evt fires, cleanup occurs
INSERT orig101 (thing1,checked) values ('fish4',1); -- when evt fires, cleanup occurs
INSERT orig101 (thing1,checked) values ('fish5',1); -- when evt fires, cleanup occurs
select * from orig101; -- until evt fires, checked=1 row(s) still there
select * from copy101;
UPDATE orig101 set checked=1 where id=1;
-- Tweak the frequency of event firing (say every minute, or every hour, whatever)
我的个人资料页面上有大约3个链接用于活动。祝你好运。