The creation, use and precautions of Oracle learning view

202-01-25 The creation of Oracle learning view


One, Oracle version

Oracle    11.2.0.1.0 - 64bit

Tool        SQLPlus

Second, what is the view

1. View, for ease of use, is an encapsulation or alias of complex queries. The definition of the view exists in the data dictionary. .

2. The purpose, advantages and disadvantages of using views.

Purpose: Restrict access to the database, encapsulate complex queries, and provide independent requirements for user needs (not necessarily showing all data to users)

Benefits: Increase the security of the basic table data, because the keywords or group functions used in the creation statement can restrict the user's DML operations on the view.

Disadvantage: The use of views will increase the burden of data, which is a decrease in database performance . Even if the query statement of the view is simple, it may be a complex query with hundreds or thousands of rows under the appearance.

3. Points to note: The view is just a definition and does not store its own data. The data is still stored in the basic table .

                   The view that stores the data is called a snapshot.

Three, create and use views

1. Authorize the scott user (the scott account I used in the exercise). After consulting the DBA, I learned that in the version after 10g, the default view creation permission of the scott user has been recycled.

2. Create a view and use it:

Login: run cmd--->sqlplus /nolog as an administrator

Log in to the database ORCL instance as DBA

SQL>conn sys/sys@orcl as sysdba; 

Grant scott permission to create views

SQL>grant create view to scott;

Then switch to log in to the scott user

SQL>conn scott/tiger

Create a view:

SQL>create view v1 as select deptno,min(sal) sals from emp group by deptno;

View the structure of the view:

SQL>desc v1;

Query all data of view v1:

SAL>select * from v1;

 

Supplement: To force the creation of a view, you need to use the syntax of forcing a view, the Force keyword.

1. As mentioned earlier, the view is a definition that exists in the data dictionary, so the view can exist before the basic table .

语法:create force view v2 as select * from tt;

Will report an error (don't care): Warning: The created view has a compilation error.

2. The status of the query view v2 is INVALID , which is invalid or invalid

select object_name,object_type,status from user_objects where object_name = 'V2';

3. Create table tt

create table tt as select * from emp;

Check the status of view v2 again, it is still INVALID .

4. "Activate" view, need to be called once

select * from v2;

5. Check the status of view v2 for the third time and find that it is already VALID , valid and legal.

Fourth, the execution process of the view

1. The database first finds the definition of view v2

select * from v2;

2. The definition found by the database execution, here is exactly how the view reduces the database performance.

Five, modify and delete views

1. Modify the view. Only need to modify the definition of the view, do not need to change the basic table
Syntax: create or replace view v3 as select * from dept ;

Query view v3: select * from v3;

修改视图v3:create or replace view v3 as select * from dept where deptno = 10;

Query v3, the result of the query has changed, which proves that the view modification is successful.

2. Delete the view. Only need to clear the definition of the view from the data dictionary.

Syntax: drop view v3;

Six, perform DML operations on the view

When the statement that creates a view contains the following conditions, the DML of the view may be restricted (√ means operable, x means inoperable)

 

Serial number Happening delete update insert
1 The statement contains group functions when creating a view:
min(),max(),count(*),avg(),sum()
x x x
2 When creating a view, the statement contains:
group by clause
x x x
3 The deduplication keyword is used in the statement when creating the view: the
distinct keyword
x x x
4 The rownum pseudo column is included in the statement when creating the view x x x
5 When creating a view, the statement contains the expression column:
such as case...when expression
x x
6 There is a not null column in the base table,
but the column does not appear in the view definition
x

 

 

 

 

 

 

 

 

 

 

 

To create a view is to add a Check check constraint.

The option constraint to be checked is used to constrain the where condition to ensure that the selected row data will not change when the update and insert operations are performed.

Syntax 1: with check option [constraint constraint name]

Read-only constraint.

Syntax 2: with read only

Scope of application: Both constraints apply to views or views created based on other views

Compared:

    1. Both are used to limit the update and insert operations performed through the view

    2. When the syntax 1 constraint exists, DML operations are allowed only when the conditions are met; when the syntax 2 constraint exists, DML operations are not allowed under any circumstances, restricting read-only.

Syntax 1 operation example:

SQL> create or replace view tt1 as select * from emp where deptno = 10 with check option constraint tt1_ck;

The view has been created.

SQL> update tt1 set deptno = 11;
update tt1 set deptno = 11
       *
An error occurred in line 1:
ORA-01402: view WITH CHECK OPTION where clause violation

Syntax 2 operation example:

SQL> create or replace view v4 as select * from emp with read only;

SQL> update v4 set ename ='H' where ename ='HELLO';
update v4 set ename ='H' where ename ='HELLO';
An error occurred in line 1:
ORA-42399: Cannot perform DML operation on read-only view

Seven, other knowledge points

1. Embedded view. I saw this in a book written by the company's DBA, and I realized that I also often use the view.

select ename,sal from emp,(select deptno,avg(sal) salary from emp group by deptno) a

 where emp.deptno = a.deptno and emp.sal> a.salary; In
this line of statement, a is the embedded view and only applies to the current statement.

2. rownum pseudo column

rownum starts from 1, when the first corresponding row does not meet the condition, it will be filtered out, and then the first one becomes 1, so the use of pseudo-column needs to start from 1.

Rownum does not exist in the data table. It is calculated by the database. It is sorted according to the order in which the data is retrieved from the database. It is a sequence.

Note when using rownum:

1). Only use <or != without using views;

2). If you want to use between...and, =, >=, >, you must use the view to achieve.

Example 1-No view:

SQL>select count(*) from emp;

  COUNT(*)
----------
         15

SQL> select * from emp where rownum >2;

No rows selected

SQL> select count(*) from emp where rownum <=3;

  COUNT(*)
----------
         3

There are clearly 15 rows of data in the emp table. When using >2, it is not selected. When using <=3, three rows are selected.

Example 2-View

SQL> select * from (select rownum,ename from emp order by 1) where rownum between 2 and 5;

No rows selected

SQL> select rm,ename from (select rownum rm,ename from emp order by 1) where rm between 2 and 5;

        RM ENAME
---------- ----------
         2 SMITH
         3 ALLEN
         4 WARD
         5 JONES

SQL> select rm,ename from (select rownum rm ,ename from emp) where rm = 4;

        RM ENAME
---------- ----------
         4 WARD

By defining an alias for the pseudo column rownum, it becomes illegal and legal, and a temporary view in the current SQL context is created. Operators such as =, between...and can be used.

 

The view is pretty fun.

Guess you like

Origin blog.csdn.net/Ezreal_XLove/article/details/113104443