使用一个insert-SQL更新两个表

时间:2014-05-15 19:47:03

标签: mysql sql

是否可以对一个查询进行以下两个查询?

update customers set customer_name = 'John'  where customer_id=1;

update purchases set state='Accepted'  where customer_id=1;

customer(表)

customer_id(PK)
customer_name

purchases(表)

customer_id(FK)
product
state

由于

1 个答案:

答案 0 :(得分:0)

您可以在一个交易中执行它们:

START TRANSACTION;
update customers set customer_name = 'John'  where customer_id=1;
update purchases set state='Accepted'  where customer_id=1;
COMMIT;

如果事务中的某些内容失败,则所有更改都将回滚