mysql中的配置项sql-mode

在mysql中,使用存储过程来实现购物,需要用到事务操作,start transaction ;
    当user的money不足以支付商品时,应当进行回滚。获取异常用    declare continue handler for SQLEXCEPTION set e=1;
        
        -- 产品表
        create table productn(
            pid int key auto_increment,
            num int unsigned,
            price float(7,2) default 0.00,
            title char(50)
        )//
        insert into productn(pid,num,price,title) value(1,5,50.00,'宝强牌帽子')//
        -- 用户表
        create table usern(
            uid int key auto_increment,
            username char(20) unique not null,
            password char(32) not null,
            money float(10,2) unsigned default 0.00
        )//
        insert into usern(uid,username,password,money) value(1,'xiaosong','123123',80.00)//

        create procedure buy(in pidn int,in uidn int,in numn int)
            begin
                declare jiage float(7,2) default 0.00;
                declare zongjia float(9,2) default 0.00;
                declare e tinyint(1) default 0;
                declare continue handler for SQLEXCEPTION set e=1;
                -- 获取价格
                select price into jiage from productn where pid=pidn;
                -- 算出总价
                set zongjia=jiage*numn;
                -- 开启事务
                start transaction;
                -- 扣款
                update usern set money=money-zongjia where uid=uidn;
                -- 出库
                update productn set num=num-numn where pid=pidn;
                -- 判断是否有异常
                if e=1 then
                    rollback;
                    select 0 as re;
                else
                    commit;
                    select 1 as re;
                end if;
            end//

    !!!!!!!
    然而在实际调用过程中, call buy(1,1,2)//  判断异常并没有起作用,购物操作仍然执行,用户的money被置为0。
    这样就需要更改mysql的配置文件 my.ini  中的配置项 sql-mode
        在phpstudy中该项只包含两个值 :sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
        最好把该项置成: sql-mode="ONLY_FULL_GROUP_BY,NO_AUTO_VALUE_ON_ZERO,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,PIPES_AS_CONCAT_,ANSI_QUOTES"

 

转载于:https://my.oschina.net/u/2965359/blog/760286

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值