mysql - 即使有使用键列的 where 子句,删除也会失败

标签 mysql sql mysql-workbench

我是mySQL的新手,在学习连接时,我尝试使用左连接、右连接和完全外连接进行完全连接。然后我意识到我的原始表之一(表 t2)有一个空行。

当尝试删除它时,我收到错误:错误代码:1175。您正在使用安全更新模式,并且尝试更新没有使用 KEY 列的 WHERE 的表要禁用安全模式,请在首选项中切换选项 -> SQL 编辑器并重新连接。

我运行的代码是:

create table t1( c1 integer, c2 integer, c3  varchar (10));
create table t2( c1 integer, c2 integer, c3 varchar(10));

insert into t1 values ( 1, 2, "foo"), (2,3, "bar"),(3,8,"random");
insert into t2 values ( 1, 4, "jack"), (2,6,"jill"), (4,9,"hill");
alter table t2 add primary key (c1);
update  t2 set c1 = 3 where c1 = 4; 
delete from t2 where c1 is null;
select * from t1;
select * from t2;
select * from t1 left join t2 on t1.c1=t2.c1 
union
select * from t1 right join t2 on t1.c1=t2.c1;

以及 MySQL Workbench v8.0 中没有

的输出
 delete from t2 where c1 is null;

是这样的:

t1
+------+------+--------+
| c1   | c2   | c3     |
+------+------+--------+
|    1 |    2 | foo    |
|    2 |    3 | bar    |
|    3 |    8 | random |
+------+------+--------+

t2
+----+------+------+
| c1 | c2   | c3   |
+----+------+------+
|  1 |    4 | jack |
|  2 |    6 | jill |
|  3 |    9 | hill |
|NULL| NULL | NULL |
+----+------+------+

t1 union t2
+------+------+--------+------+------+------+
| c1   | c2   | c3     | c1   | c2   | c3   |
+------+------+--------+------+------+------+
|    1 |    2 | foo    |    1 |    4 | jack |
|    2 |    3 | bar    |    2 |    6 | jill |
|    3 |    8 | random |    3 |    9 | hill |
+------+------+--------+------+------+------+

这是一个错误还是我的代码有问题?我该如何解决? 感谢所有意见。谢谢

更新:当我使用 MySQL 命令行时,我没有看到空行,但当我在工作台中运行脚本时我看到它。我还根据答案更新了我的代码,但目前我无法添加图片,因为我是新人,我不允许添加图片:-(,下面是新代码。

create table t1( c1 integer, c2 integer, c3  varchar (10), primary key(c1));
create table t2( c1 integer, c2 integer, c3 varchar(10),primary key (c1));

insert into t1 values ( 1, 2, "foo"), (2,3, "bar"),(3,8,"random");
insert into t2 values ( 1, 4, "jack"), (2,6,"jill"), (4,9,"hill");
/*alter table t2 add primary key (c1);*/
update  t2 set c1 = 3 where c1 = 4; 
delete from t2 where c1 is null;
select * from t1;
select * from t2;
select * from t1 left join t2 on t1.c1=t2.c1 
union
select * from t1 right join t2 on t1.c1=t2.c1;

最佳答案

只需在工作台查询编辑器中的代码下方运行查询之前使用

SET SQL_SAFE_UPDATES = 0;

关于mysql - 即使有使用键列的 where 子句,删除也会失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51587896/

相关文章:

c# - mysql-c# 连接未完成

java - 在 MySQL 中旋转一个表

Mysql 安装程序显示错误 : Memoy could not be written

python - MySql Python 连接

php - AS 或不 AS,查询

java - 使用 Hibernate 在外键字段中插入空值

sql - 选择子表的前两行

java - 将Java连接到MySQL数据库

Mysql查询替换路径中的未知数字

php - SQL BETWEEN 语句不返回任何结果?