View (learning notes), view learning notes

Source: Internet
Author: User

View (learning notes), view learning notes
ViewA view is a table exported from one or more entity tables (or views. Unlike an object table, a view itself is a virtual table that does not contain any real data. The database only stores view definitions, but does not Store View data. The data is still stored in the original object table. Therefore, the data in the object table changes, and the data queried from the view changes accordingly. In this sense, a view is like a window through which you can see the data that you are interested in and its changes in the database. Advantages of a view:

  • A view can simplify user operations;
  • View allows you to view the same data from multiple perspectives;
  • Views provide a certain degree of logical independence for restructured databases;
  • The view can provide security protection for confidential data;
  • View can be used to clearly express the query;
Create view syntax

CREATE [FORCE | NOFORCE] [or replace] VIEW name [(alias 1, alias 2,...)]

AS

Subquery;

Syntax parameters:The main parameters in creating a view are described as follows: FORCE:Indicates that the table to be created does not exist or you can create a view; NOFORCE:(Default) indicates that the table to be created must exist; otherwise, the table cannot be created; Or replace:It indicates the replacement of the view. If the created view does not exist, a new view is created. If the created view already exists, it is replaced.

Authorize the permission to create a view

 

Grant create view to tests; -- tests is the user

 

Simple View:

Example 1,Create a view with a basic salary greater than 2000

CREATE VIEW v_empviewASSELECT * FROM emp WHERE sal>2000;

Check whether the view is successfully created.

SELECT * FROM tab WHERE tabtype='VIEW';

Query v_empview

SELECT * FROM v_empview;

Query View Details

SELECT view_name,text_length,text FROM User_Views;

Example 2,Create a view that only contains information about employees in 20 departments

CREATE OR REPLACE VIEW v_emp20ASSELECT * FROM emp WHERE deptno=20;
-- Query whether the creation is successful SELECT * FROM tab WHERE tabtype = 'view'; -- query the myemponly_dept20 view select * FROM v_myemponly_dept20;
Execute DML operations in the view

Example 3,

Create a view that only contains information about employees in 20 departments, and add a new one.

CREATE OR REPLACE VIEW v_emp20ASSELECT empno,ename,job,sal,deptno FROM emp WHERE deptno=20;
-- Query v_myview SELECT * FROM v_emp20;
-- Add a data insert into v_emp20 (EMPNO, ENAME, JOB, sal, deptno) VALUES (8888, 'java', 'cler',) to the v_emp20 view; COMMIT; -- Query v_myview SELECT * FROM v_emp20; -- the result is successfully added.

Example 4: Modify the view

UPDATE v_emp20 SET ename = 'oracle', job = 'manager', sal = 2000 WHERE empno = 8888; COMMIT; -- Query v_myview SELECT * FROM v_emp20;

Example 5,Delete data from v_emp20 View

Delete from v_emp20 WHERE empno = 8888; COMMIT; -- Query v_myview SELECT * FROM v_emp20;
Complex View

Views that contain Mappings of multiple tables

Example 6,Show Details of each department

Create or replace view v_myviewASSELECT d. deptno, d. dname, d. loc, COUNT (e. empno) COUNT, NVL (ROUND (AVG (sal), 2), 0) AVG, NVL (SUM (sal), 0) SUM, NVL (MAX (sal), 0) MAX, NVL (MIN (sal), 0) MINFROM emp e, dept dWHERE e. deptno (+) = d. deptnoGROUP BY d. deptno, d. dname, d. loc -- Query v_myview SELECT * FROM v_myview;

Example 7,Alias for the queried column in the view

Create or replace view v_myview (Department ID, Department name, Department location, number of people, average salary, total salary, maximum salary, minimum wage) ASSELECT d. deptno, d. dname, d. loc, COUNT (e. empno) COUNT, NVL (ROUND (AVG (sal), 2), 0) AVG, NVL (SUM (sal), 0) SUM, NVL (MAX (sal), 0) MAX, NVL (MIN (sal), 0) MINFROM emp e, dept dWHERE e. deptno (+) = d. deptnoGROUP BY d. deptno, d. dname, d. loc -- Query v_myview SELECT * FROM v_myview;


For a simple view, you can directly execute INSERT, UPDATE, and DELETE operations.
However, for complex views, direct INSERT, UPDATE, and DELETE operations are not allowed. DML operations cannot be performed directly when the view meets any of the following conditions. The details are as follows:
1) has a set operator (UNION, union all, INTERSECT, MINUS );
2) Grouping functions (MIN, MAX, SUM, AVG, COUNT, etc );
3) has clauses such as group by, connect by, or start;
4) has the DISTINCT keyword;
5) have connection query;
To execute DML operations on complex views in the preceding situations, a trigger must be requisitioned to complete the operations.

 

With check option clause ---- ensure that the view creation conditions are not changed

When creating a view, you sometimes need to use some WHERE clauses to limit some conditions,
However, after a view is created by default, you can use the view to modify the field Content Used in the WHERE clause,
At this time, the with check option clause must be used to ensure that the view creation conditions are not updated.
Syntax:

With check option clause CREATE [FORCE | NOFORCE] [or replace] VIEW name [(alias 1, alias 2,...)] AS subquery [with check option [CONSTRAINT name];

Example 8,Create a view containing only 20 Department members. Add constraints and the Department number cannot be changed.

Create or replace view v_emp20ASSELECT * FROM emp WHERE deptno = 20 with check option constraint v_emp20_ck; --- SELECT * FROM v_emp20 in the v_myview VIEW after the constraint is optional OR not written; -- if the data is updated and the Department number is changed to another one, an error is reported and the where condition UPDATE v_emp20 SET deptno = 40 WHERE empno = 7369 is returned;


With read only clause -- make all fields in the view unupdatable

Syntax:

 

With read only clause CREATE [FORCE | NOFORCE] [or replace] VIEW name [(alias 1, alias 2,...)] AS subquery [with check option [CONSTRAINT name] [with read only];

 

Example 8,Create a view containing only 20 employees and set the constraint to read-only

Create or replace view v_emp20ASSELECT * FROM emp WHERE deptno = 20 with read only; -- set to READ-ONLY attribute -- Query v_myview view select * FROM v_emp20; -- if the data is updated now, an error is reported and the where condition UPDATE v_emp20 SET ename = 'ttttt', comm = null where empno = 7369 is returned;

Delete View

Drop view name

Example 9,Delete v_myview

DROP VIEW v_myview;
-- Query the user_views data dictionary SELECT view_name, text_length, text, read_only FROM user_views;

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.