❖ Views |
A view is like a "virtual relation" defined via a query.
View definition and removal:
ViewName CREATE VIEWQuery ASViewName (AttributeNames) CREATE VIEWQuery ASViewName DROP VIEW
Query may be any SQL query, involving: stored tables, other views
CREATE OR REPLACE
❖ Views (cont) |
The stored tables used by a view are referred to as base tables.
Views are defined only after their base tables are defined.
A view is valid only as long as its underlying query is valid.
Dropping a view has no effect on the base tables.
Views are a convenient abstraction mechanism
❖ Views (cont) |
Example: defining/naming a complex query using a view:
CREATE VIEW
CourseMarksAndAverages(course,term,student,mark,avg)
AS
SELECT s.code, termName(t.id), e.student, e.mark,
avg(mark) OVER (PARTITION BY course)
FROM CourseEnrolments e
JOIN Courses c on c.id = e.course
JOIN Subjects s on s.id = c.subject
JOIN Terms t on t.id = c.term
;
which would make the following query easy to solve
SELECT course, term, student, mark FROM CourseMarksAndAverages WHERE mark < avg;
❖ Views (cont) |
Example: An avid Carlton drinker might not be interested in other kinds of beer.
CREATE VIEW MyBeers AS SELECT * FROM Beers WHERE brewer = 'Carlton';
which is used as
SELECT * FROM MyBeers;
name | brewer | style
------------------+---------+-------
Crown Lager | Carlton | Lager
Fosters Lager | Carlton | Lager
Invalid Stout | Carlton | Stout
Melbourne Bitter | Carlton | Lager
Victoria Bitter | Carlton | Lager
❖ Views (cont) |
A view might not use all attributes of the base relations.
Example: We don't really need the address of inner-city hotels.
CREATE VIEW InnerCityHotels AS
SELECT name, license
FROM Bars
WHERE addr in ('The Rocks','Sydney');
SELECT * FROM InnerCityHotels;
name | license
-----------------+---------
Australia Hotel | 123456
Lord Nelson | 123888
Marble Bar | 122123
❖ Views (cont) |
A view might use computed attribute values.
Example: Number of beers produced by each brewer.
CREATE VIEW BeersBrewed AS SELECT brewer, count(*) as nbeers FROM beers GROUP BY brewer; SELECT * FROM BeersBrewed; brewer | nbeers ----------+-------- 3 Ravens | 1 Akasha | 1 Alesmith | 1 ...
❖ Renaming View Attributes |
This can be achieved in two different ways:
CREATE VIEW InnerCityHotels AS
SELECT name AS bar, license AS lic
FROM Bars
WHERE addr IN ('The Rocks', 'Sydney');
CREATE VIEW InnerCityHotels(bar, lic) AS
SELECT name, license
FROM Bars
WHERE addr IN ('The Rocks', 'Sydney');
Both of the above produce the same view.
❖ Using Views |
Views can be used in queries as if they were stored relations.
However, they differ from stored relations in two important respects:
insert into MyBeers values ('Zero','Carlton','No-alcohol');
would update the Beers
❖ Using Views (cont) |
Example: of view changing when base table changes.
SELECT * FROM InnerCityHotels;
name | license
-----------------+---------
Australia Hotel | 123456
Lord Nelson | 123888
Marble Bar | 122123
-- then the Lord Nelson goes broke
DELETE FROM Bars WHERE name = 'Lord Nelson';
-- no explict update has been made to InnerCityHotels
SELECT * FROM InnerCityHotels;
name | license
-----------------+---------
Australia Hotel | 123456
Marble Bar | 122123
❖ Updating Views |
Explicit updates are allowed on views satisfying the following:
RWHERERWHERESELECTSELECTNULL❖ Updating Views (cont) |
Example: Our InnerCityHotel
INSERT INTO InnerCityHotels
VALUES ('Jackson''s on George', '9876543');
creates a new tuple in the Bars
(Jackson's on George, NULL, 9876543)
but this new tuple does not satisfy the view condition:
addr IN ('The Rocks', 'Sydney')
so it does not appear if we select from the view.
❖ Evaluating Views |
Two alternative ways of implementing views:
❖ Evaluating Views (cont) |
Example: Using the InnerCityHotels
CREATE VIEW InnerCityHotels AS
SELECT name, license
FROM Bars
WHERE addr IN ('The Rocks', 'Sydney');
SELECT name
FROM InnerCityHotels
WHERE license = '123456';
--is rewritten into the following form before execution
SELECT name
FROM Bars
WHERE addr IN ('The Rocks', 'Sydney')
AND license = '123456';
❖ Materialized Views |
Materialized views are implemented as stored tables
On each update to base tables, need to also update the view table.
Clearly this costs space and makes updates more expensive.
However, in a situation where
Materialized views are used extensively in data warehouses.
Produced: 29 Sep 2020