110.Oracle数据库SQL开发之 视图——创建并使用视图
欢迎转载,转载请标明出处:http://blog.csdn.net/notbaron/article/details/50043089
视图实际上是一个或多个表上的预定义查询,这些表称为基表(base table). 从视图中检索信息与从表中检索信息的方法完全相同。
视图中并不存储着行,它们始终存储在表中。
User_tables,user_sequences和user_indexes都是视图。
视图有以下优点:
n 可以将复杂查询编写为视图,并授权用户访问视图的权限
n 限制用户只能访问视图,阻止用户直接查询基表
n 限制某个视图只能访问基表中的某些行
视图可以使用CREATE VIEW语句创建,其简化语法如下:
CREATE [OR REPLACE] VIEW [{FORCE |NOFORCE}] VIEW view_name
[(alias_name[, alias_name …])] AS subquery
[WITH {CHECK OPTION | READ ONLY} CONSTRAINTconstraint_name];
用户必须具有CREATE VIEW特权才能创建视图。
grant create view to store;
store@PDB1> create view cheap_products_view asselect * from products where price < 15;
View created.
创建视图employees_view,其子查询检索employees表中除salary之外的所有列:
store@PDB1> create view employees_view as selectemployee_id,manager_id,first_name,last_name,title from employees;
View created.
查询从cheap_products_view检索行:
store@PDB1> select product_id,name,price fromcheap_products_view;
PRODUCT_ID NAME PRICE
---------- ----------------------------------------
4 Tank War 13.95
6 2412: The Return 14.95
7 Space Force 9 13.49
8 From Another Planet 12.99
9 Classical Music 10.99
11Creative Yell 14.99
12My Front Line 13.49
7 rows selected.
store@PDB1> select * from employees_view;
EMPLOYEE_ID MANAGER_ID FIRST_NAMELAST_NAME TITLE
----------- ---------- -------------------- --------------------
1 James Smith CEO
2 1 Ron Johnson Sales Manager
3 2 Fred Hobbs Salesperson
4 2 Susan Jones Salesperson
对cheap_products_view也可以执行DML操作。
只能对简单视图执行DML操作;复杂视图不支持DML操作。
通过为视图添加CHECK OPTION 约束,可以指定对视图执行的DML操作必须满足子查询的条件。
store@PDB1> create view cheap_products_view2 asselect * from products where price < 15 with check option constraintcheap_products_view2_price;
View created.
此时插入会报错:
store@PDB1> insert into cheap_products_view2(product_id,product_type_id,name,price) values ( 15,1,'Southern Front',19.50);
insert into cheap_products_view2( product_id,product_type_id,name,price)values ( 15,1,'Southern Front',19.50)
*
ERROR at line 1:
ORA-01402: view WITH CHECK OPTIONwhere-clause violation
通过为视图添加READ ONLY约束,可以指定该视图是只读的。
如下:
store@PDB1> create view cheap_products_view3 asselect * from products where price < 15 with read only constraintcheap_products_view3_read_only;
View created.
插入如下:
store@PDB1> insert into cheap_products_view3 (product_id,product_type_id,name,price ) values ( 16,1,'Northern Front',19.50);
insert into cheap_products_view3 (product_id,product_type_id,name,price ) values ( 16,1,'Northern Front',19.50)
*
ERROR at line 1:
ORA-42399: cannot perform a DML operationon a read-only view
使用DESCRIBE命令可以获取有关视图定义的信息。
如下:
store@PDB1> describe cheap_products_view3;
Name Null? Type
------------------------------------------------------------- ------------------------------------
PRODUCT_ID NOT NULL NUMBER(38)
PRODUCT_TYPE_ID NUMBER(38)
NAME NOT NULL VARCHAR2(30)
DESCRIPTION VARCHAR2(50)
PRICE NUMBER(5,2)
要查看text列中存储的整个视图定义,可以使用SQLPLUS命令SET LONG,设置当前检索LONG列时SQLPLUS显示的字符数。
store@PDB1> set long 200
store@PDB1> select view_name,text_length,text fromuser_views order by view_name;
VIEW_NAME
----------------------------------------------------------------------------------------------------
TEXT_LENGTH TEXT
-------------------------------------------------------------------------------------------
CHEAP_PRODUCTS_VIEW
97 select"PRODUCT_ID","PRODUCT_TYPE_ID","NAME","DESCRIPTION","PRICE"from products
where price < 15
CHEAP_PRODUCTS_VIEW2
116select"PRODUCT_ID","PRODUCT_TYPE_ID","NAME","DESCRIPTION","PRICE"from products
where price < 15 with check option
CHEAP_PRODUCTS_VIEW3
113select"PRODUCT_ID","PRODUCT_TYPE_ID","NAME","DESCRIPTION","PRICE"from products
where price < 15 with read only
EMPLOYEES_VIEW
71 selectemployee_id,manager_id,first_name,last_name,title from employees
从user_constraints视图中可以获取有关视图约束的信息:
store@PDB1>select constraint_name,constraint_type,status,deferrable,deferred fromuser_constraints
where table_name in('CHEAP_PRODUCTS_VIEW2','CHEAP_PRODUCTS_VIEW3') order by constraint_name;
CONSTRAINT_NAME
----------------------------------------------------------------------------------------------------
C STATUS DEFERRABLE DEFERRED
- ---------------------- ---------
CHEAP_PRODUCTS_VIEW2_PRICE
V ENABLED NOT DEFERRABLE IMMEDIATE
CHEAP_PRODUCTS_VIEW3_READ_ONLY
O ENABLED NOT DEFERRABLE IMMEDIATE
Constraint_type中的V 对应的是CHECKOPTION约束。
O表示 READ ONLY约束。
复杂视图包含的子查询具有以下特点:
l 从多个基表中检索数据
l 使用GROUP BY 或 DISTINCT子句对记录分组
l 包含函数调用
如下:
store@PDB1> create view products_and_types_view as
select p.product_id,p.nameproduct_name,pt.name product_type_name,p.price from products p full outer joinproduct_types pt
using(product_type_id) order by p.product_id;
View created.
进行查询:
store@PDB1> select * from products_and_types_view;
PRODUCT_ID PRODUCT_NAME PRODUCT_TY PRICE
---------- ---------------------------------------- ----------
1 Modern Science Book 40
2 New Chemistry Book 35
3 Supernova Book 25.99
4 Tank War Video 13.95
5 Z Files Video 49.99
6 2412: The Return Video 14.95
7 Space Force 9 DVD 13.49
8 From Another Planet DVD 12.99
9 Classical Music CD 10.99
10Pop 3 CD 15.99
11Creative Yell CD 14.99
12My Front Line 13.49
13Lunar Landing Video 15.99
14Submarine Video 15.99
15Airplane Video 15.99
Magazine
16 rows selected.