SQL Views:-: CREATE VIEW View - Name AS SELECT Column - List FROM Table - Name (WHERE Condition)
SQL Views:-: CREATE VIEW View - Name AS SELECT Column - List FROM Table - Name (WHERE Condition)
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:-
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.
AS
SELECT column_list
1. Simple View:- Create a view based on one table. DML operations can be performed
on simple views.
SQL> connect scott/tiger
Connected.
2 as
create view v1
ERROR at line 1:
Connected.
Grant succeeded.
Connected.
2 as
View created.
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
1 row created.
2 where deptno=10;
1 row updated.
1 row deleted.
50 MKTG HYD
10 ACCOUNTING Nellore
20 RESEARCH DALLAS
30 SALES CHICAGO
50 MKTG HYD
10 ACCOUNTING Nellore
20 RESEARCH DALLAS
30 SALES CHICAGO
Table created.
To copy required fields:
2 as
View created.
DEPTNO DNAME
---------- --------------
50 MKTG
10 ACCOUNTING
20 RESEARCH
30 SALES
1 row created.
2 deptno=20;
1 row updated.
1 row deleted.
DEPTNO DNAME
---------- --------------
60 ACCounts
10 ACCOUNTING
20 XXX
30 SALES
60 ACCounts
10 ACCOUNTING Nellore
20 XXX DALLAS
30 SALES CHICAGO
2 LOCATION) as
View created.
1 row created.
2 DEPT_NUMBER=30;
1 row updated.
1 row deleted.
70 Manufacture Hyd
10 ACCOUNTING Nellore
20 XXX DALLAS
30 YYY CHICAGO
70 Manufacture Hyd
10 ACCOUNTING Nellore
20 XXX DALLAS
30 YYY CHICAGO
To copy required fields into new fields with a little bit change:
2 )
3 as
4 select empno,ename,sal,comm,sal+nvl(comm,0)
5 from emp;
View created.
14 rows selected.
1 row created.
10 rows updated.
1 row deleted.
14 rows selected.
2 as
View created.
70 Manufacture Hyd
30 YYY CHICAGO
1 row created.
SQL> update v5 set dname='Accounts' where
2 deptno=30;
1 row updated.
1 row deleted.
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.
2 as
3 select empno,ename,dname,loc
5 dept.deptno;
View created.
14 rows selected.
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
ERROR at line 1:
ORA-01776: cannot modify more than one base table through a join view
2 empno=7934;
ERROR at line 1:
2 as
View created.
SALARY
------
5000
ERROR at line 1:
2 salary=5000;
update cv1 set salary=9999 where
ERROR at line 1:
ERROR at line 1:
As
2 as
View created.
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
ERROR at line 1:
ERROR at line 1:
ERROR at line 1:
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.
As
Select query;
ERROR at line 1:
select * from fv
ERROR at line 1:
Table created.
no rows selected
1 row created.
1 row created.
1 row updated.
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:-
As
Select query;
2 as
delete from mv
ERROR at line 1:
2 100,'Rama');
ERROR at line 1:
ERROR at line 1:
ORA-01732: data manipulation operation not legal on this view
Table dropped.
no rows selected
14 rows selected.
2 as
Table dropped.
14 rows selected.
select * from cv
ERROR at line 1:
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.
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.