如果删除,触发将行插入另一个 table
Trigger to insert row into another table if deleted
我正在尝试创建一个触发器,将已删除的一行复制到另一行 table。到目前为止,当我删除一行时,我只是将整个第一个 table 复制到第二个中,不是很有用。
表 1 是 comment_id、file_id、user_id、comment_text、comment_datetime 和父级
的注释
表 2 是 comment_log,删除了_comment_id、file_id、user_id、comment_text、comment_datetime 和 comment_deletion_datetime。
所以我只想将已被用户、版主或管理员删除的评论存储在 comment_log。
INSERT INTO comment_log(deleted_comment_id, file_id, user_id, comment_text,comment_datetime, comment_deletion_datetime)
SELECT comment.comment_id, file_id, user_id, comment_text, comment_datetime, CURRENT_TIMESTAMP
FROM comment
这就是我到目前为止所取得的进展,我已经尝试过像后面的 WHERE 之类的东西,但我不知道该放什么。 old.comment_id 应该给我旧的 id,但我不知道如何从评论 table.
中获取具有该 id 的评论
被删除行的列在触发器中可用 OLD.*
,所以我会这样做:
INSERT INTO comment_log
SET deleted_comment_id = OLD.comment_id,
file_id = OLD.file_id,
user_id = OLD.user_id,
comment_text = OLD.comment_text,
comment_datetime = OLD.comment_datetime,
comment_deletion_datetime = CURRENT_TIMESTAMP;
我正在尝试创建一个触发器,将已删除的一行复制到另一行 table。到目前为止,当我删除一行时,我只是将整个第一个 table 复制到第二个中,不是很有用。
表 1 是 comment_id、file_id、user_id、comment_text、comment_datetime 和父级
的注释表 2 是 comment_log,删除了_comment_id、file_id、user_id、comment_text、comment_datetime 和 comment_deletion_datetime。
所以我只想将已被用户、版主或管理员删除的评论存储在 comment_log。
INSERT INTO comment_log(deleted_comment_id, file_id, user_id, comment_text,comment_datetime, comment_deletion_datetime)
SELECT comment.comment_id, file_id, user_id, comment_text, comment_datetime, CURRENT_TIMESTAMP
FROM comment
这就是我到目前为止所取得的进展,我已经尝试过像后面的 WHERE 之类的东西,但我不知道该放什么。 old.comment_id 应该给我旧的 id,但我不知道如何从评论 table.
中获取具有该 id 的评论被删除行的列在触发器中可用 OLD.*
,所以我会这样做:
INSERT INTO comment_log
SET deleted_comment_id = OLD.comment_id,
file_id = OLD.file_id,
user_id = OLD.user_id,
comment_text = OLD.comment_text,
comment_datetime = OLD.comment_datetime,
comment_deletion_datetime = CURRENT_TIMESTAMP;