0% found this document useful (0 votes)
22 views

Week8 - Views

Uploaded by

cheriejalapeno23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Week8 - Views

Uploaded by

cheriejalapeno23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

BS(SWE) Part-II 2nd Semester Database Systems Week 8 - Views

Objective
The objective of this lab is to get familiar with views in databases. After completing this
lab students will be able to create, view and update the views.

Introduction
View
● A view is a logical table based on a table or another view.
● A view contains no data of its own but is like a window through which data from
tables can be viewed or changed.
● The tables on which a view is based are called base tables.
● The view is stored as a SELECT statement in the data dictionary.

Advantages of Views
● Views restrict access to the data because the view can display selective columns
from the table.
● Views can be used to make simple queries to retrieve the results of complicated
queries. Views provide data independence for ad hoc users and application
programs.
● One view can be used to retrieve data from several tables.
● Views provide groups of users access to data according to their particular
criteria.

Creating a View
You can create a view by embedding a subquery within the CREATE VIEW statement.

Syntax

CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view


[(alias[, alias]...)]
AS subquery
[WITH CHECK OPTION [CONSTRAINT constraint]]
[WITH READ ONLY [CONSTRAINT constraint]];

Keyword Description

OR REPLACE re-creates the view if it already exists

FORCE creates the view regardless of whether or


not the base tables exist

Department of Software Engineering, Faculty of Engineering & Technology, UoS.


BS(SWE) Part-II 2nd Semester Database Systems Week 8 - Views

NOFORCE creates the view only if the base tables


exist (This is the default.)

View is the name of the view

Alias specifies names for the expressions


selected by the view’s query

subquery is a complete SELECT statement

WITH CHECK OPTION specifies that only rows accessible to the


view can be modified

constraint is the name assigned to the CHECK


OPTION constraint

WITH READ ONLY ensures that no DML operations can be


performed on this view

The example creates a view that contains the employee number, last name, and salary
for each employee in department 80.

CREATE VIEW empvu80


AS SELECT employee_id, last_name, salary
FROM employees
WHERE department_id = 80;

You can display the structure of the view by using the iSQL*Plus DESCRIBE command.

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.
● If you do not specify a constraint name for a view created with the WITH CHECK
OPTION, the system assigns 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 or regranting object privileges previously
granted on it.

Department of Software Engineering, Faculty of Engineering & Technology, UoS.


BS(SWE) Part-II 2nd Semester Database Systems Week 8 - Views

● You can control the column names by including column aliases within the
subquery.

Department of Software Engineering, Faculty of Engineering & Technology, UoS.


BS(SWE) Part-II 2nd Semester Database Systems Week 8 - Views

Simple View vs Complex View

Feature Simple Views Complex Views

Number of tables One One or more

Contain functions No Yes

Contain groups of data No Yes

DML operations through a Yes Not always


view

Using Column Aliases


The following command creates a view containing the employee number
(EMPLOYEE_ID) with the alias ID_NUMBER, name (LAST_NAME) with the alias
NAME, and annual salary (SALARY) with the alias ANN_SALARY for every employee
in department 50.

CREATE VIEW salvu50 (ID_NUMBER, NAME, ANN_SALARY)


AS SELECT employee_id, last_name, salary*12
FROM employees
WHERE department_id = 50;

Retrieving Data from a View


You can retrieve data from a view as you would from any table.
You can display either the contents of the entire view or just specific rows and columns.

SELECT * FROM salvu50

Department of Software Engineering, Faculty of Engineering & Technology, UoS.


BS(SWE) Part-II 2nd Semester Database Systems Week 8 - Views

Views in the Data Dictionary

Modifying a View
With the OR REPLACE option, a view can be created even if one exists with this name
already, thus replacing the old version of the view for its owner.

The following command modifies the EMPVU80 view by using CREATE OR REPLACE
VIEW clause.

CREATE OR REPLACE VIEW empvu80


(id_number, name, sal, department_id)
AS SELECT employee_id, first_name || ' ' || last_name,
salary, department_id
FROM employees
WHERE department_id = 80;

Note:
● Add an alias for each column name.

Department of Software Engineering, Faculty of Engineering & Technology, UoS.


BS(SWE) Part-II 2nd Semester Database Systems Week 8 - Views

● Column aliases in the CREATE VIEW clause are listed in the same order as the
columns in the subquery.

Creating a Complex View


