You are on page 1of 32

ICT 321

VIEWS AND DATABASE


PROGRAMMING

Views
In some cases, it is not desirable for
all users to see the entire logical
model (that is, all the actual relations
stored in the database.)
A view contains rows and columns,
just like a real table. The fields in a
view are fields from one or more real
tables in the database. In some
cases, we can modify a view and
present the data as if the data were
coming from a single table.

Views
Consider a person who needs to
know an instructors name and
department, but not the salary. This
person should see a relation
described, in SQL, by
select ID, name,
dept_name
from instructor

Views
A view provides a mechanism to hide
certain data from the view of certain
users.
Any relation that is not of the
conceptual model but is made visible
to a user as a virtual relation is
called a view.
In DBMS, A view is a query stored in
the database
Think of it as a table definition for future
use

Views
Views are useful, primarily for READonly users and are not always safe for
CREATE, UPDATE, and DELETE.

Views advantages
Simplicity
Certain data items are subject to
retrieval on a frequent basis, then they
need a predefined query

Security
Provides a limited access to users and
read-only data from base tables

Maintainability
Just as an object-oriented class abstracts
underlying data and behavior, a view
abstracts the gory details of a query

View advantages
Development
Views can allow multiple developers to
point their development code to a single
database, yet make changes in specific
tables.

Virtual fields:
In a database design where storing
calculated fields is not permitted, or in
situations where new calculated fields
need to be added, views can provide a
virtual representation of those columns
that are calculated as the query is
executed

View Definition
Views are created through use of a
CREATE VIEW command that
incorporates use of the SELECT
statement.
Views are queried just like tables.
To create a view use the following
yntax:
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition

View Definition
View definition is not the same as
creating a new relation by evaluating
the query expression
Rather, a view definition causes the
saving of an expression; the expression is
substituted into queries using the view.

