码迷,mamicode.com
首页 > 数据库 > 详细

MySQL、You are using safe update mode

时间:2015-04-30 14:01:37      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

MySQL默认模式是sql_safe_updates=1的。在这个模式下不管你是update还是delete一行where 条件都要指定主键。如果where条件只出现的不是主键就会出错。

例子:

  set sql_safe_updates =1;--开始安全更新、这个是我这个例子的前提。

  --第一步:建立两个表

  create table t(x int,y int);

  create table t2(x int primary key,y int);

  --第二步:向表中插入数据

  insert into t(x,y) values(1,1),(2,2),(3,3);

  insert into t2(x,y) values(1,1),(2,2),(3,3);

  --第三步:测试安全更新模式对update delete 的影响。

  update t set y =100 where x=1;--这个会出报错、You are using safe update mode......

  update t2 set y=100 where x=1;--这个就没有事。因为x这表t2中是主键、而在表t中不是。

  --解决方案:

  为了对t表的更新也可以完成、我们就要在update 语句前加上一句。

  set sql_safe_updates =0;--禁用这个模式就可以了

MySQL、You are using safe update mode

标签:

原文地址:http://www.cnblogs.com/JiangLe/p/4468604.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!