|
|
|
How to Create and Manage Views in Oracle
ViewsViews are known as logical tables. They represent the data of one of more tables. A view derives its data from the tables on which it is based. These tables are called base tables. Views can be based on actual tables or another view also. Whatever DML operations you performed on a view they actually affect the base table of the view. You can treat views same as any other table. You can Query, Insert, Update and delete from views, just as any other table. Views are very powerful and handy since they can be treated just like any other table but do not occupy the space of a table. The following sections explain how to create, replace, and drop views using SQL commands. Creating ViewsSuppose we have EMP and DEPT table. To see the empno, ename, sal, deptno, department name and location we have to give a join query like this. select e.empno,e.ename,e.sal,e.deptno,d.dname,d.loc From emp e, dept d where e.deptno=d.deptno; So everytime we want to see emp details and department names where they are working we have to give a long join query. Instead of giving this join query again and again, we can create a view on these table by using a CREATE VIEW command given below create view emp_det as select e.empno, e.ename,e.sal,e.deptno,d.dname,d.loc from emp e, dept d where e.deptno=d.deptno; Now to see the employee details and department names we don’t have to give a join query, we can just type the following simple query. select * from emp_det; This will show same result as you have type the long join query. Now you can treat this EMP_DET view same as any other table. For example, suppose all the employee working in Department No. 10 belongs to accounts department and most of the time you deal with these people. So every time you have to give a DML or Select statement you have to give a WHERE condition like .....WHERE DEPTNO=10. To avoid this, you can create a view as given below
CREATE VIEW accounts_staff AS
SELECT Empno, Ename, Deptno
FROM Emp
WHERE Deptno = 10
WITH CHECK OPTION CONSTRAINT ica_Accounts_cnst;
Now to see the account people you don’t have to give a query with where condition you can just type the following query.
Select * from accounts_staff;
Select sum(sal) from accounst_staff;
Select max(sal) from accounts_staff;
As you can see how views make things easier.
The query that defines the Considering the example above, the following
INSERT INTO Accounts_staff VALUES (110, 'ASHI', 10); However, the following INSERT INTO Accounts_staff VALUES (111, 'SAMI', 30); Creating FORCE VIEWSA view can be created even if the defining query of the view
cannot be executed, as long as the You can only create a view with errors by using the
CREATE FORCE VIEW AS ...; When a view is created with errors, Oracle returns a message and
leaves the status of the view as Replacing/Altering ViewsTo alter the definition of a view, you must replace the view using one of the following methods:
For example, assume that you create the
CREATE OR REPLACE VIEW Accounts_staff AS SELECT Empno, Ename, Deptno FROM Emp WHERE Deptno = 30 WITH CHECK OPTION CONSTRAINT ica_Accounts_cnst; Replacing a view has the following effects:
With some restrictions, rows can be inserted into, updated in, or
deleted from a base table using a view. The following statement inserts a new
row into the INSERT INTO Accounts_staff VALUES (199, 'ABID', 30);
Restrictions on DML operations for views use the following criteria in the order listed:
The constraint created by CREATE VIEW Accounts_staff AS
SELECT Empno, Ename
FROM Emp
WHERE Deptno = 10
WITH CHECK OPTION CONSTRAINT ica_Accounts_cnst;
Considering this view definition, you can update the
If you don’t want any DML operations to be performed on views, create them WITH READ ONLY option. Then no DML operations are allowed on views. Referencing Invalid ViewsWhen a user attempts to reference an invalid view, Oracle returns an error message to the user: ORA-04063: view 'view_name' has errors
This error message is returned when a view exists but is unusable due to errors in its query (whether it had errors when originally created or it was created successfully but became unusable later because underlying objects were altered or dropped). Dropping ViewsUse the SQL command DROP VIEW Accounts_staff; Modifying a Join ViewOracle allows you, with some restrictions, to modify views that involve joins. Consider the following simple view: CREATE VIEW Emp_view AS SELECT Ename, Empno, deptno FROM Emp; This view does not involve a join operation. If you issue the SQL statement: UPDATE Emp_view SET Ename = 'SHAHRYAR' WHERE Empno = 109; then the However, if you create a view that involves a join operation, such as: CREATE VIEW Emp_dept_view AS SELECT e.Empno, e.Ename, e.Deptno, e.Sal, d.Dname, d.Loc FROM Emp e, Dept d /* JOIN operation */ WHERE e.Deptno = d.Deptno AND d.Loc IN ('HYD', 'BOM', 'DEL');
then there are restrictions on modifying either the
A modifiable join view is a view that
contains more than one table in the top-level
Any The following example shows an
UPDATE Emp_dept_view SET Sal = Sal * 1.10 WHERE Deptno = 10; The following UPDATE Emp_dept_view SET Loc = 'BOM' WHERE Ename = 'SAMI'; This statement fails with an
In general, all modifiable columns of a join view must map to
columns of a key-preserved table. If the view is defined using the
So, for example, if the UPDATE Emp_dept_view SET Deptno = 10 WHERE Ename = 'SAMI'; The statement fails because it is trying to update a join column. Deleting from a Join ViewYou can delete from a join view provided there is one and only one key-preserved table in the join. The following DELETE FROM Emp_dept_view
WHERE Ename = 'SMITH';
This In the following view, a CREATE VIEW emp_emp AS
SELECT e1.Ename, e2.Empno, e1.Deptno FROM Emp e1, Emp e2
WHERE e1.Empno = e2.Empno;
If a view is defined using the
CREATE VIEW Emp_mgr AS
SELECT e1.Ename, e2.Ename Mname
FROM Emp e1, Emp e2
WHERE e1.mgr = e2.Empno
WITH CHECK OPTION;
No deletion can be performed on this view because the view involves a self-join of the table that is key preserved. Inserting into a Join ViewThe following INSERT INTO Emp_dept (Ename, Empno, Deptno) VALUES ('ASHU', 119, 40);
The following INSERT INTO Emp_dept (Ename, Empno, Deptno) VALUES ('ASHU', 110, 77);
The following INSERT INTO Emp_dept (Ename, Empno, Deptno) VALUES (110, 'TANNU’, 'BOMBAY'); An Listing Information about VIEWS.To see how many views are there in your schema. Give the following query.Select * from user_views;To see which columns are updatable in join views.Data Dictionaries which shows which columns are updatable.
If you are in doubt whether a view is modifiable, then you can
This might return: OWNER TABLE_NAME COLUMN_NAM UPD ---------- ---------- ---------- --- SCOTT EMP_DEPT EMPNO NO SCOTT EMP_DEPT ENAME NO SCOTT EMP_DEPT DEPTNO NO SCOTT EMP_DEPT DNAME NO SCOTT EMP_DEPT LOC NO 5 rows selected. |
|
|