search
HomeDatabaseMysql Tutorial允许进行DML操作的视图条件
允许进行DML操作的视图条件Jun 07, 2016 pm 03:56 PM
dmlCanshieldoperateconditionviewconduct

视图可以屏蔽某些基表的信息,或是join多个基表组成一个复杂查询,视图本身也是可以进行DML操作,但受一些条件的限制。 首先我们看下官方文档对视图进行DML操作的要求说明: The following notes apply to updatable views: An updatable view is one you ca

视图可以屏蔽某些基表的信息,或是join多个基表组成一个复杂查询,视图本身也是可以进行DML操作,但受一些条件的限制。

首先我们看下官方文档对视图进行DML操作的要求说明:

The following notes apply to updatable views:

An updatable view is one you can use to insert, update, or delete base table rows. You can create a view to be inherently updatable, or you can create an INSTEAD OF trigger on any view to make it updatable.

这里说明了两种可updateable(包括增删改基表)视图的方法:一是继承基表的视图,二是使用INSTEAD OF的触发器来实现任意视图的updatable。

To learn whether and in what ways the columns of an inherently updatable view can be modified, query the USER_UPDATABLE_COLUMNS data dictionary view. The information displayed by this view is meaningful only for inherently updatable views.

USER_UPDATABLE_COLUMNS数据字典视图可以找到视图的哪些字段可以进行增加、更新和删除。

For a view to be inherently updatable, the following conditions must be met:

对于这种updatable继承的视图,需要满足以下条件:

1. Each column in the view must map to a column of a single table. For example, if a view column maps to the output of a TABLE clause (an unnested collection), then the view is not inherently updatable.

2. The view must not contain any of the following constructs:

A set operator

A DISTINCT operator

An aggregate or analytic function

A GROUP BY, ORDER BY, MODEL, CONNECT BY, or START WITH clause

A collection expression in a SELECT list

A subquery in a SELECT list

A subquery designated WITH READ ONLY

Joins, with some exceptions, as documented in Oracle Database Administrator's Guide

3. In addition, if an inherently updatable view contains pseudocolumns or expressions, then you cannot update base table rows with an UPDATE statement that refers to any of these pseudocolumns or expressions.

4. If you want a join view to be updatable, then all of the following conditions must be true:

对于一个join视图,如果需要可updatable,那么就需要满足如下条件:

(1) The DML statement must affect only one table underlying the join.

DML必须仅影响一个join连接的表。

(2) For an INSERT statement, the view must not be created WITH CHECK OPTION, and all columns into which values are inserted must come from a key-preserved table. A key-preserved table is one for which every primary key or unique key value in the base table is also unique in the join view.

INSERT语句,不能使用WITH CHECK OPTION,并且所有待插入的列都来自于key-preserved表。

key-preserved表是指基表中每个主键或唯一键也必须是在join视图中唯一。

(3) For an UPDATE statement, the view must not be created WITH CHECK OPTION, and all columns updated must be extracted from a key-preserved table.

UPDATE语句,视图不能使用WITH CHECK OPTION创建,同样更新字段也必须来自于key-preserved表。

5. For a DELETE statement, if the join results in more than one key-preserved table, then Oracle Database deletes from the first table named in the FROM clause, whether or not the view was created WITH CHECK OPTION.

DELETE语句,如果join结果有多个key-preserved表,Oracle只会删除FROM子句中第一个表的记录,不管视图是否使用WITH CHECK OPTION。

下面通过一系列实验来说明。

创建测试表:

create table dept(deptid int primary key, deptname varchar2(20));

create table employee(empid int primary key, empname varchar2(20), deptid int);

创建测试数据:

insert into dept values(1,'dept1');

insert into dept values(2,'dept2');

insert into dept values(3,'dept3');

insert into employee values(1,'emp1',1);

insert into employee values(2,'emp2',1);

insert into employee values(3,'emp3',2);

创建视图:

create view testv

as select d.deptid deptid, deptname, empid, empname, e.deptid edeptid

from dept d join employee e

on d.deptid = e.deptid;

SQL> select * from testv;
DEPTID DEPTNAME EMPID EMPNAME EDEPTID
---------- -------------------- ---------- -------------------- ----------
1 dept1 1 emp1 1
1 dept1 2 emp2 1
2 dept2 3 emp3 2

