通过更新VIEW的方式
update
(select emp.dname, dept.dname as new_dname
from emp
inner join dept on dept.deptno = emp.deptno)
set dname = new_name
执行时,可能会出现“无法修改与非键值保存表对应的列”,这时需要在表dept.deptno上增加主键,再执行
通过子查询方式
update emp set emp.dname = (select dept.dname from dept where dept.deptno = emp.deptno) where exists (select dept.dname from dept where dept.deptno = emp.deptno)
使用merge into语句操作
merge into emp using (select dname, deptno from dept where dept.dname in ('accounting','research')) dept on (dept.deptno = emp.deptno) when matched then update set emp.dname = dept.dname
使用merge更新效率高,因为只对dept表进行了一次查询,如果用子查询方式update,则需要扫描两次dept