简体   繁体   中英

How to insert a deleted row in another table

I have a table users . I want whenever I (or a script) deletes one of this users, that the deleted row is inserted into another user_deleted table.

The users_deleted tables is exactly the same as the users table + a datetime column with NOW() for the deletion time.

For that, i've found the MySQL trigger functionality very handy, sadly it doesn't work like excepted.

在此处输入图像描述

Thing is, when there is a deletion on the users , in users_delete a blank new row is inserted.

What may be the problem here?

You should be selecting old values that should be inserted to another table inside the trigger code.

INSERT INTO users_deleted (datetime, id, name)
VALUES (NOW(), OLD.id, OLD.name);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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