仅employee表是key-preserved表。

测试1:对key-preserved表字段进行增加、更新的操作

update testv set empname='empx' where edeptid=1;

update testv set empname='empx' where empid=1;

update testv set empname='empx' where deptid=1;

insert into testv(empid,empname,edeptid) values(4,'emp4',2);

以上SQL可以执行,因为修改或添加的字段都是employee的,即key-preserved表。

测试2:验证上述“DELETE语句,如果join结果有多个key-preserved表,Oracle只会删除FROM子句中第一个表的记录,不管视图是否使用WITH CHECK OPTION”

create view testv

as select d.deptid deptid, deptname, empid, empname, e.deptid edeptid

from employee e join dept d

on d.deptid = e.deptid;

create view testv

as select d.deptid deptid, deptname, empid, empname, e.deptid edeptid

from employee e join dept d

on d.deptid = e.deptid

WITH CHECK OPTION;

select * from testv;
DEPTID DEPTNAME EMPID EMPNAME EDEPTID
---------- -------------------- ---------- -------------------- ----------
1 dept1 1 emp1 1
1 dept1 2 emp2 1
2 dept2 3 emp3 2

delete from testv where deptid = 1;
2 rows deleted.

select * from dept;
DEPTID DEPTNAME
---------- --------------------
1 dept1
2 dept2
3 dept3

select * from employee;
EMPID EMPNAME DEPTID
---------- -------------------- ----------
3 emp3 2

delete from testv where empid = 1;
1 row deleted.


select * from testv;
DEPTID DEPTNAME EMPID EMPNAME EDEPTID
---------- -------------------- ---------- -------------------- ----------
1 dept1 2 emp2 1
2 dept2 3 emp3 2

select * from dept;
DEPTID DEPTNAME
---------- --------------------
1 dept1
2 dept2
3 dept3

select * from employee;
EMPID EMPNAME DEPTID
---------- -------------------- ----------
2 emp2 1
3 emp3 2

测试3:对于INSERT和UPDATE语句,不能使用WITH CHECK OPTION创建视图

create view test1v
as select t1id ,t1v,t2id,t2v
from test1 join test2
on test1.t1id=test2.t2id
with check option;

insert into test1v(t1id,t1v) values(4,'t4');
*
ERROR at line 1:
ORA-01733: virtual column not allowed here

update test1v set t1id=4 where t1id=1;
*
ERROR at line 1:
ORA-01733: virtual column not allowed here

测试4:非key-preserved表字段不能更新或插入

update testv set deptname='deptx' where deptid=1
update testv set deptname='deptx' where empid=1
insert into testv(deptid,deptname) values(4,'dept4')
ORA-01779: cannot modify a column which maps to a non key-preserved table

测试5:查看视图中哪些字段可以增删改

select * from USER_UPDATABLE_COLUMNS where table_name='TESTV';
OWNER TABLE_NAME COLUMN_NAME UPD INS DEL
-------------------------------------------------------------------------------------------
DCSOPEN TESTV DEPTID NO NO NO
DCSOPEN TESTV DEPTNAME NO NO NO
DCSOPEN TESTV EMPID YES YES YES
DCSOPEN TESTV EMPNAME YES YES YES
DCSOPEN TESTV EDEPTID YES YES YES

If you want a join view to be updatable, then all of the following conditions must be true:
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP编程中有哪些常见的Behat操作?PHP编程中有哪些常见的Behat操作?Jun 12, 2023 am 08:19 AM

PHP编程中有哪些常见的Behat操作?Behat是一个行为驱动开发(BDD)工具,允许测试人员和开发人员在自然语言中撰写测试用例,并将这些用例转化为可执行的代码。它支持PHP语言,并提供了丰富的库和功能,可以实现多种常见的测试操作。下面列举了PHP编程中常见的Behat操作。前置条件(Background)在编写测试用例时,有时候会有一些公共的前置条件需要

ThinkPHP6如何进行表单验证操作?ThinkPHP6如何进行表单验证操作?Jun 12, 2023 am 09:36 AM

