使用外键删除多个表中的条目

发布于 2024-12-26 11:33:32 字数 734 浏览 2 评论 0原文

以下设置将:

  • 允许我删除现有条目
  • 阻止我在 products 中创建新条目

这可能是因为之前在 products 中创建了条目categories_products。但在知道产品 ID 之前,我无法在 categories_products 中创建条目。

你会怎么办?

ALTER TABLE products
ADD CONSTRAINT FK_products
FOREIGN KEY (id) REFERENCES categories_products(product_id)
ON UPDATE CASCADE
ON DELETE CASCADE;

表格

categories:
id, name

categories_products:
category_id, product_id

products:
id, name

创建每个条目的顺序

  1. 在类别中创建条目
  2. 在产品中创建条目
  3. 使用 1. 和 2. 中的 ID 创建类别之间的关系< /code> 和 产品

The following setup will:

  • Allow me to delete existing entries
  • Prevent me from creating new entries in products

This probably happens because entries gets created in products before categories_products. But I cannot create entries in categories_products before I know the product ID.

What would you do?

ALTER TABLE products
ADD CONSTRAINT FK_products
FOREIGN KEY (id) REFERENCES categories_products(product_id)
ON UPDATE CASCADE
ON DELETE CASCADE;

Tables

categories:
id, name

categories_products:
category_id, product_id

products:
id, name

Which order each entry gets created

  1. Create an entry in categories
  2. Create an entry in product
  3. Using the IDs from 1. and 2. to create a relationship between categories and products

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

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

发布评论

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

评论(2

沙沙粒小 2025-01-02 11:33:32

我认为你的约束是倒退的。该外键应该位于 categories_products 表中并引用 products 表,而不是相反:

ALTER TABLE categories_products
ADD CONSTRAINT FK_products
FOREIGN KEY (product_id) REFERENCES products(id)
ON DELETE CASCADE;

I think you have your constraint backwards. That foreign key should be on the categories_products table and referencing the products table, not the other way around:

ALTER TABLE categories_products
ADD CONSTRAINT FK_products
FOREIGN KEY (product_id) REFERENCES products(id)
ON DELETE CASCADE;
↙厌世 2025-01-02 11:33:32

您有一个多对多关系:

  • 每个产品可以有多个类别。
  • 每个类别可以有很多产品。

因此,您应该添加两个外键来支持这种类型的关系。这些外来必须添加到categories_products表中:

ALTER TABLE categories_products
  ADD CONSTRAINT fk_products FOREIGN KEY (product_id)
    REFERENCES products(id),
  ADD CONSTRAINT fk_categories FOREIGN KEY (category_id)
    REFERENCES categories(id);

然后,要删除类别和相关产品,请使用如下查询:

SET FOREIGN_KEY_CHECKS = 0;

DELETE c, cp, p
  FROM category c
    JOIN categories_products cp
      ON cp.category_id = c.id
    JOIN products p
      ON p.id = cp.product_id
WHERE
  c.id = 100;

SET FOREIGN_KEY_CHECKS = 1;

您还可能需要添加ON DELETE CASCADE子句,它将帮助您从父表中删除行并自动删除子表中的匹配行。例如,下一个查询将自动删除 categories_products 表中的相关记录:

DELETE FROM category id = 100;

You have a many-to-many relation:

  • Each product can have many categories.
  • Each category can have many products.

So, you should add two foreing keys to support this type of relation. These foreign have to be added to the categories_products table:

ALTER TABLE categories_products
  ADD CONSTRAINT fk_products FOREIGN KEY (product_id)
    REFERENCES products(id),
  ADD CONSTRAINT fk_categories FOREIGN KEY (category_id)
    REFERENCES categories(id);

Then, to delete category and related products use a query like this:

SET FOREIGN_KEY_CHECKS = 0;

DELETE c, cp, p
  FROM category c
    JOIN categories_products cp
      ON cp.category_id = c.id
    JOIN products p
      ON p.id = cp.product_id
WHERE
  c.id = 100;

SET FOREIGN_KEY_CHECKS = 1;

You also may want to add ON DELETE CASCADE clause, it will help you to delete the row from the parent table and automatically delete the matching rows in the child table. For example, next query will remove automatically related records in categories_products table:

DELETE FROM category id = 100;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文