首頁 資料庫 mysql教程 允许进行DML操作的视图条件

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

Jun 07, 2016 pm 03:56 PM
dml 可以 屏蔽 操作 條件 視圖 進行

视图可以屏蔽某些基表的信息,或是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:
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

PyCharm使用教學:詳細指引你執行操作 PyCharm使用教學:詳細指引你執行操作 Feb 26, 2024 pm 05:51 PM

PyCharm是一款非常受歡迎的Python整合開發環境(IDE),它提供了豐富的功能和工具,使得Python開發變得更有效率和便利。本文將為大家介紹PyCharm的基本操作方法,並提供具體的程式碼範例,幫助讀者快速入門並熟練操作工具。 1.下載安裝PyCharm首先,我們需要前往PyCharm官網(https://www.jetbrains.com/pyc

微信怎麼封鎖朋友圈的廣告推播 關閉微信朋友圈廣告的方法 微信怎麼封鎖朋友圈的廣告推播 關閉微信朋友圈廣告的方法 Mar 12, 2024 pm 01:50 PM

我們每一天無時無刻都是需要使用微信這一APP的,這裡的功能豐富多樣,都是不單單的讓大家在這裡進行聊天,都還是可以很好的為你們處理生活中的各種的一些事物,都能很好的解決一些問題,自己的生活都能夠變得越來越美好的,現在大家都還是特別的喜歡刷刷朋友圈,會發現朋友圈上,都有你們好友發布的一些生活的動態訊息,記錄自己的一些美好的生活,設定朋友圈的可見範圍,都是能夠在規定的時間內,看這一些朋友圈,都是自行決定,每一次大家刷朋友圈的時候,都能夠發現一些廣告的推播的介面,大家看的非常的煩躁,就是想要關閉這

什麼是 sudo,為什麼它如此重要? 什麼是 sudo,為什麼它如此重要? Feb 21, 2024 pm 07:01 PM

sudo(超級使用者執行)是Linux和Unix系統中的關鍵指令,允許一般使用者以root權限執行特定指令。 sudo的功能主要體現在以下幾個方面:提供權限控制:sudo透過授權使用者以臨時方式取得超級使用者權限,從而實現了對系統資源和敏感操作的嚴格控制。普通用戶只能在需要時透過sudo獲得臨時的特權,而不需要一直以超級用戶登入。提升安全性:透過使用sudo,可以避免在常規操作中使用root帳號。使用root帳戶進行所有操作可能會導致意外的系統損壞,因為任何錯誤或不小心的操作都將具有完全的權限。而

如何在iPhone上設定簡訊屏蔽功能 如何在iPhone上設定簡訊屏蔽功能 Feb 24, 2024 pm 01:48 PM

隨著智慧型手機的普及,我們每天都會收到大量的短信,有些是廣告推銷訊息,有些是垃圾短信,這些短信不僅浪費了我們的時間,還佔據了手機的空間。然而,幸運的是,iPhone手機提供了一些功能來封鎖這些煩人的簡訊。本文將介紹如何使用iPhone來屏蔽簡訊。要屏蔽短信,首先打開“設定”應用程序,然後滾動螢幕並點擊“訊息”。在資訊設定介面,你可以看到一些選項,包括「已封鎖的

探探怎麼屏蔽手機通訊錄好友 封鎖手機聯絡人的設定步驟 探探怎麼屏蔽手機通訊錄好友 封鎖手機聯絡人的設定步驟 Mar 12, 2024 pm 03:55 PM

我們都是特別的喜歡探探這個社交平台,非常的安全可靠,都能夠在這裡結識到超多的一些網絡上的朋友,隨機的進行匹配,都一定都能夠匹配到互相喜歡的用戶哦,當大家成功的進行配對的話,都能夠放心的在這裡進行社交聊天,每一天的聊天,都能夠拉進大家的關係,變得更加的親近,且不少的一些朋友們,在使用這一社交平台的時候,肯定都是不想要讓你們的一些朋友,或者是手機通訊錄的一些好友們發現,避免出現一些尷尬的情況發生哦,就能夠使用這裡的屏蔽聯繫人的方法,還有不怎麼懂屏蔽聯絡人操作,就是可以看看小編帶給你們的這些教程

win10開機密碼忘記按F2怎麼操作 win10開機密碼忘記按F2怎麼操作 Feb 28, 2024 am 08:31 AM

想必很多的用戶家裡都有那麼幾台不用的電腦,因為長時間不用完全忘了開機密碼,於是想知道一下,忘記密碼要怎麼操作呢?那就一起來看看吧。 win10開機密碼忘記按F2怎麼操作1、按下電腦的電源鍵,然後開機時按下F2(不同電腦品牌進入bios的按鍵也不同)。 2.在bios介面中,找到security選項(不同品牌電腦的位置可能有所不同)。一般都在頂部的設定選單中。 3.然後找到SupervisorPassword選項並且點選。 4.這時候用戶就可以看到自己的密碼了,同時找到旁邊的Enabled切換為Dis

Linux Deploy的操作步驟及注意事項 Linux Deploy的操作步驟及注意事項 Mar 14, 2024 pm 03:03 PM

LinuxDeploy的操作步驟及注意事項LinuxDeploy是一款強大的工具,可協助使用者在Android裝置上快速部署各種Linux發行版,讓使用者在行動裝置上體驗完整的Linux系統。本文將詳細介紹LinuxDeploy的操作步驟以及注意事項,同時提供具體的程式碼範例,幫助讀者更好地使用此工具。操作步驟:安裝LinuxDeploy:首先在

可以刪除downloads資料夾嗎? 可以刪除downloads資料夾嗎? Feb 19, 2024 pm 03:38 PM

downloads可以刪除嗎近年來,隨著數位時代的來臨,我們生活中出現了越來越多的數位化產品和服務。而隨之而來的,就是我們對於數位化內容的需求與日俱增。在日常生活和工作中,我們經常需要下載各種各樣的文件,如文件、圖片、音訊和視訊等等。而這些下載的文件,通常都被保存在一個名為「downloads」(下載)的資料夾中。然而,隨著時間的推移,我們經常發現,“

See all articles