无法修改映射到非密钥保留表的列时更新表

时间:2014-01-07 16:22:17

标签: sql oracle oracle11g

----- s.op_id= ------
| S |----------| OP |
----- op.op_id ------
                  |
                  | op.op_id = j.op_id
                  |
                -----
                | J |
                -----

oracle数据库有三个表journeyop_profilespecial

journey有两列journey_id (主键)op_idop_profile有主键op_idspecialop_id上有一个op_profile外键,另一列s_id是表主键。

我尝试使用以下sql usin sql developer将所有op_id列更新为等于journey_id表的journey列:

UPDATE (SELECT special.op_id, journey.journey_id, op_profile.op_id AS op
        FROM special, journey, op_profile
        WHERE special.op_id = journey.op_id AND journey.op_id = op_profile.op_id)
SET op_id = journey_id, op = journey_id;

会出现以下错误

SQL Error: ORA-01779: cannot modify a column which maps to a non key-preserved table
01779. 00000 -  "cannot modify a column which maps to a non key-preserved table"
*Cause:    An attempt was made to insert or update columns of a join view which
           map to a non-key-preserved table.
*Action:   Modify the underlying base tables directly.

似乎说这个操作由于每个表的键而失败。

这可能是这样还是另一种?

2 个答案:

答案 0 :(得分:2)

是的,可以使用两个更新语句:

update s
    set op_id = (select j.journey_id
                 from j
                 where j.op_id = s.op_id
                );

update op
    set op_id = (select j.journey_id
                 from j
                 where j.op_id = op.op_id
                );

答案 1 :(得分:0)

在以下内容上添加索引(如果之前不可用)

  1. special.op_id
  2. journey.op_id
  3. op_profile.op_id
  4. 如果无法获取一组稳定的更新行,则会出现此问题。