0% found this document useful (0 votes)
37 views18 pages

SQL 10 Views

The document discusses views in SQL. It defines a view as a virtual table that presents a subset of data from existing database tables. The document explains that views are created using a SELECT statement to retrieve data. It also lists several things users should be able to do with views, such as create, retrieve data from, alter, insert/update/delete data through, and drop views. An example is provided to demonstrate a view called EMPVU that shows a subset of data from the EMP table.

Uploaded by

misterfarhan0307
Copyright
© © All Rights Reserved
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)
37 views18 pages

SQL 10 Views

The document discusses views in SQL. It defines a view as a virtual table that presents a subset of data from existing database tables. The document explains that views are created using a SELECT statement to retrieve data. It also lists several things users should be able to do with views, such as create, retrieve data from, alter, insert/update/delete data through, and drop views. An example is provided to demonstrate a view called EMPVU that shows a subset of data from the EMP table.

Uploaded by

misterfarhan0307
Copyright
© © All Rights Reserved
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/ 18

(SQL)

Creating Views

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 1
Objectives

• After completing this lesson, you should be


able to do the following:
– Describe a view
– Create a view
– Retrieve data through a view
– Alter the definition of a view
– Insert, update, and delete data through
a view
– Drop a view

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 2
What Is a View?

• A View is a subset of the database that is


presented to one or more users.
• A view is often referred to as a virtual table.
• Views are created by using SELECT statement
that retrieves data from existing table(s).

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 3
What Is a View?
EMP Table
EMPNO
EMPNO ENAME
ENAME JOBJOB
JOB MGR
MGR HIREDATE
HIREDATE SAL
SAL COMM
SAL COMM DEPTNO
COMM DEPTNO
DEPTNO
-----
----- --------
-------
------- ---------
--------------
--------- ---- ---------
----- --------- ------
-----
----- -----
------------
----- -------
-------
7839
7839
7839 KING
KING PRESIDENT
PRESIDENT
PRESIDENT 17-NOV-81
17-NOV-81 5000
5000
5000 10
10
10
7782
7698
7698 CLARK
BLAKE
BLAKE MANAGER
MANAGER
MANAGER 7839
7839 09-JUN-81
01-MAY-81
01-MAY-81 2850
1500
2850 300 30
10
30
7934
7782
7782 MILLER
CLARK
CLARK MANAGER
CLERK
MANAGER 7782
7839
7839 23-JAN-82
09-JUN-81
09-JUN-81 2450
1300
2450 10
10
10
7566
7566
7566 JONES
JONES MANAGER
MANAGER
MANAGER 7839
7839 02-APR-81
02-APR-81 2975
2975
2975 20
20
20
EMPVU10
7788
7654 ViewSALESMAN
7654 SCOTT
MARTIN
MARTIN ANALYST
SALESMAN 7566
7698
7698 09-DEC-82
28-SEP-81
28-SEP-81 1250
3000 1400
1250 1400 30
20
30

EMPNO 7876
7499
7499 ADAMS
ENAME ALLEN
ALLEN SALESMAN
CLERK
JOB
SALESMAN 7788
7698
7698 12-JAN-83
20-FEB-81
20-FEB-81 1600
1100 300
1600 300 30
20
30
------ --------
7369
7844 SMITH
TURNER -----------
SALESMAN
CLERK
7844 TURNER SALESMAN 7902
7698 17-DEC-80
08-SEP-81
7698 08-SEP-81 1500
800
1500 00 30
20
30
7900
7900 JAMES
JAMES CLERK 7698
7698 03-DEC-81 950 30
7839 7521
KING PRESIDENT
CLERK 03-DEC-81 950 30
7521 WARD
WARD SALESMAN
SALESMAN 7698
7698 22-FEB-81
22-FEB-81 1250
1250 500
500 30
30
7782 7902
CLARK
7902 FORD
FORD MANAGER
ANALYST
ANALYST 7566
7566 03-DEC-81
03-DEC-81 3000
3000 20
20
7934 MILLER
7369 SMITH
7369 SMITH CLERK7902 17-DEC-80
CLERK
CLERK 7902 17-DEC-80 800
800 20
20
7788
7788 SCOTT
SCOTT ANALYST
ANALYST 7566
7566 09-DEC-82
09-DEC-82 3000
3000 20
20
7876
7876 ADAMS
ADAMS CLERK
CLERK 7788
7788 12-JAN-83
12-JAN-83 1100
1100 20
20
7934
7934 MILLER
MILLER CLERK
CLERK 7782
7782 23-JAN-82
23-JAN-82 1300
1300 10
10

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 4
Why Use Views?

– To restrict database access


– Converting between units.
– Columns can be renamed.
– Simplifying the construction of complex queries.
– To present different views of the same data
– Providing user security functions.

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 5
Creating a View
– You embed a subquery within the CREATE VIEW statement.

– The subquery can contain complex SELECT syntax.


– The subquery cannot contain an ORDER BY clause.

CREATE
CREATE [OR
[OR REPLACE]
REPLACE] VIEW
VIEW view
view
[(alias[,
[(alias[, alias]...)]
alias]...)]
AS
AS subquery
subquery
[WITH
[WITH CHECK
CHECK OPTION]
OPTION]
[WITH
[WITH READ
READ ONLY]
ONLY]

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 6
Creating a View
– Create a view, EMPVU10, that contains details of
employees in department 10.
SQL> CREATE VIEW empvu10
2 AS SELECT empno, ename, job
3 FROM emp
4 WHERE deptno = 10;
View created.

• Describe the structure of the view by using the


SQL*Plus DESCRIBE command.

SQL>
SQL> DESCRIBE
DESCRIBE empvu10
empvu10

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 7
Creating a View
– Create a view by using column aliases in the
subquery.
SQL> CREATE VIEW salvu30
2 AS SELECT empno EMPLOYEE_NUMBER, ename NAME,
3 sal SALARY
4 FROM emp
5 WHERE deptno = 30;
View created.

– Select the columns from this view by the given


alias names.

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 8
Retrieving Data from a View

SQL> SELECT *
2 FROM salvu30;

EMPLOYEE_NUMBER
EMPLOYEE_NUMBER NAME
NAME SALARY
SALARY
---------------
--------------- ----------
---------- ---------
---------
7698
7698 BLAKE
BLAKE 2850
2850
7654
7654 MARTIN
MARTIN 1250
1250
7499
7499 ALLEN
ALLEN 1600
1600
7844
7844 TURNER
TURNER 1500
1500
7900
7900 JAMES
JAMES 950
950
7521
7521 WARD
WARD 1250
1250

66 rows
rows selected.
selected.

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 9
Querying a View

SQL*Plus
USER_VIEWS
USER_VIEWS
SELECT *
EMPVU10
EMPVU10
FROM empvu10;
SELECT
SELECT empno,
empno, ename,
ename, job
job
FROM
FROM emp
emp
WHERE
WHERE deptno
deptno == 10;
10;
7839 KING PRESIDENT
7782 CLARK MANAGER EMP
7934 MILLER CLERK

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 10
Modifying a View
– Modify the EMPVU10 view by using CREATE OR
REPLACE VIEW clause. Add an alias for each
column name.
SQL> CREATE OR REPLACE VIEW empvu10
2 (employee_number, employee_name, job_title)
3 AS SELECT empno, ename, job
4 FROM emp
5 WHERE deptno = 10;
View created.

– Column aliases in the CREATE VIEW clause are


listed in the same order as the columns in the
subquery.

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 11
Simple Views
and Complex Views

•Feature Simple Views Complex Views

•Number of tables One One or more

•Contain functions No Yes

•Contain groups of data No Yes

•DML through view Yes Not always

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 12
Creating a Complex View
• Create a complex view that contains group functions
to display values from two tables.

SQL> CREATE VIEW dept_sum_vu


2 (name, minsal, maxsal, avgsal)
3 AS SELECT d.dname, MIN(e.sal), MAX(e.sal),
4 AVG(e.sal)
5 FROM emp e, dept d
6 WHERE e.deptno = d.deptno
7 GROUP BY d.dname;
View created.

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 13
Rules for Performing
DML Operations on a View
– You can perform DML operations on simple views.
– You cannot remove a row if the view contains the
following:
• Group functions
• A GROUP BY clause
• The DISTINCT keyword
– You cannot add data if:
• There are NOT NULL columns in the base tables
that are not selected by the view
• Columns defined by expressions

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 14
Using the WITH CHECK OPTION Clause

– You can ensure that DML on the view stays


within the domain of the view by using the WITH
CHECK OPTION clause.
SQL> CREATE OR REPLACE VIEW empvu20
2 AS SELECT *
3 FROM emp
4 WHERE deptno = 20
5 WITH CHECK OPTION;
View created.

• Any attempt to change the department number for


any row in the view will fail because it violates the
WITH CHECK OPTION constraint.

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 15
Denying DML Operations
– You can ensure that no DML operations occur
by adding the WITH READ ONLY option to
your view definition.

SQL> CREATE OR REPLACE VIEW empvu10


2 (employee_number, employee_name, job_title)
3 AS SELECT empno, ename, job
4 FROM emp
5 WHERE deptno = 10
6 WITH READ ONLY;
View created.

• Any attempt to perform a DML on any row in the


view will result in Oracle Server error.

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 16
Removing a View

• Remove a view without losing data because a view is


based on underlying tables in the database.

DROP
DROP VIEW
VIEW view;
view;

SQL> DROP VIEW empvu10;


View dropped.

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 17
Quiz(Tuesday 08-dec-2020)
• Subquery, joins, union, intersect etc

© 2009 Punjab University College of Information Technology (PUCIT) September 8, 2009 Slide 18

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy