Download sql_iii

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Database wikipedia , lookup

Clusterpoint wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Relational model wikipedia , lookup

Join (SQL) wikipedia , lookup

Database model wikipedia , lookup

Transcript
Application Development with Views
MGMT 360
Database Management
Outline
Background
Creating views and using views
Processing queries that reference views
Updatable views
Data requirements for hierarchical forms
Data requirements for reports
What is a View?
Creates a virtual table from a SELECT statement
Views do not exist as independent entities in the database
Derived table
ANSI terms: Base table; Viewed table
Behaves like a base table (virtual)
Stored query
VIEWS
Purpose of Views:
 It
can hide parts of the database from certain users
 It permits users to access data that is customized to their needs
 It can simplify complex operations on base relations
TITLES
•Title_id
•Title
•Type
•Pub_id
•Price
•Advance
•Ytd_sales
•Notes
•Pubdate
TITLENUM
•Title_id
•Pub_id
•Price
•Advance
•Ytd_sales
•Pubdate
TITLETEXT
•Title_id
•Title
•Type
•Notes
Splitting a Base Table into Two Views
TITLENUM
•Title_id
•Pub_id
•Price
•Advance
•Ytd_sales
•Pubdate
TITLETEXT
•Title_id
•Title
•Type
•Notes
CREATE VIEW titlesview
As
SELECT tn.title_id, pub_id, price,
advance, ytd_sales, pubdate, tit;le,
type, notes
FROM titlenum tn,
titletext tt
WHERE tn.title_id=tt.title_id
TITLES
•Title_id
•Title
•Type
•Pub_id
•Price
•Advance
•Ytd_sales
•Notes
•Pubdate
Uniting Two Base Tables in a Single View
View
CREATE VIEW view_name
[(column_name [, column_name] …)]
AS
SELECT_statement
DROP VIEW view_name
View Advantages
Reduce impact of database definition changes
Simplify database usage
Unit of database security
Can be a performance penalty on complex views
Three Schema Architecture
View 1
External to
Conceptual
Mappings
Conceptual
to Internal
Mappings
View 2
Conceptual
Schema
Internal
Schema
View n
External
Level
Conceptual
Level
Internal
Level
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
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
Using Views
Example 3
SELECT OfferNo, CourseNo, FacFirstName,
FacLastName, OffTime, OffDays
FROM MS_View
WHERE OffTerm = 'SPRING' AND OffYear = 2000
Example 4
SELECT OfferNo, Instructor, NumStudents,
CrsUnits
FROM Enrollment_View, Course
WHERE Enrollment_View.CourseNo =
Course.CourseNo
AND NumStudents < 5
Processing View Queries
Materialization
Execute two queries
Large overhead
Modification
Substitute view definition for view references
Execute one query
Incurs little overhead
Not possible for all view queries
Modification Example
Example 5: Query using a view
SELECT OfferNo, CourseNo, FacLastName
FROM MS_View
WHERE OffYear = 2000
Example 6: Modified query
SELECT OfferNo, Course.CourseNo, FacLastName
FROM Faculty, Course, Offering
WHERE FacDept = 'MS' AND OffYear = 2000
AND Faculty.FacSSN = Offering.FacSSN
AND Offering.CourseNo = Course.CourseNo
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
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'
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
Multiple Table Updatable Views
No industry standard
Only recently supported
Oracle rules in Appendix 5.B
More complex rules than single table updatable views
1-M Updatable Queries
Associated with 1-M relationships
Join column of the 1 table: primary key or unique
Determine updatable tables
M table updatable
Primary key
Foreign key: must include in the query result
Required columns of the M table
Include primary key and required columns to support insert operations
on the 1 table
Use join operator style
1-M Updatable Query Example
Example 10: Save as Course_Offering_View1
SELECT Course.CourseNo, CrsDesc, CrsUnits,
Offering.OfferNo, OffTerm, OffYear,
Offering.CourseNo, OffLocation,
OffTime, FacSSN, OffDays
FROM Course INNER JOIN Offering
ON Course.CourseNo = Offering.CourseNo
Usage of a 1-M Updatable Query
Example 11: Insert a row into the Course_Offering_View1.
INSERT INTO Course_Offering_View1
( OfferNo, Offering.CourseNo, OffTerm,
OffYear, OffLocation, OffTime,
FacSSN, OffDays )
VALUES
( 7799, 'IS480', 'Spring', 2000, 'BLM201',
#1:30PM#, '098-76-5432', 'MW' )
Extensions to Multiple Tables
Apply rules to each 1-M relationship
FK of each M table in the query result
Usually only the lowest level M table is updatable
Examples
Course-Offering, Faculty-Offering
Offering-Enrollment, Faculty-Offering
Hierarchical Forms
Formatted window for data entry and display
Main form
Subform
Provide attractive interface for a 1-M relationship
Specification of data requirements is important
Revised University Database
Example Hierarchical Form
Analysis of Data Requirements
Identify the 1-M relationship
Identify the linking fields
Determine other tables in the main form and the subform
Determine updatable tables
Write queries for the main form and subform
Registration Main Form Query
SELECT RegNo, RegTerm, RegYear,
RegDate, Registration.StdSSN,
Registration.StdSSN,
RegStatus, StdFirstName,
StdLastName, StdClass,
StdCity, StdState
FROM Registration INNER JOIN Student
ON Registration.StdSSN =
Student.StdSSN
Registration Subform Query
SELECT RegNo, Enrollment.OfferNo, OffTime,
Offering.CourseNo, OffLocation,
OffTerm, OffYear, Offering.FacSSN,
FacFirstName, FacLastName,
CrsDesc, CrsUnits
FROM ( ( Enrollment INNER JOIN Offering
ON Enrollment.OfferNo =
Offering.OfferNo )
INNER JOIN Course
ON Offering.CourseNo =
Course.CourseNo )
LEFT JOIN Faculty
ON Faculty.FacSSN =
Offering.FacSSN
Faculty Assignment Form
Data Requirements for the Faculty
Assignment Form
Step 1: Faculty (1 table), Offering (M table)
Step 2: Faculty.FacSSN, Offering.FacSSN
Step 3: only data from the Faculty and Offering tables
Step 4: update Offering.FacSSN
Faculty Assignment Queries
Main form
SELECT FacSSN, FacFirstName,
FacLastName, FacDept
FROM Faculty
Subform
SELECT OfferNo, Offering.CourseNo,
FacSSN, OffTime, OffDays,
OffLocation, CrsUnits
FROM Offering INNER JOIN COURSE
ON Offering.CourseNo =
Course.CourseNo
Hierarchical Reports
Stylized presentation of data appropriate to a selected
audience
Use nesting (or indentation) to provide a visually appealing
layout
Vocabulary
Group: sorted field; usually indented
Detail line: innermost field
Example Hierarchical Report
Summary Data in Detail Lines
Query Formulation Skills for Reports
Less difficult than skills for forms
Inspect report
Match
fields in the report to database columns
Determine the needed tables
Identify the join conditions
Determine the data level in the detail lines
Row
data versus summary data
Query should provide data for the detail lines
Faculty Work Load Query
SELECT Offering.OfferNo, FacFirstName, FacLastName,
FacDept, OffTerm, CrsUnits, OffLimit,
Count(Enrollment.RegNo) AS NumStds,
NumStds/Offlimit AS PercentFull,
(NumStds/Offlimit) < 0.25 AS LowEnrollment
FROM Faculty, Offering, Course, Enrollment
WHERE Faculty.FacSSN = Offering.FacSSN
AND Course.CourseNo = Offering.CourseNo
AND Offering.OfferNo = Enrollment.OfferNo
AND ( ( Offering.OffTerm = 'Fall'
AND Offering.OffYear = 1999 )
OR ( Offering.OffTerm = 'Winter'
AND Offering.OffYear = 2000 )
OR ( Offering.OffTerm = 'Spring'
AND Offering.OffYear = 2000 ) )
GROUP BY Offering.OfferNo, FacFirstName, FacLastName,
FacDept, OffTerm, CrsUnits, OffLimit
Summary
Significant benefits with a modest performance penalty
Foundation of application data requirements
Updatable views important for hierarchical forms
Carefully analyze data requirements before developing forms
and reports