您的位置:首页 > 其它

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

2014-02-24 14:12 477 查看
当更新一个子查询的时候,会出现这个错误。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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