简体   繁体   English

插入后删除触发器

[英]Trigger to delete after insert

I have two tables, new table and registered table, what I want is that what is new will be registered when an update is made to a boolean (Accepted = 1 send it to registered) and after the information has been inserted back to registered delete that same one again, try to make a trigger that would do that but although it saves me well when doing this operation the trigger does not advance and does not do the operations我有两个表,新表和注册表,我想要的是当对 boolean 进行更新(接受 = 1 将其发送到注册)并在信息插入回注册删除后将注册新的内容再次相同,尝试制作一个可以做到这一点的触发器,但是虽然它在执行此操作时很好地节省了我,但触发器不会前进并且不会执行操作

BEGIN
CREATE TRIGGER `trigger_1` AFTER UPDATE ON `new` 
FOR EACH ROW INSERT INTO registered (Num_Registered, Name_Registered, Type_Id, Semes_Registered, Fac_Registered, Pass) SELECT Mat_New, Name_New, Id_New, Semes_New, Fac_New, Pass_New FROM new WHERE Accepted=1;

DELETE FROM new WHERE Accepted=1 OR Accepted=0;//Here taking into account that 0 is rejected and deleted, and 1 is accepted and sent to registered
END;

In the same way I tried to make 2 triggers (one that made the insertion of one table to another and then another trigger that after the insertion deletes said in the table first) but they only collide with each other * error 1442 -cant update table new in stored function / trigger because it is already in use by statement which invoked this stored function / trigger以同样的方式,我尝试制作 2 个触发器(一个将一个表插入另一个表,然后另一个触发器在插入后首先在表中删除),但它们只会相互冲突 * 错误 1442 - 无法更新表新存储的 function / 触发器,因为它已被调用此存储的 function / 触发器的语句使用

CREATE TRIGGER `trigger_1` BEFORE UPDATE ON `new`
FOR EACH ROW INSERT INTO registered (Num_Registered, Name_Registered, Type_Id, Semes_Registered, Fac_Registered, Pass) SELECT Mat_New, Name_New, Id_New, Semes_New, Fac_New, Pass_New FROM new WHERE Accepted=1;


CREATE TRIGGER `trigger_2` AFTER UPDATE ON `new`
 FOR EACH ROW DELETE FROM new WHERE Accepted=1 OR Accepted=0;//Here taking into account that 0 is rejected and deleted, and 1 is accepted and sent to registered

Obviously, the insert into the registrations table is simple.显然,插入registrations很简单。 And I can understand reasons why you would want this.我可以理解你想要这个的原因。 For instance, you might have additional data for the "accepted" folks.例如,您可能有“接受”人员的其他数据。 You might want foreign key relationships only to "accepted".您可能只希望外键关系“接受”。

The challenge is that you want the update to turn into a delete .挑战在于您希望update变成delete I don't think that is possible in a trigger in MySQL.我认为这在 MySQL 的触发器中是不可能的。 But a simple alternative is to just use a view:但是一个简单的选择是只使用一个视图:

create view v_new as
    select n.*
    from new n
    where n.accepted <> 1;

This also has the advantage that you have a record of everyone who was ever new, even though they would not be visible to the users who use the view.这还有一个优点,即您可以记录每个新用户,即使使用该视图的用户看不到他们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM