V := ViewQuery(R1, R2, ..., Rn); Evaluate Q;
CREATE VIEW ViewName AS Queryor to rename attributes:
CREATE VIEW ViewName(A1, A2, ..., Am) AS Query
Student(ID, name, address, GPA, sizeHS) Campus(location, enrollment, rank) Apply(ID, location, date, major, decision)
CREATE VIEW RecentSC AS SELECT ID, date, major FROM Apply WHERE location = 'SC' AND date > 2/15/05 AND decision = 'U'Can use in query:
SELECT RecentSC.ID, name, major FROM RecentSC, Student WHERE RecentSC.ID = Student.ID AND GPA > 3.7rewritten to:
SELECT RecentSC.ID, name, major FROM (SELECT ID, date, major FROM Apply WHERE location = 'SC' AND date > 2/15/05 AND decision = 'U') RecentSC, Student WHERE RecentSC.ID = Student.ID AND GPA > 3.7or in "flattened" form:
SELECT Apply.ID, name, major FROM Apply, Student WHERE Apply.ID = Student.ID AND location = 'SC' AND date > 2/15/05 AND decision = 'U' AND GPA > 3.7Or could make query into another view:
CREATE VIEW AdmitSC AS SELECT RecentSC.ID, name, major FROM RecentSC, Student WHERE RecentSC.ID = Student.ID AND GPA > 3.7
CREATE VIEW MultiApply AS SELECT DISTINCT Student.* FROM Student, Apply A1, Apply A2 WHERE Student.ID = A1.ID AND Student.ID = A2.ID AND A1.location <> A2.location
CREATE VIEW AllDB AS SELECT Student.ID, name, address, GPA, sizeHS, Campus.location, enrollment, rank, date, major, decision FROM Student, Campus, Apply WHERE Student.ID = Apply.ID AND Apply.location = Campus.locationNo more joins in queries!
Now can write:
SELECT * FROM AllDB WHERE GPA < 3.5 AND rank > 3 AND major = 'psychology'rewritten to:
SELECT Student.ID, name, address, GPA, sizeHS, Campus.location, enrollment, rank, date, major, decision FROM Student, Campus, Apply WHERE Student.ID = Apply.ID AND Apply.location = Campus.location AND GPA < 3.5 AND rank > 3 AND major = 'psychology'
DELETE FROM RecentSC WHERE date <= 3/15/05translated to:
DELETE FROM Apply WHERE date <= 3/15/05 AND location = 'SC' AND date > 2/15/05 AND decision = 'U'
CREATE VIEW CS_EE AS SELECT ID, location, major FROM Apply WHERE major = 'CS' OR major = 'EE'Insertion into this view:
INSERT INTO CS_EE VALUES (123, 'Berkeley', 'CS')translated to:
INSERT INTO Apply VALUES (123, 'Berkeley', NULL, 'CS', NULL)Default values are used instead of NULL when defined.
INSERT INTO Apply VALUES (123, 'Berkeley', NULL, 'CS', 'U')Another insertion into this view:
INSERT INTO CS_EE VALUES (123, 'Berkeley', 'math')translated to:
INSERT INTO Apply VALUES (123, 'Berkeley', NULL, 'math', NULL)Resulting tuple is not in the view!
RecentSC
view:
INSERT INTO RecentSC VALUES (123, 4/15/05, 'economics')translated to:
INSERT INTO Apply VALUES (123, NULL, 4/15/05, 'economics', NULL)
Apply
tuple doesn't appear in view!
INSERT INTO AdmitSC VALUES (123, 'Mary', 'CS')
Question: Translated to?
Example:
CREATE VIEW AvgGPA(a) AS SELECT avg(GPA) FROM Student; UPDATE AvgGPA SET a = 3.0;
Question: Translated to?
Example: Students who applied to more than one campus
CREATE MATERIALIZED VIEW MultiApply AS SELECT DISTINCT Student.* FROM Student, Apply A1, Apply A2 WHERE Student.ID = A1.ID AND Student.ID = A2.ID AND A1.location <> A2.locationQuestion: What modifications to regular relations might invalidate current contents of MultiApply?
Even though materialized views are stored as tables, updates to materialized views incur same ambiguity problems as updates to virtual views.
Question: Why not just update materialized view table?
Question: How does one decide whether to use a virtual or a materialized view?