简体   繁体   中英

Create table as Select, then Update statement with JOIN in Oracle

I'm using Oracle HR database. I was wondering why the following query isn't working:

create table ecopy 
as select *
from employees;

create table dcopy
as select *
from departments;

UPDATE (select d.location_id, e.salary 
from ecopy e inner join dcopy d 
on e.department_id=d.department_id)
set salary = salary+1 
where location_id = 1800

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

While the this one, on the origin tables is doing it's job:

UPDATE (select d.location_id, e.salary 
from employees e inner join departments d 
on e.department_id=d.department_id)
set salary = salary+1 
where location_id = 1800

Could anyone explain it to me?

Here is the explanation:

In your real life your relation supported by keys - reference constraint

employee.department_id(MANY) = departments.department_id(ONE)

In the case of UPDATE with JOIN , you can update only columns in your "MANY" table and only if they have real reference.

Your Create as select. . . Create as select. . . tables definitely don't have these references, hence Oracle optimizer throws this error.

Here are some references Reference 1 Reference 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM