ORA-01779: cannot modify a column which maps to a non key-preserved table

来源:互联网 发布:什么是软件过程规范 编辑:程序博客网 时间:2024/04/29 00:32

当更新一个子查询的时候,会出现这个错误。ORA-01779: cannot modify a column which maps to a non key-preserved table

下面先模拟一下这个错误:

SQL> select * from gw1;

        ID NAME
---------- ----------
         1 a
         2 b
         3 c

SQL> select * from gw2;

        ID  UPDATE_ID
---------- ----------
         1         11
         2         22
         3         33
         4         44

SQL> select gw1.id,gw2.update_id from gw1,gw2 where gw1.id=gw2.id;

        ID  UPDATE_ID
---------- ----------
         1         11
         2         22
         3         33

SQL> update (select gw1.id,gw2.update_id from gw1,gw2 where gw1.id=gw2.id) tmp set tmp.id=tmp.update_id;
update (select gw1.id,gw2.update_id from gw1,gw2 where gw1.id=gw2.id) tmp set tmp.id=tmp.update_id
                                                                              *
ERROR at line 1:
ORA-01779: cannot modify a column which maps to a non key-preserved table

产生这个错误的原因是因为gw2表的id不是唯一的,有可能gw1的一个id会对应多个gw2的id,这样就不知道怎么更新了。即使gw2的表中并不存在这样的记录。

解决方法:给gw2表的id列加上unique约束或设置为主键

SQL> alter table gw2 modify id unique;

Table altered.

SQL> update (select gw1.id,gw2.update_id from gw1,gw2 where gw1.id=gw2.id) tmp set tmp.id=tmp.update_id;

3 rows updated.

SQL> select * from gw1;

        ID NAME
---------- ----------
        11 a
        22 b
        33 c

如需转载,请注明出处:http://blog.csdn.net/nanaranran/article/details/19820425

0 0