http://wmlm.itpub.net/post/12871/278640

因为在项目中大量地使用了视图,而在视图上的更新上产生了一点儿问题,所以抽时间对可更新视图进行了复习,英文看得多了,也就成了中文

测试用表

CREATE TABLE Dept_tab (
Deptno NUMBER(4) PRIMARY KEY,
Dname VARCHAR2(14), Loc VARCHAR2(13));
CREATE TABLE Emp_tab (
Empno NUMBER(4) PRIMARY KEY, Ename VARCHAR2(10),
Job varchar2(9), Mgr NUMBER(4),
Hiredate DATE, Sal NUMBER(7,2),
Comm NUMBER(7,2), Deptno NUMBER(2),
FOREIGN KEY (Deptno) REFERENCES Dept_tab(Deptno));
CREATE VIEW Emp_dept_view AS
SELECT e.Empno, e.Ename, e.Deptno, e.Sal, d.Dname, d.Loc FROM Emp_tab e, Dept_tab d /* JOIN operation */
WHERE e.Deptno = d.Deptno AND d.Loc IN ('DALLAS', 'NEW YORK', 'BOSTON');In this view,EMP_TAB is a key-preserved table, becauseEMPNO is a key of theEMP_TAB table, and also a key of the result of the join.DEPT_TAB is not a key-preserved table, because although DEPTNO is a key of theDEPT_TAB table, it is not a key of the join.

Rule for DML Statements on Join Views

Any UPDATE,INSERT, or DELETE statement on a join view can modifyonly one underlying base table.

Updating a Join View

The following example shows an UPDATE statement that successfully modifies theEMP_DEPT_VIEW view:

UPDATE Emp_dept_view
  SET Sal = Sal * 1.10 
    WHERE Deptno = 10;

The following UPDATE statement would be disallowed on theEMP_DEPT_VIEW view:

UPDATE Emp_dept_view
  SET Loc = 'BOSTON'
WHERE Ename = 'SMITH';

This statement fails with an ORA-01779 error ("cannot modify a column which maps to a non key-preserved table"), because it attempts to modify the underlyingDEPT_TAB table, and theDEPT_TAB table is not key preserved in theEMP_DEPT view.

In general, all modifiable columns of a join view must map to columns of a key-preserved table. If the view is defined using theWITHCHECKOPTION clause, then all join columns and all columns of repeated tables are not modifiable.

So, for example, if the EMP_DEPT view were defined usingWITH CHECKOPTION, then the followingUPDATE statement would fail:

UPDATE Emp_dept_view
    SET Deptno = 10
        WHERE Ename = 'SMITH';

The statement fails because it is trying to update a join column.

总结:更新一个复杂视图的条件是,只能更新主键表(Key-Preserved )的列;如果WITHCHECKOPTION ,则不能更新公共列;

Deleting from a Join View

You can delete from a join view provided there is one and only one key-preserved table in the join.

The following DELETE statement works on theEMP_DEPT view:

DELETE FROM Emp_dept_view
    WHERE Ename = 'SMITH';

This DELETE statement on theEMP_DEPT view is legal because it can be translated to aDELETE operation on the baseEMP_TAB table, and because theEMP_TAB table is the only key-preserved table in the join.

In the following view, a DELETE operation cannot be performed on the view because bothE1 and E2 are key-preserved tables:

CREATE VIEW emp_emp AS
    SELECT e1.Ename, e2.Empno, e1.Deptno
        FROM Emp_tab e1, Emp_tab e2
        WHERE e1.Empno = e2.Empno;
        WHERE e1.Empno = e2.Empno;

If a view is defined using the WITHCHECK OPTION clause and the key-preserved table is repeated, then rows cannot be deleted from such a view. For example:

CREATE VIEW Emp_mgr AS
    SELECT e1.Ename, e2.Ename Mname
       FROM Emp_tab e1, Emp_tab e2
            WHERE e1.mgr = e2.Empno
            WITH CHECK OPTION;

No deletion can be performed on this view because the view involves a self-join of the table that is key preserved.

一句话总结:You can delete from a join view provided there isone and only one key-preserved table in the join.

Inserting into a Join View

The following INSERT statement on theEMP_DEPT view succeeds, because only one key-preserved base table is being modified (EMP_TAB), and 40 is a validDEPTNO in the DEPT_TAB table (thus satisfying theFOREIGN KEY integrity constraint on theEMP_TAB table).

INSERT INTO Emp_dept (Ename, Empno, Deptno)
    VALUES ('KURODA', 9010, 40);

The following INSERT statement fails for the same reason: ThisUPDATE on the base EMP_TAB table would fail: theFOREIGN KEY integrity constraint on theEMP_TAB table is violated.

INSERT INTO Emp_dept (Ename, Empno, Deptno)
    VALUES ('KURODA', 9010, 77);

