Presentation is loading. Please wait.

Presentation is loading. Please wait.

McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. What is a View? Derived table Behaves like a base table (virtual) Stored.

Similar presentations


Presentation on theme: "McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. What is a View? Derived table Behaves like a base table (virtual) Stored."— Presentation transcript:

1 McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. What is a View? Derived table Behaves like a base table (virtual) Stored query

2 McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. View Advantages Reduce impact of database definition changes Simplify database usage Unit of database security Can be a performance penalty on complex views

3 McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Three Schema Architecture

4 McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. View Definition Example Example 1: Create a view consisting of offerings taught by faculty in the MS department. CREATE VIEW MS_View AS SELECT OfferNo, Course.CourseNo, CrsUnits, OffTerm, OffYear, Offering.FacSSN, FacFirstName, FacLastName, OffTime, OffDays FROM Faculty, Course, Offering WHERE FacDept = 'MS' AND Faculty.FacSSN = Offering.FacSSN AND Offering.CourseNo = Course.CourseNo

5 McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Column Renaming Example 2: create a view containing offering data and the number of enrolled students. CREATE VIEW Enrollment_View ( OfferNo, CourseNo, Term, Year, Instructor, NumStudents ) AS SELECT Offering.OfferNo, CourseNo, OffTerm, OffYear, FacLastName, COUNT(*) FROM Offering, Faculty, Enrollment WHERE Offering.FacSSN = Faculty.FacSSN AND Offering.OfferNo = Enrollment.OfferNo GROUP BY Offering.OfferNo, CourseNo, OffTerm, OffYear, FacFirstName, FacLastName

6 McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Using Views Example 3 SELECT OfferNo, CourseNo, FacFirstName, FacLastName, OffTime, OffDays FROM MS_View WHERE OffTerm = 'SPRING' AND OffYear = 2003 Example 4 SELECT OfferNo, Instructor, NumStudents, CrsUnits FROM Enrollment_View, Course WHERE Enrollment_View.CourseNo = Course.CourseNo AND NumStudents < 5

7 McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Processing View Queries Materialization –Execute two queries –Large overhead –Preferred in static environments Modification –Substitute view definition for view references –Execute one query –Incurs little overhead –Not possible for all view queries

8 McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Modification Example Example 5: Query using a view SELECT OfferNo, CourseNo, FacLastName FROM MS_View WHERE OffYear = 2003 Example 6: Modified query SELECT OfferNo, Course.CourseNo, FacLastName FROM Faculty, Course, Offering WHERE FacDept = 'MS' AND OffYear = 2003 AND Faculty.FacSSN = Offering.FacSSN AND Offering.CourseNo = Course.CourseNo

9 McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Single Table Updatable Views Support modification statements Rules for single table updatable views –1-1 correspondence between view rows and base table rows –View includes PK of base table –View includes all required columns –View definition does not have GROUP BY or DISTINCT

10 McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Updatable View Examples Example 7: Single table updatable view CREATE VIEW Fac_View1 AS SELECT FacSSN, FacFirstName, FacLastName, FacRank, FacSalary, FacDept, FacCity, FacState, FacZipCode FROM Faculty WHERE FacDept = 'MS' Example 8: View update UPDATE Fac_View1 SET FacSalary = FacSalary * 1.1 WHERE FacRank = 'ASST'

11 McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. View Update with Side Effects Modify column used in the WHERE condition of a view definition Use WITH CHECK OPTION clause to prevent Example 9: Change the department of rows in the Fac_View1 UPDATE Fac_View1 SET FacDept = 'FIN' WHERE FacSalary > 100000

12 McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. Multiple Table Updatable Views No industry standard Only recently supported More complex rules than single table updatable views Access supports flexible view updates for multi-table views Oracle rules in Appendix 10.B


Download ppt "McGraw-Hill/Irwin © 2004 The McGraw-Hill Companies, Inc. All rights reserved. What is a View? Derived table Behaves like a base table (virtual) Stored."

Similar presentations


Ads by Google