我有3张桌子: -
customer(customer_id)
sales(sales_id, customer_id)
salesdetails(salesdetails_id, sales_id)
如何在一个查询中删除客户以及其他2个相关表格数据?
我坚持到这里: -
DELETE FROM customer, sales, salesdetails
USING customer, sales, salesdetails
WHERE customer.customer_id = sales.customer_id
AND sales.sales_id = salesdetails.sales_id
AND customer.customer_id = 'C0001';
注意:我正在使用和新的数据库。
答案 0 :(得分:0)
创建表格时,添加删除方法' cascade'。 例如:
客户:
CREATE TABLE customer(
customer_id integer primary key auto_increment
);
销售:
CREATE TABLE sales(
sales_id integer primary key auto_increment,
FOREIGN KEY (customer_id) REFERENCES customer(id)
ON DELETE CASCADE
);
这样,当您删除客户时,所有相关的销售也将被删除。