The following INSERT statement fails with anORA-01776 error ("cannot modify more than one base table through a view").

INSERT INTO Emp_dept (Ename, Empno, Deptno)
    VALUES (9010, 'KURODA', 'BOSTON');

An INSERT cannot, implicitly or explicitly, refer to columns of a non-key-preserved table. If the join view is defined using theWITH CHECK OPTION clause, then you cannot perform anINSERT to it.

总结:insert成功的条件only one key-preserved base table is being modified并且符合约束;

但如果在join视图中with check option那么你不能再insert了.

相关视图 Three views you can use for modifying join views :USER_UPDATABLE_COLUMNS

Outer Joins

Views that involve outer joins are modifiable in some cases. For example:

CREATE VIEW Emp_dept_oj1 AS
    SELECT Empno, Ename, e.Deptno, Dname, Loc
    FROM Emp_tab e, Dept_tab d
    WHERE e.Deptno = d.Deptno (+);

Columns in the base EMP_TAB table ofEMP_DEPT_OJ1 are modifiable through the view, because EMP_TAB is a key-preserved table in the join.

The following view also contains an outer join:

CREATE VIEW Emp_dept_oj2 AS
SELECT e.Empno, e.Ename, e.Deptno, d.Dname, d.Loc
FROM Emp_tab e, Dept_tab d
WHERE e.Deptno (+) = d.Deptno;

In this view, EMP_TAB is no longer a key-preserved table, because theEMPNO column in the result of the join can have nulls (the last row in theSELECT above). So, UPDATE, DELETE, and INSERT operations cannot be performed on this view.

Consider the following set of views:

CREATE VIEW Emp_v AS
    SELECT Empno, Ename, Deptno
        FROM Emp_tab;
CREATE VIEW Emp_dept_oj1 AS
    SELECT e.*, Loc, d.Dname
        FROM Emp_v e, Dept_tab d
            WHERE e.Deptno = d.Deptno (+);

In these examples, EMP_V is merged intoEMP_DEPT_OJ1 because EMP_V is a simple view, and so EMP_TAB is a key-preserved table. But if EMP_V is changed as follows:

CREATE VIEW Emp_v_2 AS
    SELECT Empno, Ename, Deptno
        FROM Emp_tab
            WHERE Sal > 1000;

Then, because of the presence of theWHERE clause, EMP_V_2 cannot be merged into EMP_DEPT_OJ1, and henceEMP_TAB is no longer a key-preserved table.

If you are in doubt whether a view is modifiable, then you canSELECT from the view USER_UPDATABLE_COLUMNS to see if it is

=================

http://www.geekinterview.com/talk/11040-key-preserved-table.html

A table is key preserved if every key of the table can also be a key of the result of the join. In key preserved table rows from the base appears at most once. Key preserved table guarantees to return only one copy of each row from the base table.

Code:
SQL> CREATE VIEW emp_dept AS
2  SELECT      a.empno, a.ename, a.sal , a.deptno, b.dname
3  FROM        emp a, dept b
4  WHERE       a.deptno = b.deptno ;
View created.
SQL> SELECT * FROM emp_dept;
EMPNO ENAME            SAL    DEPTNO DNAME
--------- ---------- --------- --------- --------------
7369 SMITH           5000        20 RESEARCH
7499 ALLEN         2129.6        30 SALES
7521 WARD         1663.75        30 SALES
7566 JONES        3959.73        20 RESEARCH
7654 MARTIN       1663.75        30 SALES
7698 BLAKE        3793.35        30 SALES
7782 CLARK        3260.95        10 ACCOUNTING
7788 SCOTT           3993        20 RESEARCH
7839 KING            6655        10 ACCOUNTING
7844 TURNER        1996.5        30 SALES
7876 ADAMS         1464.1        20 RESEARCH
7900 JAMES        1264.45        30 SALES
7902 FORD            3993        20 RESEARCH
7934 MILLER        1730.3        10 ACCOUNTING
14 rows selected.

In the above example emp is key preserved table. Rows from EMP appears only once. DEPT is not a key preserved table. DEPTNO is key column in dept. But it is not a key column in EMP_DEPT view.

in a join,a table is called a key-preserve table if its keys are preserved through the join.every key of the table can also be a key of the resultant join resultset.every primary key or unique key value in the base table must also be unique in the result set of the join.
A)key-preservation is a property of the table inside the join view not the table itself independently.a table may be key preseved in one join view and may not be key preserved in another join view.
B)it is not necessary for the key columns of a table to be selected in the join view for the table to be key preserved.
C)if the key columns of a table is selected in the view defination ,it does not make the the table key preserved.
D)the key-preserved property of the table in a join view does not depend on the data inside the table.it depends on the schema design and the relationship between the tables.
E)a join view may select data from many tables ,any dml operation can modify data from only one underlying table.
F)user can't refer to the columns of a non-key-preserved table in an insert statment.
G)delete operation can be performed on a join view if the join view has one and only one key-preserved table.

