一说到视图是否可以update,我就在想,如果视图只包含一张表,应该是可以update的,因为SQL会执行查询转换,将视图转成表。那多个表关联的视图,是否可以update呢,下面我们来做个试验:
SQL> create table t1 as select * from dba_objects;
表已创建。SQL> create table t2 as select * from dba_objects;
表已创建。
SQL> create or replace view v_test as select * from t1;
视图已创建。
SQL> update v_test set subobject_name=owner;
已更新50593行。
SQL> commit;
提交完成。
SQL> create or replace view v_test as select t1.* from
2 t1,t2 where t1.object_id=t2.object_id;
视图已创建。
SQL> update v_test set subobject_name=owner;
update v_test set subobject_name=owner *
第 1 行出现错误:
ORA-01779: 无法修改与非键值保存表对应的列
SQL> update /*+ BYPASS_UJVC */v_test set subobject_name=owner;
已更新50592行。
SQL> commit;
提交完成。
SQL> create or replace view v_test as select t1.object_id,t2.subobject_name,
2 t1.owner from
3 t1,t2 where t1.object_id=t2.object_id;
视图已创建。
SQL> update /*+ BYPASS_UJVC */v_test set subobject_name=owner;
已更新50592行。
SQL> commit;
提交完成。