0% found this document useful (0 votes)
78 views20 pages

Creating Views: Dept. of Computer Science Faculty of Science and Technology

The document discusses views in a database course. It defines a view as a logical representation of data from one or more tables. The lesson outline covers describing and creating views, retrieving data through views, altering view definitions, and inserting, updating, deleting data via views. The document provides an example of an EMP table and EMPVU10 view that represents a subset of EMP data.

Uploaded by

Maruf Shahriar
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)
78 views20 pages

Creating Views: Dept. of Computer Science Faculty of Science and Technology

The document discusses views in a database course. It defines a view as a logical representation of data from one or more tables. The lesson outline covers describing and creating views, retrieving data through views, altering view definitions, and inserting, updating, deleting data via views. The document provides an example of an EMP table and EMPVU10 view that represents a subset of EMP data.

Uploaded by

Maruf Shahriar
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/ 20

Creating Views

Course Code: CSC 2108 Course Title: Introduction to Database

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: 17 Week No: 10 Semester: Fall


Lecturer: Kazi Sadia
Lecture Outline
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
Database Objects

Object Description

Table Basic unit of storage; composed of rows


and columns

View Logically represents subsets of data from


one or more tables

Sequence Generates primary key values

Index Improves the performance of some queries

Synonym Alternative name for an object


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

EMPNO 7876
7499
7499 ADAMS
ENAME
7876 ALLEN
ALLEN SALESMAN
ADAMS CLERK
JOB
SALESMAN
CLERK 7788
7698
7698 12-JAN-83
7788 20-FEB-81
12-JAN-83
20-FEB-81 1600
1100
1100 300
1600 300 30
20
30
20
------ 7369
--------
7369
7844 SMITH
TURNER
7844 SMITH -----------
SALESMAN
CLERK
TURNER SALESMAN
CLERK 7902
7698
7902 17-DEC-80
08-SEP-81
7698 17-DEC-80
08-SEP-81 1500
800
1500
800 00 30
20
30
20
7902
7900
7900 FORD
JAMES
JAMES CLERK
ANALYST
ANALYST 7566
7698
7698 03-DEC-81 3000
950 30
20
7839 7698
KING
7902
7521
FORD
PRESIDENT
CLERK 7566 03-DEC-81 3000
950 30
20
7521 BLAKE
7698 WARD
BLAKE
WARD SALESMAN
MANAGER
MANAGER 7839
SALESMAN 7698
7698 01-MAY-81
7839 22-FEB-81
01-MAY-81
22-FEB-81 1250
2850
2850 500
1250 500 30
30
30
30
7782 7654
CLARK
7902
7902 MARTIN
7654 FORD
MARTIN
FORD MANAGER
ANALYST
SALESMAN
ANALYST
SALESMAN 7698
7566
7566 28-SEP-81
7698 03-DEC-81
28-SEP-81
03-DEC-81 3000
1250
1250 1400
3000 1400 20
30
20
30
7934 7499
MILLER
7499
7369 ALLEN
SMITH
7369 ALLEN
SMITH CLERK7698
CLERK
SALESMAN
CLERK
SALESMAN 7698
7902 20-FEB-81
17-DEC-80
7902 20-FEB-81
17-DEC-80 1600
800
1600
800 300
300 20
30
20
30
7844
7788
7788 TURNER
7844 SCOTT
SCOTT ANALYST
TURNER SALESMAN
SALESMAN 7698
ANALYST 7566
7566 08-SEP-81
7698 09-DEC-82
09-DEC-82 3000
08-SEP-81 1500
3000
1500 00 20
30
20
30
7900
7876
7876 JAMES
7900 ADAMS
ADAMS CLERK
JAMES CLERK
CLERK
CLERK 7698
7788
7788 03-DEC-81
7698 12-JAN-83
12-JAN-83 1100
03-DEC-81 950
1100
950 20
30
20
30
7521
7934
7934 WARD
7521 MILLER
MILLER CLERK
WARD SALESMAN
SALESMAN 7698
CLERK 7782
7782 22-FEB-81
7698 23-JAN-82
23-JAN-82 1300
22-FEB-81 1250
1300
1250 500
500 10
30
10
30
Why Use Views?
 To restrict database access
 To make complex queries easy
 To allow data independence
 To present different views of the same data
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


