简体   繁体   English

删除触发器后的MySQL

[英]MySQL after delete Trigger

I have 2 tables member_details and archives . 我有2个表member_detailsarchives I want a trigger that will insert deleted data from member_details into archives as soon as as a particular record is deleted from the member_details table. 我想要一个trigger ,一旦从member_details表中删除了特定的记录,就会insertmember_details删除的数据insertarchives中。

DELIMITER $$ 
CREATE TRIGGER member_details_ADEL AFTER DELETE ON member_details 
FOR EACH ROW 
insert into archives values 

You can use OLD keyword to fetch values from the deleted record. 您可以使用OLD关键字从已删除的记录中获取值。
But you have to specify column names with the insert statement. 但是您必须使用insert语句指定列名称。

Example : 范例

delimiter //

drop trigger if exists member_details_adel //

create trigger member_details_adel 
       after delete on member_details 
       for each row 
         insert 
           into archives( col1, col2, colx ) 
         values( old.col1, old.col2, old.colx );

//

delimiter ;

Change and extend column names as per your table needs. 根据表需要更改和扩展列名称。

Update 1 : 更新1

There was syntax error in your tried trigger body. 您尝试过的触发器主体中存在语法错误。
Corrected one is as follows: 更正后的内容如下:

delimiter $$ 
create trigger member_details_adel 
       after delete on member_details 
       for each row 
         insert 
           into archives (title,  name,  name of spouse,  
                          level of education,  address,  
                          phone number,  occupation,  
                          date joined,  emergency contact)
         values (old.title,  old.name, old.`name of spouse`, 
                 old.`level of education`, old.`address`, 
                 old.`phone number`, old.`occupation`, 
                 old.`date joined`, old.`emergency contact`); 
$$
delimiter ;

This is how you can do it, lets say you have a table called test with the following data 这就是您的操作方式,可以说您有一个名为test的表,其中包含以下数据

mysql> select * from test ;
+----------------------+
| id                   |
+----------------------+
| 10                   |
| 20                   |
| 30                   |
+----------------------+

You have another table called test1 as no data currently 您还有另一个名为test1的表,因为当前没有数据

mysql> select * from test1 ;
Empty set (0.00 sec)

Now lets write a trigger so that when there is a delete on the test table the deleted record gets added to the test1 table. 现在让我们编写一个触发器,以便在测试表上进行删除时,将删除的记录添加到test1表中。 You need to use after delete trigger for this and the using old.col you can get the data 您需要为此使用after delete触发器after delete ,使用old.col可以获取数据

In this example I have only one column you can use old.col name to access any column on the table where the trigger is executing 在此示例中,我只有一列,您可以使用old.col名称访问执行触发器的表上的任何列

delimiter //
create trigger log_delete after delete on test
for each row
begin
  insert into test1 (id) values (old.id);
end ; //
delimiter ;

Now lets delete a record in test table as 现在让我们删除测试表中的记录为

mysql> delete from test where id = 10 ;
Query OK, 1 row affected,  (0.00 sec)

mysql> select * from test ;
+----------------------+
| id                   |
+----------------------+
| 20                   |
| 30                   |
+----------------------+
2 rows in set (0.00 sec)

mysql> select * from test1 ;
+----------------------+
| id                   |
+----------------------+
| 10                   |
+----------------------+
1 row in set (0.00 sec)

Now as you can see the deleted row has been added to the table test1. 现在,您可以看到已删除的行已添加到表test1中。

All you need to do is to set proper table names and the column names in the above trigger along with the trigger name you want to have 您需要做的就是在上述触发器中设置适当的表名称和列名称以及您想要的触发器名称

USE church; 
DELIMITER $$ 
CREATE TRIGGER member_details_ADEL AFTER DELETE ON member_details 
FOR EACH ROW 
begin
    insert into archives 
    (TITLE, NAME, `NAME OF SPOUSE`, `LEVEL OF EDUCATION`, ADDRESS, `PHONE NUMBER`, OCCUPATION, `DATE JOINED`, `EMERGENCY CONTACT`) 
    values 
    (old.TITLE, old.NAME, old.`NAME OF SPOUSE`, old.`LEVEL OF EDUCATION`, old.`ADDRESS`, old.`PHONE NUMBER`,old.`OCCUPATION`, old.`DATE JOINED`, old.`EMERGENCY CONTACT`); 
END ; $$
delimiter ;

Make sure to backtick the column names when there is a space in between or in case using a reserve keyword. 如果之间有空格,或者使用reserve关键字,请确保对列名进行反引号。

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

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