我想要一个触发器从 MySQL 中的 2 个表中删除

发布于 2024-10-14 03:19:50 字数 528 浏览 4 评论 0原文

我有 3 个 MySQL 表(foodappleorange)。

我想从以下位置删除行:

apple(idapple, iduser, name) 
orange(idornge, iduser, name)

使用一个触发器删除 food(iduser, name) 中的一行时?

到目前为止,这是我的触发器:

  CREATE TRIGGER `food_before_delete`

    AFTER DELETE ON `food` 
    FOR EACH ROW 

      DELETE FROM apple, orange 
      WHERE 
      apple.iduser=OLD.iduser and 
      orange.iduser=OLD.iduser

但它不会编译。如何制作一个同时从两个表中删除的触发器?

I have 3 MySQL tables (food, apple, and orange).

I want to delete rows from:

apple(idapple, iduser, name) 
orange(idornge, iduser, name)

When deleting a row in food(iduser, name) using one trigger?

Here is my trigger so far:

  CREATE TRIGGER `food_before_delete`

    AFTER DELETE ON `food` 
    FOR EACH ROW 

      DELETE FROM apple, orange 
      WHERE 
      apple.iduser=OLD.iduser and 
      orange.iduser=OLD.iduser

But it won't compile. How can make a trigger that deletes from two tables at once?

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

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

发布评论

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