Creating a View
 You embed a subquery within the CREATE VIEW statement.

 The subquery can contain complex SELECT syntax.

CREATE
CREATE [OR
[OR REPLACE]
REPLACE] [FORCE|NOFORCE]
[FORCE|NOFORCE] VIEW
VIEW view
view
[(alias[,
[(alias[, alias]...)]
alias]...)]
AS
AS subquery
subquery
[WITH
[WITH CHECK
CHECK OPTION
OPTION [CONSTRAINT
[CONSTRAINT constraint]]
constraint]]
[WITH
[WITH READ
READ ONLY]
ONLY]
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
Describe the
the structure
structure of
of the
the view
view by
by
using
using the
the SQL*Plus
SQL*Plus DESCRIBE
DESCRIBE
command.
command.
SQL>
SQL> DESCRIBE
DESCRIBE empvu10
empvu10
Creating a View
 Create a view by using column aliases in the subquery.

 Select the columns from this view by the given alias


names.

SQL> CREATE VIEW salvu30


2 AS SELECT empno EMPLOYEE_NUMBER, ename NAME,
3 sal SALARY
4 FROM emp
5 WHERE deptno = 30;
View created.
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.
Modifying a View
 Modify the EMPVU10 view by using CREATE OR REPLACE
VIEW clause. Add an alias for each column name.

 Column aliases in the CREATE VIEW clause are listed in


the same order as the columns in the subquery.

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.
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.
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
Rules for Performing DML Operations
on a View
 You cannot modify data in a view if it contains:
 Any of the conditions mentioned in the previous slide
 Columns defined by expressions
 The ROWNUM pseudocolumn
 You cannot add data if:
 The view contains any of the conditions mentioned above or in the
previous slide
 There are 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 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 CONSTRAINT empvu20_ck;
View created.

•• Any
Any attempt
attempt to
to change
change the
the department
department number
number for
for any
any row
row
in
in the
the view
view will
will fail
fail because
because itit violates
violates the
the WITH
WITH CHECK
CHECK
OPTION
OPTION constraint.
constraint.
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
Any attempt
attempt to
to perform
perform aa DML
DML on
on any
any row
row in
in
the
the view
view will
will result
result in
in Oracle
Oracle Server
Server error.
error.
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.
Summary
 A view is derived from data in other tables or other views.
 A view provides the following advantages:
 Restricts database access
 Simplifies queries
 Provides data independence
 Allows multiple views of the same data
 Can be dropped without removing the underlying data
Books

1. Modern Database Management (Sixth Edition) by Fred R. McFadden, Jeffrey A.


Hoffer, Mary B. Prescott
2. Database System Concepts (Fifth Edition) by Henry F. Korth, S. Sudarshan, A.
Silberschatz
3. Oracle-database-10g-sql-fundamentals-1-student-guide-volume-1
4. SQL and Relational Theory: How to Write Accurate SQL Code by C.J. Date
5. Database Systems: A Practical Approach to Design, Implementation and
Management (4th Edition) by Thomas M. Connolly, Carolyn E. Begg
6. Fundamentals of Database Systems, 5th Edition by RamezElmasri, Shamkant B.
Navathe
7. Database Design and Relational Theory: Normal Forms and All That Jazz by C. J. Date
8. An Introduction to Database Systems 8th Edition, by C.J. Date
References

1. https://www.db-book.com/db6/slide-dir/index.html
2. https://docs.oracle.com/en/database/oracle/oracle-database/20/sqlrf/SQL-
Standards.html#GUID-BCCCFF75-D2A4-43AD-8CAF-C3C97D92AC63
3. https://www.slideshare.net/HaaMeemMohiyuddin1/data-knowledge-and-
information
4. https://www.slideshare.net/tabinhasan/from-data-to-wisdom
5. https://www.slideshare.net/thinnaphat.bo/

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