Professional Documents
Culture Documents
View created.
Once a view has been created, it can be queried exactly like a base table.
Syntax:
Select columnname,columnname from <View_Name>;
Example:
SQL> SELECT *FROM Young_Passengers;
NAME
GEETHA
RAM
SEETHA
Updateable Views:
Views can also be used for data manipulation (i.e. the user can perform the Insert, Update
and Delete operations). Views on which data manipulation can be done are called
Updateable Views. When you give an updateable view name in the Update, Insert or Delete
SQL statement, modifications to data will be passed to the underlying table.
For a view to be updateable, it should meet the following criteria:
Views defined from Single Table:
If the user wants to INSERT records with the help of a view, then the PRIMARY KEY
column/s and all the NOT NULL columns must be included in the view.The user can
UPDATE, DELETE records with the help of a view even if the PRIMARY KEY column and
NOT NULL column/s are excluded from the view definition.
Example:
SQL>CREATE TABLE Employee (Eno INTEGER PRIMARY KEY, Ename
VARCHAR2(20),Address VARCHAR2(50),Job VARCHAR2(20), Date_of_Join
DATE,Salary NUMBER(10), Age INTEGER);
DATE_OF_J SALAR
ENO ENAME ADDRESS JOB OIN Y AGE
ARCHAN KVT
1001 A NAGAR CLERK 1-May-02 35000 25
NAGARJU GANDHI
1002 NA NAGAR TRAINER 1-May-05 45000 28
RAJAJI PROGRAM 10000
1003 RAHUL NAGAR MER 1-May-10 0 45
RAJAJI
1004 SAMPATH NAGAR CLERK 1-May-00 10000 46
NIRUPAM NEHRU PROGRAM 14500
1005 A NAGAR MER 1-May-13 0 25
1006 RAM BALAJI CLERK 1-May-10 25000 30
NAGAR
1 row(s) updated.
A view can be created from more than one table. For the purpose of creating the View
these tables will be linked by a join condition specified in the where clause of the Views
definition. The behavior of the View will vary for Insert, Update, Delete and Select table
operations depending upon the following:
Whether the tables were created using a Referencing clause
Whether the tables were created without any Referencing clause and are actually
standalone tables not related in any way.
View defined from Multiple tables (Which have no Referencing clause):
If a view is created from multiple tables, which were not created using a Referencing
clause (i.e. No logical linkage exists between the tables), then though the PRIMARY KEY
column/s as well as the NOT NULL columns are included in the View definition the views
behavior will be as follows:
The INSERT, UPDATE or DELETE operation is not allowed. If attempted Oracle displays
the followingerror message:
For insert/modify:
ORA-01779: cannot modify a column, which maps to a non-preserved table.
For delete:
ORA-01752: cannot delete from view without exactly one key-preservedtable.
View defined from Multiple tables (Which have been created with a Referencing
clause):
If a view is created from multiple tables, which were created using a Referencing clause
(i.e. a logicallinkage exists between the tables), then though the PRIMARY KEY Column/s
as well as the NOT NULL columns are included in the View definition the views behavior
will be as follows:
An INSERT operation is not allowed.
The DELETE or MODIFY operations do not affect the Master table.
The view can be used to MODIFY the columns of the detail table included in the
view.
If a DELETE operation is executed on the view, the corresponding records from the
detail table will be deleted.
Syntax for creating a Master/Detail View (Join View):
SQL>Create View EmpDept_Vw As
Select a.Empno,a.Ename,a.Salary,a.Deptno,b.Dname From Employee a,DeptDet b
Where a.Deptno=b.Deptno;
View created.
When an INSERT operation is performed using the view
SQL>Insert into EmpDept_Vw values(1127,AbhayShah,10000,10,Technical);
ORA-01776:cannot modify more than one base table through a join view When an MODIFY
operation is performed using the view
SQL>Update EmpDept_Vw set salary=4300 where Empno=1125;
1 row updated.
When an DELETE operation is performed using the view
SQL>Delete From EmpDept_Vw where Empno=1123;
1 row deleted.
Common restrictions on updateable views:
The following condition holds true irrespective of the view being created from a single table
or multiple tables.For the view to be updateable the view definition must not include:
Aggregate functions.
DISTINCT, GROUP BY or HAVING clause.
Sub-queries.
Constants, Strings or Value Expressions like Salary * 2.25.
UNION, INTERSECT or MINUS clause.
If a view is defined from another view, the second view should be updateable.
If the user tries to perform any of INSERT, UPDATE, DELETE operation, on a view which
is created from a non-updateable view Oracle returns the following error message:
For insert/modify/delete:
ORA-01776:data manipulation operation not legal on this view
To Create Read-only View:
In this view, you cannot manipulate the records. Because of this view is created with read
only.
SQL>Create View EmpRO As select * from Employee with Read Only;
View created.
To Create View With Check option:
In this view, you cannot change value of deptno column. Because of this view is created
with checkoption.
View dropped.
Result:
Thus the view has been created and all operations were performed successfully.