ORA-01779: cannot modify a column which maps to a non-key-preserved table分析
2012-06-06 13:53
531 查看
Oracle中试图对一个子查询进行更新时可能会出现ORA-01779错误。该错误的内容为:
ORA-01779: cannot modify a column which maps to a non-key-preserved table
例如,使用以下的更新查询就会出现该错误。
CREATE TABLE test1 ( id integer primary key, num integer );
INSERT INTO test1 VALUES (1,0);
INSERT INTO test1 VALUES (2,0);
INSERT INTO test1 VALUES (3,0);
INSERT INTO test1 VALUES (4,0);
CREATE TABLE test2 ( id integer, num integer, upd integer );
INSERT INTO test2 VALUES (1,10, 0);
INSERT INTO test2 VALUES (2,20, 1);
UPDATE ( SELECT t1.id id1, t1.num num1, t2.id id2, t2.num num2
FROM test1 t1, test2 t2 WHERE t1.id=t2.id AND t2.upd=1 )
SET num1=num2; ORA-01779: cannot modify a column which maps to a non-key-preserved table
这个错误的意思是,子查询的结果中,更新数据源(test2)的内容不唯一,导致被更新对象(test1)中的一行可能对应数据源(test2)中的多行。 本例中,test2表的id不唯一,因此test2表中可能存在id相同但是num不相同的数据,这种数据是无法用来更新 test1 的。
解决方法就是保证数据源的唯一性,例如本例中可以为test2.id创建一个唯一索引:
CREATE UNIQUE INDEX test2_idx_001 ON test2 (id);
之后上面的更新就可以执行了。
另外也可以强制 Oracle 执行,方法是加上 BYPASS_UJVC 注释。
UPDATE
( SELECT /*+ BYPASS_UJVC */ t1.id id1, t1.num num1, t2.id id2, t2.num num2
FROM test1 t1, test2 t2
WHERE t1.id=t2.id AND t2.upd=1 )
SET num1=num2;
BYPASS_UJVC的作用是跳过Oracle的键检查。 这样虽然能够执行了,但是如果test2中存在不唯一的数据,test1就会被更新多次而导致意想不到的结果。
ORA-01779: cannot modify a column which maps to a non-key-preserved table
例如,使用以下的更新查询就会出现该错误。
CREATE TABLE test1 ( id integer primary key, num integer );
INSERT INTO test1 VALUES (1,0);
INSERT INTO test1 VALUES (2,0);
INSERT INTO test1 VALUES (3,0);
INSERT INTO test1 VALUES (4,0);
CREATE TABLE test2 ( id integer, num integer, upd integer );
INSERT INTO test2 VALUES (1,10, 0);
INSERT INTO test2 VALUES (2,20, 1);
UPDATE ( SELECT t1.id id1, t1.num num1, t2.id id2, t2.num num2
FROM test1 t1, test2 t2 WHERE t1.id=t2.id AND t2.upd=1 )
SET num1=num2; ORA-01779: cannot modify a column which maps to a non-key-preserved table
这个错误的意思是,子查询的结果中,更新数据源(test2)的内容不唯一,导致被更新对象(test1)中的一行可能对应数据源(test2)中的多行。 本例中,test2表的id不唯一,因此test2表中可能存在id相同但是num不相同的数据,这种数据是无法用来更新 test1 的。
解决方法就是保证数据源的唯一性,例如本例中可以为test2.id创建一个唯一索引:
CREATE UNIQUE INDEX test2_idx_001 ON test2 (id);
之后上面的更新就可以执行了。
另外也可以强制 Oracle 执行,方法是加上 BYPASS_UJVC 注释。
UPDATE
( SELECT /*+ BYPASS_UJVC */ t1.id id1, t1.num num1, t2.id id2, t2.num num2
FROM test1 t1, test2 t2
WHERE t1.id=t2.id AND t2.upd=1 )
SET num1=num2;
BYPASS_UJVC的作用是跳过Oracle的键检查。 这样虽然能够执行了,但是如果test2中存在不唯一的数据,test1就会被更新多次而导致意想不到的结果。
相关文章推荐
- ORA-01779: cannot modify a column which maps to a non-key-preserved table
- 解决错误ORA-01779: cannot modify a column which maps to a non-key-preserved table
- ORA-01779: cannot modify a column which maps to a non key-preserved table
- ORA-01779: cannot modify a column which maps to a non key-preserved table
- ORA-01779: cannot modify a column which maps to a non-key-preserved table
- ora-01779 non-key-preserved table错误及/*+ BYPASS_UJVC*/ 的使用技巧
- How to modify non-customizable entity in MS Dynamics CRM 4.0 (Add Product price column to Price List Item view)
- 自学JAVA之Cannot make a static reference to the non-static method分析
- 异常分析:Unable to execute dex: Cannot merge new index 71813 into a non-jumbo instruction!
- ORA-01445: Cannot Select ROWID from a Join View without a Key-Preserved Table
- Cannot convert value '0000-00-00 00:00:00' from column 1 to TIMESTAMP分析及解决办法
- ora-01552 :cannot use system rollback segment for non-system tablespace 解决
- Cannot refer to a non-final variable xxxxx inside an inner class defined in a different method
- Failed to read row 0, column -1 from a CursorWindow which h
- Java中的报错:Cannot make a static reference to the non-static field
- ORA-12032: cannot use rowid column from materialized view log on "DCS_BIZ"."TMP_12"
- Java 的静态函数 及Cannot make a static reference to the non-static method的解决
- ORA-04030: out of process memory when trying to allocate 64544 bytes (sort subheap,sort key)
- Cannot insert explicit value for identity column in table ‘table’ when IDENTITY_INSERT is set to OFF
- java.sql.SQLException: Cannot convert value '0000-00-00 00:00:00' from column aa to TIMESTAMP