我正在使用 mySql 数据库,我有三个表产品,生产者和供应商
我想通过提供 product_id 来删除所有表中的数据 并按特定顺序删除数据,首先,产品应删除,然后生产者,然后供应商。
表示我想要合并以下三个查询
delete from product where product_id = 2
delete from producer where product_id = 2
delete from supplier where product_id =2
答案 0 :(得分:0)
一种选择是在product_id
和Producer
表中使Supplier
成为外键。此外键将引用producer_id
表中的Product
主键。然后,删除产品记录将自动删除其他两个表中的链接记录。
例如,Producer
表可能如下所示:
CREATE TABLE Producer (
product_id INT NOT NULL,
...
FOREIGN KEY (product_id) REFERENCES Product (product_id) ON DELETE CASCADE
)
答案 1 :(得分:0)
实现此目的的一种方法是使用多重删除
DELETE product, produce, supplier FROM product, producer, supplier WHERE product.product_id = 2 and supplier.product_id = 2 and producer.product_id = 2
答案 2 :(得分:0)
没有外键和删除级联设置,最好的选择是通过在每个语句的末尾放置一个分号将它们组合成一个语句。
delete from product where product_id = 2; delete from producer where product_id = 2; delete from supplier where product_id =2;