You are on page 1of 27

Database Design (IS414)

Views

By: Neamat El Tazi


Readings
Fundamentals of Database Systems. R. Elmasri and S. B. Navathe,
6th Edition, 2004, Addison-Wesley, ISBN 0-321-12226-7 (Chapter
5)

2 IS414 - Database Design Neamat El Tazi


Outline
 Why Views?
 Defining Views
 Creating Views
 Modifying/Updating Views
 Virtual Vs. Materialized Views

3 IS414 - Database Design Neamat El Tazi


Why Views?
 Allow different users to view the database in different
ways.

4 IS414 - Database Design Neamat El Tazi


Advantages of Views

To make complex
To restrict queries easy
data access

To present
different views of
the same data

5 IS414 - Database Design Neamat El Tazi


View Terminology
 Base Table
 A table containing the raw data
 Dynamic/Virtual View
 A “virtual table” created dynamically upon request by a user.
 No data actually stored; instead data from base table made
available to user.
 Based on SQL SELECT statement on base tables or other
views
 Materialized View
 Copy or replication of data
 Data actually stored
 Must be refreshed periodically to match the corresponding
base tables
6 IS414 - Database Design Neamat El Tazi
Source: http://www.essentialsql.com/what-is-a-relational-database-view/
7 IS414 - Database Design Neamat El Tazi
Defining Views
 In SQL, a VIEW is a virtual relation based on the result-set of a
SELECT statement.

 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.

 Syntax:

CREATE VIEW view_name AS


SELECT column_name(s)
FROM table_name
WHERE condition
8 IS414 - Database Design Neamat El Tazi
Simple View Vs. Complex Views

9 IS414 - Database Design Neamat El Tazi


SQL- Create View
 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’;

10 IS414 - Database Design Neamat El Tazi


Querying a View
 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.
SELECT title View
FROM ParamountMovie
WHERE year = ‘1979’;

 Have same result as


SELECT title Table
FROM Movie
WHERE studioName = ‘Paramount’ AND year = ‘1979’;

11 IS414 - Database Design Neamat El Tazi


SQL – Querying a View (Cont.)
⚫ Query involving both view and table

SELECT DISTINCT starName View


FROM ParamountMovie, StarsIn Table

WHERE title = movieTitle AND year =


movieYear;

12 IS414 - Database Design Neamat El Tazi


Renaming Attributes in Views
⚫ 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#;

13 IS414 - Database Design Neamat El Tazi


SQL-Modifying a View
 Updating data from views rather than base tables is
possible under limitations. The view may not be
updatable in the following cases:

1. DISTINCT in SELECT
2. DERIVED COLUMNS nor EXPRESSIONS;
3. The FROM/UNION references more than one table;
4. The FROM references another VIEW that is not
updateable;
5. The CREATE VIEW contains GROUP BY or
HAVING.

14 IS414 - Database Design Neamat El Tazi


SQL – Modifying a View (Insert)
INSERT INTO ParamountMovie
VALUES (‘Star Trek’, 1979);
To make the view ParamountMovie updateable, we need to add attribute
studioName to it’s 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 length inColor studioName producerC#


‘Star Trek’ 1979 0 NULL ‘Paramount’ NULL

15 IS414 - Database Design Neamat El Tazi


SQL – 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’;

16 IS414 - Database Design Neamat El Tazi


SQL – Modifying a 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’;

17 IS414 - Database Design Neamat El Tazi


SQL – Drop 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

18 IS414 - Database Design Neamat El Tazi


Read Only Views
 You can prohibit people from performing any
updates/inserts/deletes over a view.

19 IS414 - Database Design Neamat El Tazi


Check Option
CREATE VIEW HiEmps
AS SELECT * FROM Emps
WHERE sal > 1500
WITH CHECK OPTION

 INSERT INTO HiEmps( empno, ename, sal ) VALUES ( 3333, 'Sam


Sham', 1200 )
 This inserts a tuple into Emps which actually will NOT appear in the
view.
 SQL ordinarily allows this.
 WITH CHECK OPTION prevents inserts & updates from inserting
or updating tuples so that they don't appear in the view
 In order to insert or update a value it has to satisfy the conditions
inside the view itself

20 IS414 - Database Design Neamat El Tazi


Cost of using Views

 Typically a view involves joins across multiple tables.


 Every time a view is queried, a join must be done.
 This can be expensive if the view is used frequently

 Suppose
 The view is used frequently
 The underlying tables on which the view is based do not
change very often

21 IS414 - Database Design Neamat El Tazi


Example

CREATE VIEW EmpDeptsView AS


SELECT empno, ename, job, deptno, dname
FROM Emps NATURAL JOIN Depts

 Suppose
 Emps and Depts are changed infrequently
 Everytime EmpDeptsView is accessed, Emps and Depts have to
be joined

22 IS414 - Database Design Neamat El Tazi


Materialized Views Definition

 A materialized view is an actual persistent table which


contains the data to be viewed from underlying tables.

 Every time the underlying tables are changed, the


materialized view must also be changed to keep it
consistent (sometimes this is done on a regular schedule
instead of on every change)

 Materialized Views are frequently read only. Sometimes


they support updates propagated back to their base
tables.

23 IS414 - Database Design Neamat El Tazi


Materialized View Creation

 CREATE MATERIALIZED VIEW EmpDeptsView AS


SELECT empno, ename, job, deptno, dname FROM Emps
NATURAL JOIN Depts

 EmpDeptsView is now a table that will be built


immediately, and updated whenever a change is made in
the underlying base tables (Oracle only)

 Developers can use triggers to define materialized views


in systems which do not support them directly
24 IS414 - Database Design Neamat El Tazi
Reusing a Materialized View
• Suppose I have only the result of SeattleView:
SELECT buyer, seller, product, store
FROM Person, Purchase
WHERE Person.city = ‘Seattle’ AND
Person.per-name = Purchase.buyer
• and I want to answer the query
SELECT buyer, seller
FROM Person, Purchase
WHERE Person.city = ‘Seattle’ AND
Person.per-name = Purchase.buyer AND
Purchase.product=‘ipad’.
Then, I can rewrite the query using the view.

25 IS414 - Database Design Neamat El Tazi


Query Rewriting using Views
Rewritten query:
SELECT buyer, seller
FROM SeattleView
WHERE product= ‘ipad’

Original query:
SELECT buyer, seller
FROM Person, Purchase
WHERE Person.city = ‘Seattle’
AND Person.per-name = Purchase.buyer
AND Purchase.product=‘ipad’.

26 IS414 - Database Design Neamat El Tazi


Virtual Vs. Materialized Views
 Virtual views:
 Used in databases
 Computed only on-demand – slower at runtime
 Always up to date

 Materialized views
 Used in data warehouses
 Precomputed offline – faster at runtime
 May have stale data

27 IS414 - Database Design Neamat El Tazi

You might also like