首页 > 数据库技术 > 详细

危险的 SQL

时间:2015-03-10 22:51:14      阅读:325      评论:0      收藏:0      [点我收藏+]

 

      看下这个 SQL , 有什么问题 ?

<update id="update" parameterType="CreativeGroupDO">
        update dsp_creative_group set gmt_modified = now()
        <if test="title != null">
            ,title = #{title,jdbcType=VARCHAR}
        </if>
        <if test="clickUrl != null">
            ,click_url = #{clickUrl,jdbcType=VARCHAR}
        </if>
        <where>
            <if test="creativeGroupId != null">
                and creative_group_id = #{creativeGroupId,jdbcType=BIGINT}
            </if>
        </where>
    </update>

        

      答案在下面已经用灰白色字体隐去。 请用鼠标滑过查看。

      咋看上去, 没啥问题;  可是, 如果 creativeGroupId = null 呢? 将会更新所有的 dsp_creative_group 记录的 title, clickUrl 为同一个值, 造成数据丢失。 你永远不知道应用程序会在什么时候将 creative_group_id 置为 null , 这几乎是不可控制的。  对于线上系统来说,一旦这样的事情发生, 绝对是灾难级的故障。 教训是: 永远、绝对不要在 update 的 where 字句中增加 if 条件!

 

危险的 SQL

原文:http://www.cnblogs.com/lovesqcc/p/4328408.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!