The following command creates a complex view of department names, minimum
salaries, maximum salaries, and average salaries by department. Note that alternative
names have been specified for the view. This is a requirement if any column of the view
is derived from a function or an expression.

CREATE VIEW dept_sum_vu


(name, minsal, maxsal, avgsal)
AS SELECT d.department_name, MIN(e.salary),
MAX(e.salary),AVG(e.salary)
FROM employees e, departments d
WHERE e.department_id = d.department_id
GROUP BY d.department_name;

You can view the structure of the view by using the iSQL*Plus DESCRIBE command.
Display the contents of the view by issuing a SELECT statement.

SELECT *
FROM dept_sum_vu;

Performing DML Operations on a View


You can perform DML operations on data through a view if those operations follow
certain rules.
You can remove a row from a view unless it contains any of the following:
● Group functions
● A GROUP BY clause
● The DISTINCT keyword
You can modify data through a view unless it contains any of the conditions mentioned
above or columns defined by expressions—for example, SALARY * 12.
You can add data through a view unless it contains any of the items listed above or
there are NOT NULL columns without default values in the base table that are not
selected by the view. All required values must be present in the view.

Department of Software Engineering, Faculty of Engineering & Technology, UoS.


BS(SWE) Part-II 2nd Semester Database Systems Week 8 - Views

Using the WITH CHECK OPTION Clause


It is possible to perform referential integrity checks through views. You can also enforce
constraints at the database level. The view can be used to protect data integrity, but the
use is very limited.

CREATE OR REPLACE VIEW empvu20


AS SELECT *
FROM employees
WHERE department_id = 20
WITH CHECK OPTION CONSTRAINT empvu20_ck ;

The WITH CHECK OPTION clause specifies that INSERTs and UPDATEs performed
through the view cannot create rows which the view cannot select, and therefore it
allows integrity constraints and data validation checks to be enforced on data being
inserted or updated.
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 on your view by creating it with the WITH
READ ONLY option. The example modifies the EMPVU10 view to prevent any DML
operations on the view.

CREATE OR REPLACE VIEW empvu10


(employee_number, employee_name, job_title)
AS SELECT employee_id, last_name, job_id
FROM employees
WHERE department_id = 10
WITH READ ONLY;

Any attempts to remove a row from a view with a read-only constraint results in an error.

Department of Software Engineering, Faculty of Engineering & Technology, UoS.


BS(SWE) Part-II 2nd Semester Database Systems Week 8 - Views

Removing a View
You use the DROP VIEW statement to remove a view. The statement removes the view
definition from the database. Dropping views has no effect on the tables on which the
view was based. Views or other applications based on deleted views become invalid.
Only the creator or a user with the DROP ANY VIEW privilege can remove a view.

Syntax:

DROP VIEW view;

DROP VIEW empvu80;

Department of Software Engineering, Faculty of Engineering & Technology, UoS.


BS(SWE) Part-II 2nd Semester Database Systems Week 8 - Views

-- PRACTICE --

1. Create a view called EMPLOYEES_VU based on the employee numbers,


employee names, and department numbers from the EMPLOYEES table.
Change the heading for the employee name to EMPLOYEE.

-- Paste your solution code below --


Code:

2. Display the contents of the EMPLOYEES_VU view.

-- Paste your solution code below --


Code:

3. Select the view name and text from the USER_VIEWS data dictionary view.

-- Paste your solution code below --


Code:

Department of Software Engineering, Faculty of Engineering & Technology, UoS.


BS(SWE) Part-II 2nd Semester Database Systems Week 8 - Views

4. Using your EMPLOYEES_VU view, enter a query to display all employee names
and department numbers.

-- Paste your solution code below --


Code:

5. Create a view named DEPT50 that contains the employee numbers, employee
last names, and department numbers for all employees in department 50. Label
the view columns EMPNO, EMPLOYEE, and DEPTNO. Do not allow an
employee to be reassigned to another department through the view using WITH
CHECK OPTION.

-- Paste your solution code below --


Code:

6. Display the structure and contents of the DEPT50 view.

-- Paste your solution code below --


Code:

Department of Software Engineering, Faculty of Engineering & Technology, UoS.


BS(SWE) Part-II 2nd Semester Database Systems Week 8 - Views

7. Make an attempt to reassign Matos to department 80.

-- Paste your solution code below --


Code:

8. Write a command to drop view DEPT50.

-- Paste your solution code below --


Code:

Department of Software Engineering, Faculty of Engineering & Technology, UoS.

You might also like