MySQL触发器:删除后从表中删除

发布于 2025-02-10 16:44:47 字数 1147 浏览 3 评论 0原文

范围:两个表。当创建新的赞助人时,他们会有一些有关将它们存储在第二个表中的信息(这也是使用触发器完成的,它可以按预期工作)。这是我的表结构和关系的一个例子。

表1-> 顾客

+-----+---------+-----+
+  id +   name  + val +
+=====+=========+=====+
+  37 +  george +  x  +
+-----+---------+-----+
+  38 +  sally  +  y  +
+-----+---------+-----+

表2-> patron_info

+----+-----+----------+
+ id + pid +   name   +
+----+-----+----------+
+  1 +  37 +  george  +
+----+-----+----------+
+  2 +  38 +  sally   +
+----+-----+----------+

管理员可以管理顾客。当他们选择删除赞助人时,将从表1 顾客中删除顾客。此时,表2 patron_info什么都没发生。

我只是尝试创建一个从表2中删除的触发器,当表1删除项目时。这是我尝试过的...

最初,如果存在(只是为了清除空气),我尝试将触发器放下...

DROP TRIGGER IF EXISTS log_patron_delete;

然后我尝试创建触发器...此后

CREATE TRIGGER log_patron_delete AFTER DELETE on patrons
FOR EACH ROW
BEGIN
DELETE FROM patron_info
    WHERE patron_info.pid = patrons.id
END

,我会得到语法错误1046:在第6行上查看近端的语法。我不知道这一点是什么错误。我尝试了几种不同的变体。另外,我需要在这里使用定界符吗?

谁能帮助恢复我的理智?

Scope: Two tables. When a new patron is created, they have some information about them stored into a 2nd table (This was done using a trigger as well, it works as expected). Here's an example of my table structure and relationship.

Table 1-> patrons

+-----+---------+-----+
+  id +   name  + val +
+=====+=========+=====+
+  37 +  george +  x  +
+-----+---------+-----+
+  38 +  sally  +  y  +
+-----+---------+-----+

Table 2 -> patron_info

+----+-----+----------+
+ id + pid +   name   +
+----+-----+----------+
+  1 +  37 +  george  +
+----+-----+----------+
+  2 +  38 +  sally   +
+----+-----+----------+

The administrator can manage the patrons. When they choose to remove a patron, the patron is removed from the table 1 patrons. At this point, nothing happens to table 2 patron_info.

I'm simply trying to create a trigger to delete from table 2, when table 1 has an item deleted. Here's what I've tried...

Initially, I try to drop the trigger if it exists (just to clear the air)...

DROP TRIGGER IF EXISTS log_patron_delete;

Then I try to create the trigger afterwards...

CREATE TRIGGER log_patron_delete AFTER DELETE on patrons
FOR EACH ROW
BEGIN
DELETE FROM patron_info
    WHERE patron_info.pid = patrons.id
END

At this point, I get a syntax error 1046: Check syntax near END on line 6. I don't know what the error is at this point. I've tried several different variations. Also, am I required to use a delimiter here?

Can anyone help restore my sanity?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南冥有猫 2025-02-17 16:44:47

我认为触发代码中存在错误。
当您想使用已删除的赞助人ID删除所有行时,您必须使用 old.id.id (否则它将删除其他ID)

作为新的触发器尝试:

CREATE TRIGGER log_patron_delete AFTER DELETE on patrons
FOR EACH ROW
BEGIN
DELETE FROM patron_info
    WHERE patron_info.pid = old.id;
END

不要忘记“”; “ 在删除查询上。
另外,如果您要在“控制台”窗口中输入触发代码,请使用定界符。

I think there is an error in the trigger code.
As you want to delete all rows with the deleted patron ID, you have to use old.id (Otherwise it would delete other IDs)

Try this as the new trigger:

CREATE TRIGGER log_patron_delete AFTER DELETE on patrons
FOR EACH ROW
BEGIN
DELETE FROM patron_info
    WHERE patron_info.pid = old.id;
END

Dont forget the ";" on the delete query.
Also if you are entering the TRIGGER code in the console window, make use of the delimiters also.

灯角 2025-02-17 16:44:47

为什么不在cascade上删除外键patron_info .pid?

Why not set ON CASCADE DELETE on Foreign Key patron_info.pid?

哎呦我呸! 2025-02-17 16:44:47
create trigger doct_trigger
after delete on doctor
for each row
delete from patient where patient.PrimaryDoctor_SSN=doctor.SSN ;
create trigger doct_trigger
after delete on doctor
for each row
delete from patient where patient.PrimaryDoctor_SSN=doctor.SSN ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文