view不能更新的情况:
1。基本表中没有的列不能更新
2。基本表中存在连接的情况
除了这两种情况,还有别的情况不能更新么这两种情况为什么不能更新?/

解决方案 »

  1.   

    view本来是由基表生成的,你修改了,将会直接影响基本表啊~会造成view和基本表不统一
      

  2.   

    视图包含下列结构是不可以更新的 1:集合运算符 union,union all, intersect,minus 2:distinct关键字 3:group by,order by,connect by,或者start with 4:子查询 5:分组函数 6:需要更新的列不是视图定义的 7:具有连接查询(可以更新键值保存表的数据) 8:违反基表的约束条件
      

  3.   

    如果非要做一个修改,那么可以通过view的触发器来实现INSTEAD   OF   TRIGGER
      

  4.   

    那为什么查询条件中有连接的view也不能更新阿
      

  5.   

    有连接的情况不能更新
    比如 视图是通过a的字段查询到b的字段 又关联c的字段 只显示出了a和c的字段
    通过a的字段在视图中查询c的字段的值  但是C中等于这个值的不一定都能和a有关系 这样就会造成混乱
      

  6.   


    SQL> create or replace view v_deptemp
      2  as
      3  select a.empno,a.ename,a.sal,b.deptno,b.dname
      4  from emp a,dept b
      5  where a.deptno=b.deptno
      6  /视图已创建。SQL> select * from v_deptemp where empno=1688
      2  /未选定行SQL> select * from dept where deptno=50
      2  /未选定行SQL> ed
    已写入 file afiedt.buf  1  create or replace trigger  tri_v_deptemp instead of insert on v_deptemp
      2  for each row
      3  begin
      4  begin
      5  insert into dept(deptno,dname) values(:new.deptno,:new.dname);
      6  exception
      7  when others then
      8  null;
      9  end;
     10  begin
     11  insert into emp(empno,ename,sal,deptno) values(:new.empno,:new.ename,:new.sal,:new.deptno);
     12  exception
     13  when others then
     14  null;
     15  end;
     16* end;
    SQL> /触发器已创建SQL> insert into v_deptemp values(1688,'scott',5000,50,'db')
      2  /已创建 1 行。SQL> commit
      2  /提交完成。SQL> select * from v_deptemp where empno=1688
      2  /     EMPNO ENAME             SAL     DEPTNO DNAME
    ---------- ---------- ---------- ---------- --------------
          1688 scott            5000         50 dbSQL> 
      

  7.   

    部分可更新视图示例
    create or replace view my_emp_view
    as
    select d.deptno,d.dname,e.ename,e.empno
    from dept d,emp e
    where d.deptno=e.deptno--更新视图
    update my_emp_view set ename='张三' where empno=7900;
    可以执行。update my_emp_view set dname='市场部' where empno=7900;
    不可以执行。因为此时dname是通过表连接动态计算出来的。分析:视图能否更新,取决于所更新的行是否能与实际数据表中的行一一对应,如果能对应,则可以更新。emp表中的每一行数据与my_emp_view视图中的每一行能够一一对应,但是dept表中的每一行数据却对应my_emp_view视图的多行数据,所以在my_emp_view视图中是不能更新部门信息的,但是能够更新员工数据。
      

  8.   


    刚在公交车上想了下  对于有外键约束的则不可以更新没有则可以
    SQL> create or replace trigger  tri_v_deptemp instead of insert or update on v_deptemp
      2  for each row
      3  begin
      4  if inserting then
      5  begin
      6  insert into dept(deptno,dname) values(:new.deptno,:new.dname);
      7  exception
      8  when others then
      9  null;
     10  end;
     11  begin
     12  insert into emp(empno,ename,sal,deptno) values(:new.empno,:new.ename,:new.sal,:new.deptno);
     13  exception
     14  when others then
     15  null;
     16  end;
     17  end if;
     18  if updating then
     19  begin
     20  update dept set deptno=:new.deptno,dname=:new.dname where deptno=:old.deptno;
     21  exception
     22  when others then
     23  dbms_output.put_line(sqlcode||' '||sqlerrm);
     24  end;
     25  begin
     26  update emp set empno=:new.empno,ename=:new.ename,sal=:new.sal,deptno=:new.deptno
     27  where deptno=:old.deptno;
     28  exception
     29  when others then
     30  dbms_output.put_line(sqlcode||' '||sqlerrm);
     31  end;
     32  end if;
     33  end;
     34  /触发器已创建SQL> select * from v_deptemp where deptno=50
      2  /     EMPNO ENAME             SAL     DEPTNO DNAME
    ---------- ---------- ---------- ---------- --------------
          1688 scott            5000         50 dbSQL> QL> select * from dept  where deptno=50
     2  /   DEPTNO DNAME          LOC
    --------- -------------- -------------
           50 dbQL> select * from emp where deptno=50
     2  /    EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
    --------- ---------- --------- ---------- -------------- ---------- ----------
       DEPTNO
    ---------
         1688 scott                                                5000
           50
    SQL>  alter table emp drop constraint FK_DEPTNO  ----这里删除了外键约束
      2  /表已更改。SQL> update v_deptemp set deptno=60 where deptno=50
      2  /已更新 1 行。SQL> commit
      2  /提交完成。SQL> select * from v_deptemp where deptno=50
      2  /未选定行SQL> select * from v_deptemp where deptno=60
      2  /     EMPNO ENAME             SAL     DEPTNO DNAME
    ---------- ---------- ---------- ---------- --------------
          1688 scott            5000         60 dbSQL> select * from emp where deptno=60
      2  /     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
    ---------- ---------- --------- ---------- -------------- ---------- ----------
        DEPTNO
    ----------
          1688 scott                                                5000
            60
    SQL> select * from dept where deptno=60
      2  /    DEPTNO DNAME          LOC
    ---------- -------------- -------------
            60 db
    否则报ORA-02292: 违反完整约束条件 (SCOTT.FK_DEPTNO) - 已找到子记录
    ORA-02291: 违反完整约束条件 (SCOTT.FK_DEPTNO) - 未找到父项关键字