Home Database Mysql Tutorial 允许进行DML操作的视图条件

允许进行DML操作的视图条件

Jun 07, 2016 pm 03:56 PM
dml Can shield operate condition view conduct

视图可以屏蔽某些基表的信息,或是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 of this Website
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

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 ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to set up text message blocking on iPhone How to set up text message blocking on iPhone Feb 24, 2024 pm 01:48 PM

With the popularity of smart phones, we receive a large number of text messages every day, some of which are advertising and promotional messages, and some of which are spam text messages. These text messages not only waste our time, but also occupy the space of our mobile phones. Fortunately, however, iPhones offer some features to block these annoying text messages. This article will introduce how to block text messages using iPhone. To block text messages, first open the Settings app, then scroll and tap Messages. In the information settings interface, you can see some options, including "Blocked

PyCharm usage tutorial: guide you in detail to run the operation PyCharm usage tutorial: guide you in detail to run the operation Feb 26, 2024 pm 05:51 PM

PyCharm is a very popular Python integrated development environment (IDE). It provides a wealth of functions and tools to make Python development more efficient and convenient. This article will introduce you to the basic operation methods of PyCharm and provide specific code examples to help readers quickly get started and become proficient in operating the tool. 1. Download and install PyCharm First, we need to go to the PyCharm official website (https://www.jetbrains.com/pyc

What is sudo and why is it important? What is sudo and why is it important? Feb 21, 2024 pm 07:01 PM

sudo (superuser execution) is a key command in Linux and Unix systems that allows ordinary users to run specific commands with root privileges. The function of sudo is mainly reflected in the following aspects: Providing permission control: sudo achieves strict control over system resources and sensitive operations by authorizing users to temporarily obtain superuser permissions. Ordinary users can only obtain temporary privileges through sudo when needed, and do not need to log in as superuser all the time. Improved security: By using sudo, you can avoid using the root account during routine operations. Using the root account for all operations may lead to unexpected system damage, as any mistaken or careless operation will have full permissions. and

How to block advertisement push in WeChat Moments How to turn off advertisements in WeChat Moments How to block advertisement push in WeChat Moments How to turn off advertisements in WeChat Moments Mar 12, 2024 pm 01:50 PM

We need to use WeChat APP all the time every day. The functions here are rich and diverse, not only for everyone to chat here, but also for you to handle various things in life. They can solve some problems very well, and their lives can become better and better. Nowadays, everyone still likes to check the circle of friends. You will find that there are some life updates posted by your friends on the circle of friends. information, record some of your wonderful life, and set the visibility range of your Moments. You can view these Moments within a specified time. It is your own decision. Every time you check your Moments, you will be able to discover something. Everyone is very irritated by the advertising push interface and wants to close it.

How to block friends in Tantan's mobile address book? Setting steps for blocking mobile contacts How to block friends in Tantan's mobile address book? Setting steps for blocking mobile contacts Mar 12, 2024 pm 03:55 PM

We all particularly like the Tantan social platform. It is very safe and reliable. We can all make a lot of friends on the Internet here. If we match randomly, we will definitely be able to match users who like each other. When everyone is successfully matched, they can safely chat socially here. Chatting every day can bring everyone closer and become closer, and many friends are using this social network. When you are on the platform, you definitely don’t want some of your friends or some friends in your mobile address book to find out. To avoid some embarrassing situations, you can use the method of blocking contacts here, and there are not many If you know how to block contacts, you can read these tutorials brought to you by the editor.

Linux Deploy operation steps and precautions Linux Deploy operation steps and precautions Mar 14, 2024 pm 03:03 PM

LinuxDeploy operating steps and precautions LinuxDeploy is a powerful tool that can help users quickly deploy various Linux distributions on Android devices, allowing users to experience a complete Linux system on their mobile devices. This article will introduce the operating steps and precautions of LinuxDeploy in detail, and provide specific code examples to help readers better use this tool. Operation steps: Install LinuxDeploy: First, install

How can I block the company from viewing my resume in Zhaopin Recruitment? Tutorial on Zhaopin Recruitment to block companies from seeing your resume! How can I block the company from viewing my resume in Zhaopin Recruitment? Tutorial on Zhaopin Recruitment to block companies from seeing your resume! Mar 15, 2024 pm 04:04 PM

1. How can Zhaopin Recruitment block the company from viewing my resume? Tutorial on Zhaopin Recruitment to block companies from seeing your resume! 1. Open the downloaded Zhaopin Recruitment app, log in to your account, and enter the main page. 2. After entering the main page, click My and select Online Resume. 3. After reaching the resume interface, click on the upper right corner and open the privacy settings option. 4. After entering the privacy interface, click Block Company. 5. In the interface to add blocking, enter the name of the company you want to block. 6. After final selection, click the block button below. It has been set up and the company will not be able to view your resume.

What to do if you forget to press F2 for win10 boot password What to do if you forget to press F2 for win10 boot password Feb 28, 2024 am 08:31 AM

Presumably many users have several unused computers at home, and they have completely forgotten the power-on password because they have not been used for a long time, so they would like to know what to do if they forget the password? Then let’s take a look together. What to do if you forget to press F2 for win10 boot password? 1. Press the power button of the computer, and then press F2 when turning on the computer (different computer brands have different buttons to enter the BIOS). 2. In the bios interface, find the security option (the location may be different for different brands of computers). Usually in the settings menu at the top. 3. Then find the SupervisorPassword option and click it. 4. At this time, the user can see his password, and at the same time find the Enabled next to it and switch it to Dis.

See all articles