我正在学习MySQL事务。我已经搜索了这个答案,但他们似乎都使用PHP来做我想做的工作。下面是我想要做的例子:如何判断mysql插入是否成功
- 开始事务
- 更新表1
- 插入到表2
- 如果插入成功, 一个。然后插入到Table3中并提交。 b。否则回滚事务。
我不明白如何以编程方式确定在步骤3插入是否成功。当然,我可以查询表格并查看,但我认为有一些方法可以使用返回值,但似乎只有在我使用PHP来执行事务时才有效。
这是我想要的代码块 - 这是行不通的:
begin;
start transaction;
-- attempt to reduce inventory
update store_inventory set item_qty = item_qty - 2 where id = 1;
update store_inventory set item_qty = item_qty -1 where id = 5;
-- insert the order record and check whether it succeded
insert into store_orders (purchaser_name, purchase_date)
values ('test user', now());
-- if successful, do final insert and commit
if Row_Count() > 0 Then
insert into store_inventory (order_id, inventory_id, item_qty)
values (1, 1, 2),
(1, 2, 1);
commit;
else -- otherwise rollback
rollback;
end if;
end;
您使用什么语言和适配器来尝试和执行这些操作? – 2013-04-07 04:16:20
我正在使用MySQL Workbench查询编辑器。 – 2013-04-07 04:22:10
因此,您正试图查看插入是否在以下查询中成功? – 2013-04-07 04:23:07