[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>mysql 事务中如果有sql语句出错,会导致自动回滚吗?
事务,我们都知道具有原子性,操作要么全部成功,要么全部失败。但是有些人可能会对这句话产生误解。
我们先准备一张表,来进行测试
CREATE TABLE `name` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT
'ID', `name` varchar(32) DEFAULT '' COMMENT '名称', PRIMARY KEY (`id`) ) ENGINE=
InnoDB DEFAULT CHARSET=utf8mb4;
一、如果事务中,有某条sql语句执行时报错了,我们没有手动的commit,那整个事务会自动回滚吗?
答案:会。
当我们直接把窗口关掉,新开窗口再去查询表name时,表中没有第一次插入的记录,说明整个事务被回滚了。
二、如果事务中,有某条sql语句执行时报错了,我们手动的commit,那整个事务还会回滚吗?
答案:不会。
事务中如果有sql执行错误,但是我们commit了,mysql仍然会执行成功的语句,并不会把整个事务自动回滚。
三、如果事务中,有某条sql语句执行时报错了,我们重新又开启一个事务,那之前事务还会回滚吗?
答案:不会。
我们在该会话中,重新用 begin;
开启了一个新事务,新开的事务会将该会话中未提交的事务提交(相当于commit),所以上一个事务被提交了,导致语句一记录写入表中。