SQL Views:-
A VIEW is a virtual table, through which a selective portion of the data from one or more
tables can be seen. Views do not contain data of their own. They are used to restrict access
to the database or to hide data complexity. A view is stored as a SELECT statement in the
database. DML operations on a view like INSERT, UPDATE, DELETE affects the data in
the original table upon which the view is based.
Advantages:-
1. Restricting access to the database.
2. Selecting from a view can display restricted portion of database.
3. Allow user to make simple queries. For ex:- Views allow user to select the info from
multiple tables without knowing how to perform a join.
The Syntax to create a sql view is
CREATE VIEW view_name
AS
SELECT column_list
FROM table_name [WHERE condition];
view_name is the name of the VIEW.
The SELECT statement is used to define the columns and rows that you want to display
in the view.
There are 5 types of views
1. Simple View:- Create a view based on one table. DML operations can be performed
on simple views.
SQL> connect scott/tiger
Connected.
SQL> create view v1
2 as
3 select * from dept;
create view v1
ERROR at line 1:
ORA-01031: insufficient privileges
SQL> connect system/manager
Connected.
SQL> grant all privileges to scott;
Grant succeeded.
SQL> connect scott/tiger
Connected.
To copy all fields
SQL> create view v1
2 as
3 select * from dept;
View created.
SQL> select * from v1;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> insert into v1 values(50,'MKTG','HYD');
1 row created.
SQL> update v1 set loc='Nellore'
2 where deptno=10;
1 row updated.
SQL> delete from v1 where deptno=40;
1 row deleted.
SQL> select * from v1;
DEPTNO DNAME LOC
---------- -------------- -------------
50 MKTG HYD
10 ACCOUNTING Nellore
20 RESEARCH DALLAS
30 SALES CHICAGO
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
50 MKTG HYD
10 ACCOUNTING Nellore
20 RESEARCH DALLAS
30 SALES CHICAGO
Sql>create table dept1 as select * from dept;
Table created.
To copy required fields:
SQL> create view v2
2 as
3 select deptno,dname from dept1;
View created.
SQL> select * from v2;
DEPTNO DNAME
---------- --------------
50 MKTG
10 ACCOUNTING
20 RESEARCH
30 SALES
SQL> insert into v2 values(60,'ACCounts');
1 row created.
SQL> update v2 set dname='XXX' where
2 deptno=20;
1 row updated.
SQL> delete from v2 where deptno=50;
1 row deleted.
SQL> select * from v2;
DEPTNO DNAME
---------- --------------
60 ACCounts
10 ACCOUNTING
20 XXX
30 SALES
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
60 ACCounts
10 ACCOUNTING Nellore
20 XXX DALLAS
30 SALES CHICAGO
To copy required fields into new fields:
SQL> create view v3(DEPT_NUMBER,DEPTNAME,
2 LOCATION) as
3 select deptno,dname,loc from dept1;
View created.
SQL> insert into v3 values(70,'Manufacture','Hyd');
1 row created.
SQL> update v3 set DEPTNAME='YYY' where
2 DEPT_NUMBER=30;
1 row updated.
SQL> delete from v3 where dept_number=60;
1 row deleted.
SQL> select * from v3;
DEPT_NUMBER DEPTNAME LOCATION
----------- -------------- -------------
70 Manufacture Hyd
10 ACCOUNTING Nellore
20 XXX DALLAS
30 YYY CHICAGO
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
70 Manufacture Hyd
10 ACCOUNTING Nellore
20 XXX DALLAS
30 YYY CHICAGO
To copy required fields into new fields with a little bit change:
SQL> create view v4(empno,ename,sal,comm,NET_SALARY
2 )
3 as
4 select empno,ename,sal,comm,sal+nvl(comm,0)
5 from emp;
View created.
SQL> select * from v4;
EMPNO ENAME SAL COMM NET_SALARY
---------- ---------- ---------- ---------- ----------
7369 SMITH 800 800
7499 ALLEN 1600 300 1900
7521 WARD 1250 500 1750
7566 JONES 2975 2975
7654 MARTIN 1250 1400 2650
7698 BLAKE 2850 2850
7782 CLARK 2450 2450
7788 SCOTT 3000 3000
7839 KING 5000 5000
7844 TURNER 1500 0 1500
7876 ADAMS 1100 1100
EMPNO ENAME SAL COMM NET_SALARY
---------- ---------- ---------- ---------- ----------
7900 JAMES 950 950
7902 FORD 3000 3000
7934 MILLER 1300 1300
14 rows selected.
SQL> insert into v4(empno,ename,sal,comm) values(100,'Ram',3000,100);
1 row created.
SQL> update v4 set comm=999 where comm is null;
10 rows updated.
SQL> delete from v4 where comm=0;
1 row deleted.
SQL> select * from v4;
EMPNO ENAME SAL COMM NET_SALARY
---------- ---------- ---------- ---------- ----------
100 Ram 3000 100 3100
7369 SMITH 800 999 1799
7499 ALLEN 1600 300 1900
7521 WARD 1250 500 1750
7566 JONES 2975 999 3974
7654 MARTIN 1250 1400 2650
7698 BLAKE 2850 999 3849
7782 CLARK 2450 999 3449
7788 SCOTT 3000 999 3999
7839 KING 5000 999 5999
7876 ADAMS 1100 999 2099
EMPNO ENAME SAL COMM NET_SALARY
---------- ---------- ---------- ---------- ----------
7900 JAMES 950 999 1949
7902 FORD 3000 999 3999
7934 MILLER 1300 999 2299
14 rows selected.
To copy required records only as per where condition:
SQL> create view v5
2 as
3 select * from dept where deptno>20;
View created.
SQL> select * from v5;
DEPTNO DNAME LOC
---------- -------------- -------------
70 Manufacture Hyd
30 YYY CHICAGO
SQL> insert into v5 values(80,'Sales','Guntur');
1 row created.
SQL> update v5 set dname='Accounts' where
2 deptno=30;
1 row updated.
SQL> delete from v5 where deptno=70;
1 row deleted.
SQL> select * from v5;
DEPTNO DNAME LOC
---------- -------------- -------------
80 Sales Guntur
30 Accounts CHICAGO
1. Complex View:-
Complex views comes with the help of group functions. At the time of using group
functions we have to use column aliases. We can’t perform DML operations on
Complex Views. Using Joins we can create complex views.
SQL> create view cv
2 as
3 select empno,ename,dname,loc
4 from emp,dept where emp.deptno=
5 dept.deptno;
View created.
SQL> select * from cv;
EMPNO ENAME DNAME LOC
----- ---------- -------------- -------------
7369 SMITH RESEARCH DALLAS
7499 ALLEN SALES CHICAGO
7521 WARD SALES CHICAGO
7566 JONES RESEARCH DALLAS
7654 MARTIN SALES CHICAGO
7698 BLAKE SALES CHICAGO
7782 CLARK ACCOUNTING NEW YORK
7788 SCOTT RESEARCH DALLAS
7839 KING ACCOUNTING NEW YORK
7844 TURNER SALES CHICAGO
7876 ADAMS RESEARCH DALLAS
EMPNO ENAME DNAME LOC
----- ---------- -------------- -------------
7900 JAMES SALES CHICAGO
7902 FORD RESEARCH DALLAS
7934 MILLER ACCOUNTING NEW YORK
14 rows selected.
SQL> select * from dept;
DEPTNO DNAME LOC
------ -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> insert into cv values(100,'Rani','XXX','Hyd');
insert into cv values(100,'Rani','XXX','Hyd')
ERROR at line 1:
ORA-01776: cannot modify more than one base table through a join view
SQL> update cv set dname='yyy' where
2 empno=7934;
update cv set dname='yyy' where
ERROR at line 1:
ORA-01779: cannot modify a column which maps to a non key-preserved table
SQL> create view cv1
2 as
3 select max(sal) SALARY from emp;
View created.
SQL> select * from cv1;
SALARY
------
5000
SQL> insert into cv1 values(7000);
insert into cv1 values(7000)
ERROR at line 1:
ORA-01732: data manipulation operation not legal on this view
SQL> update cv1 set salary=9999 where
2 salary=5000;
update cv1 set salary=9999 where
ERROR at line 1:
ORA-01732: data manipulation operation not legal on this view
SQL> delete from cv1;
delete from cv1
ERROR at line 1:
ORA-01732: data manipulation operation not legal on this view
Readonly View:- In this we cannot perform any DML operations.
Syntax:- create view viewname
As
Select query with read only;
SQL> create view rov
2 as
3 select * from dept with read only;
View created.
SQL> select * from rov;
DEPTNO DNAME LOC
------ -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> delete from rov;
delete from rov
ERROR at line 1:
ORA-42399: cannot perform a DML operation on a read-only view
SQL> update rov set dname='XXX';
update rov set dname='XXX'
ERROR at line 1:
ORA-42399: cannot perform a DML operation on a read-only view
SQL> insert into rov values(50,'RTR','XXX');
insert into rov values(50,'RTR','XXX')
ERROR at line 1:
ORA-42399: cannot perform a DML operation on a read-only view
Force View:- If the base table is not available also we can create the view. That view is called
force view. After creating the base table we can perform DML operations.
Syn:- create force view viewname
As
Select query;
SQL> select * from student;
select * from student
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> create force view fv
2 as
3 select * from student;
Warning: View created with compilation errors.
SQL> select * from fv;
select * from fv
ERROR at line 1:
ORA-04063: view "SCOTT.FV" has errors
SQL> create table student(sno number(3));
Table created.
SQL> select * from fv;
no rows selected
SQL> insert into fv values(100);
1 row created.
SQL> insert into fv values(101);
1 row created.
SQL> update fv set sno=102 where sno=101;
1 row updated.
SQL> delete from fv where sno=100;
1 row deleted.
Materialized View:-At the time of creating the view base table is needed. After that if the base
table is not available also view becomes valid. But remaining views are not valid.
Syn:-
create materialized view viewname
As
Select query;
SQL> create materialized view mv
2 as
3 select * from emp1;
Materialized view created.
SQL> delete from mv;
delete from mv
ERROR at line 1:
ORA-01732: data manipulation operation not legal on this view
SQL> insert into mv(empno,ename) values(
2 100,'Rama');
insert into mv(empno,ename) values(
ERROR at line 1:
ORA-01732: data manipulation operation not legal on this view
SQL> update mv set ename='xxx';
update mv set ename='xxx'
ERROR at line 1:
ORA-01732: data manipulation operation not legal on this view
SQL> drop table emp1;
Table dropped.
SQL> select * from mv;
no rows selected
SQL> select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ---- --------- ---- ---- ------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ---- --------- ---- ---- ------
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
14 rows selected.
SQL> create materialized view mv1
2 as
3 select * from emp;
Materialized view created.
SQL> drop table emp;
Table dropped.
SQL> select * from mv1;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ---- --------- ---- ---- ------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ---- --------- ---- ---- ------
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
14 rows selected.
SQL> select * from cv;
select * from cv
ERROR at line 1:
ORA-04063: view "SCOTT.CV" has errors
Note1: Create or replace is used when we want to create a view that already exists otherwise
only Create is enough.
Note2: If there is check condition, it enforces the user to insert or update the data that satisfies
the query condition. Else the user can insert or update the rows with new data.
When views become invalid:-
If the drop the base table view is invalid.
If you change the base table column name.
If you rename the base table.
1. To display all views:
Ex: Select * from user_views;
1. To display all viewnames:
Ex: Select view_name from user_views;
2. To display a particular view:
Ex: Select * from viewname;
3. To display view structure:
Ex: Desc viewname;
DROPPING A VIEW:
Syntax: Drop view viewname;
When a view is dropped, it never effects the base table.
Ex:-
Sql>Drop view cv1;
View dropped.