评论(5

白况 2024-10-21 03:19:50

使用触发器同时删除两个表:

触发器用于强制表中数据的完整性。您可以使用触发器一次从任意数量的表中删除。

在初始化触发器之前,我们需要暂时更改mysql分隔符运算符,因为触发器使用分号;运算符来指定触发器内的多个sql命令。

第 1 步更改当前分隔符:

delimiter $

第 2 步创建触发器:

CREATE TRIGGER `blog_before_delete`     
  AFTER DELETE ON `blog`     
  FOR EACH ROW     
BEGIN
  DELETE FROM blog_tags where blogid = OLD.id;
  DELETE FROM blog_comments where blogid = OLD.id;
END
$

第 3 步恢复先前的分隔符:

delimiter ;

说明:

OLD 是一个内置关键字,指的是我们要删除的博客表行。每当我们删除博客表中的条目时,Mysql 就会运行触发器 blog_before_delete。如果触发器失败,则回滚删除。这有助于确保我们的数据库的原子性、一致性、隔离性和持久性

Delete from two tables at once with a Trigger:

Triggers are used to enforce data integrity in the tables. You can use triggers to delete from any number of tables at once.

Before initializing triggers we need to change the mysql delimiter operator temporarily because triggers use semicolon ; operator to specify multiple sql commands within the trigger.

Step 1 Change current delimiter:

delimiter $

Step 2 Create trigger:

CREATE TRIGGER `blog_before_delete`     
  AFTER DELETE ON `blog`     
  FOR EACH ROW     
BEGIN
  DELETE FROM blog_tags where blogid = OLD.id;
  DELETE FROM blog_comments where blogid = OLD.id;
END
$

Step 3 Restore previous delimiter:

delimiter ;

Explanation:

OLD is a builtin keyword and refers to the blog table row that we are deleting. Mysql runs the trigger blog_before_delete whenever we delete an entry in the blog table. I the trigger fails, then the delete is rolled back. This helps ensure Atomicity, Consistency, Isolation, and Durability in our database.

久而酒知 2024-10-21 03:19:50
CREATE TRIGGER `food_before_delete`     
AFTER DELETE ON `food`     
FOR EACH ROW     
begin
  DELETE FROM apple
  WHERE apple.iduser=NEW.iduser;

  DELETE FROM orange
  WHERE orange.iduser=NEW.iduser;
end

删除语句可能需要 OLD.iduser,但不支持 NEW.iduser。检查你的手册。

CREATE TRIGGER `food_before_delete`     
AFTER DELETE ON `food`     
FOR EACH ROW     
begin
  DELETE FROM apple
  WHERE apple.iduser=NEW.iduser;

  DELETE FROM orange
  WHERE orange.iduser=NEW.iduser;
end

The delete statements may require OLD.iduser and not support NEW.iduser. Check your manual.

小瓶盖 2024-10-21 03:19:50

也许更简单的东西?

DELETE f,a,o FROM
food AS f 
LEFT JOIN apple AS a USING (iduser)
LEFT JOIN orange AS o USING (iduser)
WHERE f.name = ...

无需触发器。

Something simpler maybe?

DELETE f,a,o FROM
food AS f 
LEFT JOIN apple AS a USING (iduser)
LEFT JOIN orange AS o USING (iduser)
WHERE f.name = ...

No trigger needed.

吻泪 2024-10-21 03:19:50

我忘记了触发器中的 BEGINEND 块。并且您必须按顺序从表中删除,如下所示:

CREATE TRIGGER `food_before_delete`     
  AFTER DELETE ON `food`     
  FOR EACH ROW     
BEGIN
  DELETE FROM apple
  WHERE apple.iduser=NEW.iduser;

  DELETE FROM orange
  WHERE orange.iduser=NEW.iduser; 
END

I forgot the BEGIN and END blocks in the trigger. And you have to delete from tables sequentially like this:

CREATE TRIGGER `food_before_delete`     
  AFTER DELETE ON `food`     
  FOR EACH ROW     
BEGIN
  DELETE FROM apple
  WHERE apple.iduser=NEW.iduser;

  DELETE FROM orange
  WHERE orange.iduser=NEW.iduser; 
END
肤浅与狂妄 2024-10-21 03:19:50

MySQL 演练如何制作一个触发器,用一个触发器删除两个表:

1.登录:

el@apollo:~$ mysql -u root -p
Enter password: 
mysql> use your_database;
Database changed

2.创建一些测试表并插入行:

mysql> create table derp(derp1 INT);
Query OK, 0 rows affected (0.02 sec)

mysql> create table foo(foo1 INT);
Query OK, 0 rows affected (0.02 sec)

mysql> create table bar(bar1 INT);
Query OK, 0 rows affected (0.02 sec)

mysql> insert into derp values (1);
Query OK, 1 row affected (0.01 sec)

mysql> insert into foo values (1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into bar values (1);
Query OK, 1 row affected (0.00 sec)

3.触发触发器:

mysql> delimiter //
mysql> create trigger delete_foo_and_bar_when_derp_is_deleted
    -> after delete on derp
    -> for each row
    -> begin
    -> delete from foo where foo1=OLD.derp1;
    -> delete from bar where bar1=OLD.derp1;
    -> end//
Query OK, 0 rows affected (0.02 sec)

4.查看表是否存在并且所有三个表均已填充。

mysql> delimiter ;
mysql> select * from derp;
+-------+
| derp1 |
+-------+
|     1 |
+-------+
1 row in set (0.00 sec)

mysql> select * from foo;
+------+
| foo1 |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

mysql> select * from bar;
+------+
| bar1 |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

5.从derp中删除

mysql> delete from derp where derp1 = 1;
Query OK, 1 row affected (0.01 sec)

6.看到 derp、foo 和 bar 现在都是空的:

mysql> select * from derp;
Empty set (0.00 sec)

mysql> select * from foo;
Empty set (0.00 sec)

mysql> select * from bar;
Empty set (0.00 sec)

MySQL walkthrough of making a trigger to delete two tables with one trigger:

1. Login:

el@apollo:~$ mysql -u root -p
Enter password: 
mysql> use your_database;
Database changed

2. Create some test tables and insert rows:

mysql> create table derp(derp1 INT);
Query OK, 0 rows affected (0.02 sec)

mysql> create table foo(foo1 INT);
Query OK, 0 rows affected (0.02 sec)

mysql> create table bar(bar1 INT);
Query OK, 0 rows affected (0.02 sec)

mysql> insert into derp values (1);
Query OK, 1 row affected (0.01 sec)

mysql> insert into foo values (1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into bar values (1);
Query OK, 1 row affected (0.00 sec)

3. Make the trigger:

mysql> delimiter //
mysql> create trigger delete_foo_and_bar_when_derp_is_deleted
    -> after delete on derp
    -> for each row
    -> begin
    -> delete from foo where foo1=OLD.derp1;
    -> delete from bar where bar1=OLD.derp1;
    -> end//
Query OK, 0 rows affected (0.02 sec)

4. See the tables exist and all three are populated.

mysql> delimiter ;
mysql> select * from derp;
+-------+
| derp1 |
+-------+
|     1 |
+-------+
1 row in set (0.00 sec)

mysql> select * from foo;
+------+
| foo1 |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

mysql> select * from bar;
+------+
| bar1 |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

5. Delete from derp

mysql> delete from derp where derp1 = 1;
Query OK, 1 row affected (0.01 sec)

6. See that derp, foo, and bar are empty now:

mysql> select * from derp;
Empty set (0.00 sec)

mysql> select * from foo;
Empty set (0.00 sec)

mysql> select * from bar;
Empty set (0.00 sec)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文