现在有一个名为comments的数据库表。评论可以是文章、文件或评论的子评论。 father_table
指示此注解附加到哪个类别。 father_id
指示文章、文件或注解的id。
create table comment(
id int(11) not null auto_increment,
user_id int(11) not null ,
father_id int(11) not null ,
father_table enum('file','article','comment') not null ,
create_date timestamp not null default CURRENT_TIMESTAMP,
content text not null ,
primary key (id),
foreign key (user_id) references user (id) on delete cascade
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
我想在删除每个评论之后删除这个评论的所有子评论。所以我写了一个触发器。
create trigger foreign_key_cascade_delete_comment
after delete on comment
for each row begin
delete from comment where father_table='comment' and father_id=OLD.id;
delete from attitude where father_table='comment' and father_id=OLD.id;
delete from collect where father_table='comment' and father_id=OLD.id;
end;
然后,我从 article
表和 file
table和它工作得很好。但是当我从 comment
出错了。错误信息在这里。
[HY000][1442] Can't update table 'comment' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
我想知道如何确保所有子评论被删除,其中一个评论被删除。如何使用触发器来实现这个目标?(数据库是mysql 8)
暂无答案!
目前还没有任何答案,快来回答吧!