You are on page 1of 8

VIEWS

 A view is a database object that provides restricted access to the table data

 A view is created on a table which is called base table

Creating a view

 A view is a mirror image of the table

 A view does not store any data

 When we execute a query on the view, it will access the data from the base table

 We can have any number of views on the table

 If we drop the base table, the views are not dropped but they will become invalid

 We can perform DML’s on the views which will affect the base table
 Does the view improve the performance?

View does not improve the performance as they will not store the data

Modifying a view

If we want to display only empno and ename then,

Advantages

1. Column/row level restricted access to the table

2. View does not store the data, as a result no memory is consumed

3. Complex and long queries can be placed in a view for easier reference

Create view v5
As
Select ename…
Union
Select ename… From emp Group by ename Having count(*)>1
Union
Select pname from prods;
select *from v5;

select A. ename, B. tsal


from v5 a, emp_sal b
where a.ename = b.ename;

4. Dropping a view will not drop base table

It will not store the data, as a result no memory is consumed

Changes in the view will affect its base table

 Can we have a view of a view?

Yes, we can have

Types of views (interview question)

1. Updatable views

2. Non-updatable views

3. Simple views

4. Complex views

5. Read-only views

6. Force views

Updateable Views & Non-updatable Views

 If a view allows updates (Insert/Update/Delete) to its base table, then it is called as an

updateable view, if not it is a non-updateable view.

 A view becomes non-updateable when it is having any of the following statements.

 GROUP BY & group function

 JOIN condition
Simple Views & Complex Views

 If a view is having simple SQL statements [SELECT/FROM/WHERE] then it is called

simple view, else it will be a complex view.

 A view will become complex if it is having any of the following statement:-

 GROUP BY & group function

 SUBQUERY

 SET operators

 JOIN condition

Note:

 Most of the complex views are non-updateable, most of the simple views are updatable.

 To check the status of the view

Read-Only Views

 An updateable view can be made as non-updateable by using "Read-Only" clause.

 A read-only view will not allow any of the DML statements on it.

Force Views

 A force view is a view created without its base-table.

 It can be created using "FORCE" keyword.

 The view will be in the IN-VALID state.

 A force view is helpful in creating views before the tables in the production enviornment

while deployment. [avoids errors]


EX:

Non-updateable & complex view

Updatable & Simple view

Read only Views


Force views

"demov" is created in INVALID state

To check whether object is valid/invalid

SQL> ALTER VIEW V_TEST_V COMPILE; -- MANUAL RE-COMPILATION

View operations

Renaming

Dropping

TRUNCATE is not possible on views

Altering
possible, recompiles a view. (RE-CREATES VIEW).(converts INVALID to VALID)

 VIEW & TABLE can't have a same name

 selecting all the views information

 SQL statement for a given view

Note: can we create a view based on more than one table?

Yes , we can create


Constraints on the views

1. View read only constraints with read only

2. View with ‘CHECK’ option.

Generally a view inserts the data even though it cannot see it. If we want a view to

‘INSERT’ only the data which it see, then we should use ‘CHECK OPTION’

*** Anyway the update statement is not applicable with CHECK option

3. View unique key constraints

We can provide unique key constraints on views

4. View primary key constraints

We can provide primary key constraints on views

create view v1
(empno number primary key, ename)
as
select empno, ename from emp;