SG_007_CHAPTER 8

chenai79921發表於2009-10-23

Managing Views

Creating and Modifying Views

Create view view_name AS

Select column1,column2

From table

Where condition

Using Defined Column Names:

Create view view_name (column1,column2,…..)

AS select xxxx

From table

Where condition.

如果建立一個檢視是使用select *的話,如果之後修改主表的話,必須重新建立檢視。

Creating Views with Errors

建立檢視的時候,使用FORCE引數。(NO FORCE是預設的)

Create force view view_name AS

Select *

From table1

即使沒有table1,檢視照樣建立。但是進去檢視的時候,會出錯。

Creating Read-Only Views

Create view view_name AS

Select column1,column2

From table

Where condition

WITH READ ONLY

Creating Constraints on Views

只允許Primary key, foreign key, unique key

Modifying Views:

改變檢視的definition,使用CREATE VIEW OR REPLACE

ALTER VIEW 用來compile一個非法的view,或者新增,刪除約束。

Changing a View’s Definition

如果使用create view or replace,而不是使用drop re-create檢視,那麼被賦予在檢視上的許可權被保留。

Recompiling a View

當主表被修改後,檢視變為不合法。Oracle 自動recompile檢視,當檢視被access時。

也可以使用ALTER VIEW顯視得recompile檢視。

當檢視被recompile後,所有依賴檢視的物件都變為invalid

Dropping a View

drop檢視,檢視的定義從資料字典中刪除,賦予檢視上的許可權也被drop。依賴於檢視的其他檢視和儲存過程都變為invalid

Drop VIEW test_view

Using Views:

對檢視的dml操作都影響到基本表,

Inserting, Updating, and Deleting Data through Views

可以對檢視執行dml語句,只有當檢視沒有如下的定義:

DISTINCT

GROUP BY

START WITH

CONNECT BY

ROWNUM

UNION, UNION ALL , INTERSECT , MINUS

Subquery in the SELECT clause

聚合函式

WITH CHECK OPTION:

當透過某個conditon從基本表中建立了檢視,如果此時向檢視插入一個違反condition的資料,照樣可以正常。當建立檢視的時候加上WITH CHECK OPTION引數,此時再向檢視插入一個違反condition的資料,會出錯。

Using Join Views

多表連結形成檢視。其中一個表為key-preserved,因為這個表的主鍵,在檢視中也是主鍵。另外的表不是key-preserved

只能透過檢視,update key-preserved表。如果檢視定義為WITH CHECK OPTION, 就不能update

透過DML只能,透過檢視只能同時對一個表進行修改。

Viewing Allowable DML Operations

可以透過查詢 USER_UPDATABLE_COLUMNS

ALL_UPDATABLE_COLUMNS

DBA_UPDATABLE_COLUMNS

來確定哪些檢視是否允許updata inserte delete操作。

Using Inline Views

放在from 語句中。

Order by 不能出現在子查詢中。Inline view中可以有order by

Performing Top-‘N’ Analysis

Select *

From table

Where rownum<=10

[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/11997930/viewspace-1028076/,如需轉載,請註明出處,否則將追究法律責任。

相關文章