View example
Example: Create a view with title and
year and made by Paramount studio.
Movie (title, year, length, inColor,
studioName, producerC#)
CREATE VIEW ParamountMovie AS
SELECT title,year
FROM Movie
WHERE studioName = Paramount;

View example
A view could be used from inside a
query, a stored procedure, or from
inside another view. By adding
functions, joins, etc., to a view, it
allows us to present exactly the data
we want to the user.View
SELECT title
FROM ParamountMovie
WHERE year = 1979;
Table
Have same result
as

SELECT title
FROM Movie
WHERE studioName = Paramount AND

View example
Query involving both view and table
SELECT DISTINCT starName
View
Table
FROM ParamountMovie, StarsIn
WHERE title = movieTitle AND year =
movieYear;

View example
Movie (title, year, length, inColor, studioName,
producerC#)
MovieExec (name, address, cert#, netWorth)
CREATE VIEW MovieProd AS
SELECT title, name
FROM Movie, MovieExec
WHERE producerC# = cert#;
SELECT name
FROM MovieProd
WHERE title = Gone With the Wind;

Same result as query from tables


SELECT name
FROM Movie, MovieExec
WHERE producerC# = cert# AND title = The War Of the

View example
Defining a view from another view
create view physics_fall_2009 as
select course.course_id, sec_id, building,
room_number
from course, section
where course.course_id = section.course_id
and course.dept_name = Physics
and section.semester = Fall
and section.year = 2009;
create view physics_fall_2009_watson as
select course_id, room_number
from physics_fall_2009
where building= Watson;

Views Defined Using Other Views


One view may be used in the expression
defining another view
A view relation v1 is said to depend directly
on a view relation v2 if v2 is used in the
expression defining v1
A view relation v1 is said to depend on
view relation v2 if either v1 depends
directly to v2 or there is a path of
dependencies from v1 to v2
A view relation v is said to be recursive if
it depends on itself.

Renaming Attributes in View


Sometime, we might want to
distinguish attributes by giving the
different name.
CREATE VIEW MovieProd (movieTitle,
prodName) AS
SELECT title, name
FROM Movie, MovieExec
WHERE producerC# = cert#;

Modifying View
When we modify a view, we actually
modify a table through a view. Many
views are not updateable. Here are
rules have been defined in SQL for
updateable views:
selecting (SELECT not SELECT
DISTINCT) some attributes from one
relation R (which may itself be an
updateable view)
The WHERE clause must not involve R in a
subquery.
The list in the SELECT clause must include
enough attributes that will allow us to insert

Modifying View
If a view is based on a single underlying table
you can insert, update, delete rows in the
view
Some restrictions
cannot insert, if the underlying table has any NOT
NULL columns that do not appear in the view
cannot insert, update, delete if the view contains
GROUP BY, DISTINCT

You can insert into a multitable view, if the


underlying tables are key-preserved
Key-preserved table: a view contains enough
columns from a table to
that table

identify the primary key for

Modifying View (INSERT)


INSERT INTO ParamountMovie
VALUES (Star Trek, 1979);
To make the view ParamountMovie updateable, we need
to add attribute studioName to its SELECT clause
because it makes more sense if the studioName is
Paramount instead of NULL.
CREATE VIEW ParamountMovie AS
SELECT studioName, title, year
FROM Movie
WHERE studioName = Paramount;

Then
INSERT INTO ParamountMovie
VALUES (Paramount, Star Trek, 1979);
Title
year
producerC#

length

inColor

studioName

Modifying View (DELETE)


Suppose we wish to delete all
movies with Trek in their title from
the updateable view
ParamountMovie.
DELETE FROM ParamountMovie
WHERE title LIKE %Trek%;

It is turned into the base table delete


DELETE FROM Movie
WHERE title LIKE %Trek% AND studioName =
Paramount;

Modifying View (UPDATE)


UPDATE from an updateable view
UPDATE ParamountMovie
SET year = 1979
WHERE title = Star Trek the Movie;

It is turned into the base table update


UPDATE Movie
SET year = 1979
WHERE title = Star Trek the Movie AND
studioName = Paramount;

Dropping a View
DROP view: All views can be dropped,
whether or not the view is updateable.
DROP VIEW ParamountMovie;

DROP VIEW does not affect any tuples


of the underlying relation (table)
Movie.
However, DROP TABLE will delete the
table and also make the view
ParamountMovie unusable.
DROP TABLE Movie

View Implementation
View are basically implented using the
following approaches
Query modification and
View materialization

Suppose you have the following view


CREATE VIEW MANAGER AS
SELECT FNAME, LNAME, DName, Dnumber,
SALARY
FROM EMPLOYEE, DEPARTMENT
WHERE SSN=MGRSSN AND DNO=DNUMBER;

Query modification
present the view query in terms of a
query on the underlying base tables
Modify the view query into a query
on the underlying base tables
disadvantage: inefficient for views
defined via complex queries (especially
if additional queries are to be applied to
the view within a short time period)

Query modification
Example:
SELECT * FROM Manager WHERE Salary >
100000

becomes
SELECT Fname, Lname, Dname, Dnumber,
Salary
FROM EMPLOYEE, DEPARTMENT
WHERE SSN=MgrSSN AND Salary > 100000

View materialization
Involves physically creating and
keeping a temporary table
assumption: other queries on the view
will follow
concerns: maintaining correspondence
between the base table and the view
when the base table is updated
strategy: incremental update
Not available in MySQL

Database Programming
1. Variables and local variable
2. Compound statement (BEGIN
END)
3. Flow Control Statements
4. MySQL built-in Functions

Variables
To declare a variable use the
following syntax
DECLARE variable_name
[,variable_name...] datatype [DEFAULT
value];
Example
DECLARE @limit money
DECLARE @min_range int, @hi_range int

Variables
Assign a value into a variable:
SET @min_range = 0, @hi_range = 100
SET @limit = $10

Assign a value into a variable in SQL


statement:
SELECT @price = price FROM titles
WHERE title_id = 'PC2091'

Compound statement

MySQL built-in Functions

Flow Control Statements

CASE Syntax
IF Syntax
ITERATE Syntax
LEAVE Syntax
LOOP Syntax
REPEAT Syntax
RETURN Syntax
WHILE Syntax

You might also like