视图中的难点:主键表 About Key-Preserved Tables相关推荐

  1. 一个表对应另一个表中多个主键的查询方法(把一个表当成两个表用)

    表t_Record PID(主键)   SendUserID(发送人)   ReceiveUserID(接收人)    1              1                      2 ...

  2. oracle 主键 删除表_ORACLE中添加删除主键

    1.创建表的同时创建主键约束 (1)无命名 create table student ( studentid int primary key not null, studentname varchar ...

  3. mysql 联结主键_联结表中的复合主键 - Sequelize

    使用Sequelize和MySQL数据库,我试图在联结表中实现复合主键组合,但遗憾的是没有结果 . 我有 table : 它们与许多人有很多关系 . 在联结表user_has_project中,我想要 ...

  4. mysql自增字段不连续_MySQL中自增主键不连续之解决方案。(20131109)

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 今天只做了一件事情,但解决了很大的问题.相信这也是令很多程序员和数据库管理员头疼的事情. 假设在一MySQL数据表中,自增的字段为id,唯一字段为abc, ...

  5. 数据库中什么是主键,什么是外键?

    数据库中什么是主键,什么是外键? 作者:不染丹心  来源:博客园  发布时间:2009-12-17 22:57  阅读:128 次  原文链接   [收藏]   这需要理清几个概念: 1)候选键: 关 ...

  6. mysql添加数据不阻塞_主键表插入数据不提交,外键表插入数据被阻塞

    有客户和我说:他在含主外键的表中实验发现,在主表数据未提交,然后在外键表插入该数据数据时,出现外键表hang住现象.我开始以为是不同的会话,根据oracle数据库的一致性原则,应该新会话在外键表中不能 ...

  7. 分布式数据库中全局唯一主键

    [相关文章] <分布式数据库中全局唯一主键生成策略的设计与实现> <activiti5.10解决分布式集群部署的主键问题> <分布式环境下数据库主键方案> < ...

  8. oracle中如何取消外键的,ORACLE中添加删除主键、外键

    1.创建表的同时创建主键约束 (1)无命名 create table student ( studentid int primary key not null, studentname varchar ...

  9. SQL Server中如何给主键添加主键约束

    SQL Server中如何给主键添加主键约束? 方法/步骤 1 SQL Server中如何给表添加主键约束,下面为大家分享了两种方法希望大家能够速学速懂. 2 3 在如下图中大家可以看到的是一个Stu ...

最新文章

  1. ethereumjs/ethereumjs-vm-2-API文档
  2. 当向后台插入或读取JSON数据遇见回车时
  3. python3屏幕抓取程序_python之屏幕抓取
  4. opengl加载显示3DS模型3DS类型文件
  5. linux下不同arm 编译器的异同
  6. 《深入理解JVM.2nd》笔记(五):调优案例分析与实战
  7. 简单的深度优先遍历和广度优先遍历
  8. ios中解决图片渲染问题
  9. DIY斑竹管理初稿的确定
  10. MFC 教程【10_内存分配方式和调试机制 】
  11. vmware workstation虚拟环境安装及创建虚拟机
  12. [深度大牛]·计算机视觉王者何凯明
  13. 计算流体力学c语言教程,计算流体力学教程
  14. 火狐控制台的html,怎么使用火狐浏览器调试网页
  15. CSS如何在宽高不确定的父元素内画一个正方形
  16. 阿里云直播集成简要指南
  17. 计算机 蓝牙鼠标卡顿,无线蓝牙鼠标为什么有时会卡顿发飘,不稳定?
  18. Caffe学习笔记二 Extracting Features
  19. 王二是如何看到李四的《艳娘传奇》的,快来了解一下ROS2的话题机制吧!
  20. 推荐一个强大的工作流自动化工具...

热门文章

  1. OpenGL之深入解析渲染架构和数据传递
  2. Georgia and Bob POJ - 1704
  3. 信息学奥赛一本通(C++)在线评测系统——基础(二)基础算法 —— 1312:【例3.4】昆虫繁殖
  4. 【MFC】创建第一个MFC界面项目
  5. 【Linux】一步一步学Linux——read命令(220)
  6. ARM指令寻址方式之: 数据处理指令的寻址方式
  7. 【Android】 Android Service生命周期及用法
  8. ps自定义形状工具_PS教程——用PS绘制虚线的三种方法
  9. ios加载本地游戏html,使用WKWebView iOS加载本地HTML / Javascript
  10. go 怎么等待所有的协程完成_Go 编程:如何实现协程调度的精准控制