我有树表news_files
,files
和news
id name
1 valodik
2 gugushik
news_files
id news_id file_id
1 1 5
2 1 6
3 2 9
files
id file_path
5 /opt/storage/
6 /opt/storage/
9 /opt/storage/
trigger
在news_files
上创建了DELIMITER //
CREATE TRIGGER after_delete_files AFTER DELETE ON news_files
FOR EACH ROW
BEGIN
DELETE FROM files WHERE id=old.file_id;
END;//
DELIMITER ;
news
我要删除表files
中的数据,它会自动删除news_files.news_id
表中的行,news.id
被约束级联到delete from news where id = 1;
例子
connect()
自动删除表文件中的两行。