ThinkPHP6是一款基于PHP的MVC框架,极大地简化了Web应用程序的开发。其中表单验证是一个非常基础和重要的功能。在这篇文章中,我们将介绍ThinkPHP6中如何进行表单验证操作。一、验证规则定义在ThinkPHP6中,验证规则都需要定义在控制器中,我们可以通过在控制器中定义一个$validate属性来实现规则的定义,如下所示:usethinkVa

mysql的DML进阶、分页查找、SQL约束及多表操作方法mysql的DML进阶、分页查找、SQL约束及多表操作方法May 31, 2023 pm 07:10 PM

一.什么是DML,以及DML基本操作,表的列和行的跟新操作针对列进行修改操作#首先简单的创建一个student表为后序操作做准备usetest;createtablestudent(idint,namevarchar(8),agetinyint)engine=innodbdefaultcharset=utf8mb4;descstudent;添加新的列,格式:altertable表名add新列名数据类型(长度);altertablestudentaddaddrvarchar(20);#新增一个ad

使用Python进行RFM分析使用Python进行RFM分析Sep 03, 2023 pm 12:45 PM

Python是一种多功能的编程语言,在数据分析和机器学习领域广受欢迎。其简洁性、可读性和丰富的库使其成为处理复杂数据任务的理想选择。其中一个强大的应用是RFM分析,这是一种在营销中根据客户购买行为进行分割的技术。在本教程中,我们将通过使用Python来实施RFM分析的过程来指导您。我们将从解释RFM分析的概念及其在营销中的重要性开始。然后,我们将逐步深入探讨使用Python进行RFM分析的实际方面。在文章的下一部分中,我们将演示如何使用Python为每个客户计算RFM分数,考虑到为最近性、频率和

《黑神话:悟空》Xbox 版被曝因“内存泄漏”而延期,PS5 版优化进行中《黑神话:悟空》Xbox 版被曝因“内存泄漏”而延期,PS5 版优化进行中Aug 27, 2024 pm 03:38 PM

近日,《黑神话:悟空》在全球范围内都引发了巨大的关注,各平台的同时在线人数都再创新高,这款游戏在多个平台取得了巨大的商业成功。《黑神话:悟空》的Xbox版延期虽然《黑神话:悟空》已于PC和PS5平台发布,但其Xbox版一直没有确切消息。据了解,官方已确认《黑神话:悟空》将登陆Xbox平台。但具体上线日期尚未公布。最近有消息称,Xbox版的延期是由于技术问题所致。据相关博主透露,他在Gamescom期间与开发人员和"Xbox内部人士"的交流中得知,《黑神话:悟空》的Xbox版存

PHP编程中有哪些常见的jQuery操作?PHP编程中有哪些常见的jQuery操作?Jun 12, 2023 am 10:38 AM

PHP编程中有哪些常见的jQuery操作?在PHP编程中,使用jQuery进行网页开发是一种非常方便和高效的方式。jQuery是一个简单而强大的JavaScript库,包含了许多实用的方法和函数。在PHP编程中,我们常常使用jQuery来操纵HTML和DOM元素,使网页具有更好的交互性和高度的可视化效果。在本文中,我们将介绍一些常见的PHP编程中使用jQue

PHP编程中有哪些常见的OAuth操作?PHP编程中有哪些常见的OAuth操作?Jun 12, 2023 am 08:48 AM

OAuth(开放授权)是一种用于授权访问控制的标准化协议。在Web开发中,使用OAuth可以帮助应用程序安全地从第三方平台中获取用户数据或资源。而在PHP编程中,OAuth操作也被广泛应用。本文将介绍PHP编程中的常见OAuth操作。OAuth1.0a授权OAuth1.0a授权是OAuth中最早出现的授权方式,也是最复杂的一种授权方式。在PHP编程中,O

ThinkPHP6中如何进行分词搜索操作?ThinkPHP6中如何进行分词搜索操作?Jun 12, 2023 am 09:39 AM

随着互联网应用的不断发展,搜索引擎也成为了日常生活中必不可少的工具,而分词搜索是搜索引擎中非常重要的一种搜索方式。在使用ThinkPHP6框架开发项目时,我们也需要对分词搜索进行深入了解和应用。本文将介绍ThinkPHP6中如何进行分词搜索操作。一、分词搜索简介分词搜索是将用户输入的关键词进行分割,然后在数据库中进行模糊搜索,找到相符合的记录。相较于传统的搜

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools