我正在学习 SQL 触发器,并阅读了一些关于如何在删除表 A 中的行时删除表 B 上的行的教程。
我有以下触发器:
CREATE DEFINER=`root`@`localhost` TRIGGER `testDB`.`test_BEFORE_DELETE`
BEFORE DELETE ON `testDBtable` FOR EACH ROW
BEGIN
Delete from testDBtable2 where id = (select id from deleted);
END
testDBtable 和 testDBtable2 都有 2 列,id 和 name。
当我从 testDB 中删除一行时,收到以下错误:
Error Code: 1146. Table 'testDB.deleted' doesn't exist 0.000 sec
我正在使用 MYSQLWorkBench。
最佳答案
我认为 OLD.id
是您需要的,而不是 select id from returned
。
完整查询:
CREATE DEFINER=`root`@`localhost` TRIGGER `testDB`.`test_BEFORE_DELETE`
BEFORE DELETE ON `testDBtable` FOR EACH ROW
BEGIN
Delete from testDBtable2 where id = OLD.id;
END
关于mysql - 触发: DELETED table does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29641418/