0% found this document useful (0 votes)
74 views26 pages

Week 11b Views

1. A view called EMPLOYEE_ was created to display data from the EMP table. 2. Views allow you to present logical subsets or combinations of data without storing duplicate data. Views can restrict data access, simplify complex queries, and provide data independence. 3. Views are not stored separately from their base tables. Views depend on their base tables and reflect changes to the base tables.

Uploaded by

Kenan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views26 pages

Week 11b Views

1. A view called EMPLOYEE_ was created to display data from the EMP table. 2. Views allow you to present logical subsets or combinations of data without storing duplicate data. Views can restrict data access, simplify complex queries, and provide data independence. 3. Views are not stored separately from their base tables. Views depend on their base tables and reflect changes to the base tables.

Uploaded by

Kenan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

SELECT ENAME,CASE

WHEN (JOB='MANAGER') THEN


SELECT empno,ename,job,sal
SAL+SAL*30/100
FROM
WHENemp(JOB='ANALYST') THEN
WHERE job=
SAL+SAL*40/100
SELECT NAME,
WHEN ( empno,ename,job,sal
SELECT jobTHEN
(JOB='CLERK')
SELECT FROM emp
( NVL(MATH,0)+NVL(PHYSICS,0)+NVL(CHEMISTRY,0) ) / 3 AVERAGE FROM
GRADES; SAL+SAL*50/100
FROM emp WHERE empno=7521 )
SELECTAND
* WHERE
FROM
WHEN sal>
EMP WHERE
(JOB='SALESMAN')
job<> HIREDATE
‘SALESMAN’ THEN
AND
BETWEEN ( SELECT
’01-JAN-1980’
SAL+SAL*60/100
SAL < ANY sal FROM
AND
( SELECT emp emp
’01-MAR-1981’
sal FROM
WHEN WHERE WHERE empno=7654
(JOB='PRESIDENT')
job=‘SALESMAN’ )
THEN
SAL+SAL*70/100 ELSE SAL END AS NEW
FROM EMP;

Up to now, we have created many queries but all are lost now…

You may create a view as a table to keep and re-use your


queries.

A view is a logical table and it has a query which based on tables


or views.
What is a View ?
You can present logical subsets or combinations of data by creating views of tables.
A view contains no data but is like a window through which data from tables can be
viewed or changed.

Why Use Views?


To restrict data access
To make complex queries easy
To provide data independence
To preserve different views of the same data
Simple Views and Complex Views

There are two classifications for views : Simple and Complex

The basic difference is related to the DML ( INSERT, UPDATE,DELETE)


A simple view
-Derives data from only one table
-Contains no functions or groups of data
-Can perform DML operations through the view
A Complex view
-Derives data from many tables
-Contains functions or groups of data
-Does not allow DML operations through the view
Creating a View
SYNTAX
CREATE [OR REPLACE ] [FORCE| NOFORCE] VIEW view_name
AS
Sub-query
[WITH CHECK OPTION [CONSTRAINT constraint_name] ]
[WITH READ ONLY [CONSTRAINT constraint_name] ]

EXAMPLE
CREATE VIEW salesman
AS
SELECT * FROM emp WHERE job=‘SALESMAN’ ;

SELECT * FROM salesman ;


OR REPLACE :re-creates the view if it is already exists.

FORCE :creates the view regardless of whether or not the base table exist

NOFORCE :creates the view only if the base tables exist

WITH CHECK OPTION :specifies that only rows accesible to the view can be inserted or
updated

WITH READ ONLY : ensures that no DML operations (INSERT,UPDATE,DELETE)


can be performed on this view
1- Create a view, EMP10, that contains detail of employees in department 10.

CREATE VIEW emp10


AS
SELECT empno,ename,deptno FROM emp
WHERE deptno=10 ;

Describe the structure of the view by using iSQL*Plus DESCRIBE command

DESCRIBE emp10 ;

SELECT * FROM emp10 ;


Guidelines for creating a view

-The subquery that defines a view can contain complex SELECT syntax,

including joins, groups, and subqueries.

-The subquery that defines the view cannot contain an ORDER BY clause.

The ORDER BY clause is specified when you retrieve data from the view.
(In version ORACLE 9i)

-If you do not specify a constraint name for a view created with the

WITH CHECK OPTION,

The system assign a default name in the format SYS_Cn.

-You can use the OR REPLACE option to change the definition of the view

without dropping and re-creating it.


Create a view by using column aliases in the subquery
CREATE VIEW sal20 CREATE VIEW sal20
AS AS
SELECT empno, SELECT empno as NO,
ename, ename as EMPLOYEE,
sal*12 sal*12 as ANNUAL
FROM emp FROM emp
WHERE deptno=20 ; WHERE deptno=20 ;

View created .

You can control the column name by including column aliases within a subquery.

2- This example creates a view containing the employee number with the alias NO, name
(ename) with the alias EMPLOYEE, and annual salary (sal) with the alias ANNUAL for every
employees in department 20.
As an alternative, you can use an alias after CREATE statement and prior to the SELECT
subquery.
The number of aliases listed match the number of expressions selected in the subquery.

CREATE VIEW sal20 (NO,EMPLOYEE,ANNUAL)


AS
SELECT empno, ename, sal*12
FROM emp
WHERE deptno=20 ;
Retrieving Data from a View

SELECT * FROM sal20 ;


Modifying a View
To Display all views on the current user. SELECT * FROM user_views

3-Modify the EMP10 view by using


CREATE OR REPLACE VIEW clause.
Change deptno=10 to deptno=30 ;

CREATE VIEW emp10 AS SELECT empno,ename,deptno FROM emp WHERE deptno=30;

CREATE OR REPLACE VIEW emp10 AS SELECT empno,ename,deptno FROM emp WHERE deptno=30;
View created.

SELECT * FROM user_views


Inserting rows to a View
You can insert, update and delete rows to simple views ONLY.

SELECT * FROM USER_VIEWS

Sal20 has a function

Job_salary has a groups of data.

INSERT INTO emp10 VALUES(7777,’NURLAN’,30)

SELECT * FROM emp ; SELECT * FROM emp10;


INSERT INTO emp10 VALUES(8888,’ASEL’,20)

SELECT * FROM emp ;


SELECT * FROM emp10;

Why is ASEL not displayed ?


Deleting rows from a view

SELECT * FROM emp ; SELECT * FROM emp10;

DELETE FROM emp10 where ename=‘NURLAN’ ;


1 row deleted

DELETE FROM emp10 where ename=‘ASEL’ ;

0 row deleted
Updating rows in a view

SELECT * FROM emp ; SELECT * FROM emp10;

UPDATE EMP10 SET EMPNO=1 WHERE ename=‘NURLAN’ ;


1 row updated

UPDATE EMP10 SET EMPNO=2 WHERE ename=‘ASEL’ ;

0 row deleted
Creating a Complex View

Create a complex view that contains group functions to display values from table.

CREATE VIEW job_salary AS SELECTjob,SUM(sal) FROM emp GROUP BY job ;

CREATE VIEW job_salary AS SELECT job,SUM(sal) as SALARY FROM emp GROUP BY job ;

SELECT * FROM job_salary

DESC job_salary
DELETING DATA
You can perform DML operations on simple views.

You can not remove a row if the view containts the following :
-Group function
-A GROUP BY clause
-The DISTINCT keyword
-The ROWNUM keyword

MODIFY DATA

You can not modify data in a view if it contains :


-Group function
-A GROUP BY clause
-The DISTINCT keyword
-The ROWNUM keyword
-Columns defined by expressions

MODIFY DATA

You can not add through a view if the view includes :


-Group function
-A GROUP BY clause
-The DISTINCT keyword
-The ROWNUM keyword
-Columns defined by expressions
-NOT NULL columns in the base tables that are not selected by the view.
Using the WITH CHECK OPTION clause
You can ensure that DML operations performed on the view stay within the domain of the
View by using the WITH CHECK OPTION clause.

SELECT * FROM CLERKS ;


CREATE VIEW clerks
AS
SELECT * FROM emp WHERE job=‘CLERK’
WITH CHECK OPTION CONSTRAINT CLERKS_CK ;

INSERT INTO CLERKS VALUES


(2222,'ASET','SALESMAN',8888,'10-JAN-1988',800,NULL,20);

INSERT INTO CLERKS


VALUES (2222,'ASET',‘CLERK',8888,'10-JAN-1988',800,NULL,20);

SELECT * FROM CLERKS ;

Any attempt to change the JOB for any row in the view fails because it violates
The WITH CHECK OPTION constraint.
SELET * FROM CLERKS ;

UPDATE CLERKS SET JOB=‘SALESMAN’

UPDATE CLERKS SET DEPTNO=60

SELET * FROM CLERKS ;

If there is an attempt to perform DML operations on rows that the view has not selected,
An error is displayed, with the constraint name if that has been specified.
Denying DML Operations

You can ensure that no DML operations occur by adding the WITH READ ONLY option to
your view definition.
Any attempt to perform a DML on any row in the view results in an Oracle Server error.

CREATE VIEW EMP_COM_NULL DELETE FROM EMP_COM_NULL;


AS
SELECT * FROM emp WHERE comm IS NULL
WITH READ ONLY ;
UPDATE EMP_COM_NULL
SELECT * FROM EMP_COM_NULL; SET DEPTNO=100

INSERT INTO EMP_COM_NULL (EMPNO)


VALUES(5555)
Removing a View

You can remove a view without losing data because a view is based on underlying tables
in the database.

DROP VIEW emp10 ;


TASKS
1-Create a view called EMPLOYEE_1 based on the employee numbers,
employee name and surname, and department numbers from the EMPLOYEES table.
Change the heading for the firstname+lastname to EMPLOYEE.

2- Display the contents of the EMPLOYEE_1 view.

3- Select the view name and text from the USER_VIEWS data dictionary view
4- Using your EMPLOYEE_1 view, write a query to display all employes names and department
number.

5-Create a view DEPT50 that contains the employee number, employee last names,and
department numbers for all employees in department 50 in EMPLOYEES table.
Label the view columns EMPNO,EMPLOYEE, AND DEPTNO
Don’t allow an employee to be reassigned to another department through the view.

6- Display the structure and contents of the DEPT50 view


7- Attempt to reassign “Matos” to department 80

8- Create a view called SALARY_1 based on the employee last names, department names,

salaries, And salary grades for all employees.

Use the EMPLOYEES, DEPARTMENTS,and JOB_GRADES tables.

Label the columns Employee,Department, Salary,and the Grade, respectiveley.

9-Create a view which returns all employees have not got any commission in department 30
in EMP. Nobody can delete,or update any rows and insert any rows to that view.

You might also like