We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11
SQL: Data Definition
VIEWS
CNG351 - Data Management and File Structures
Lecture - 9 Instructor: Dr. Yeliz Yesilada Views View Definition Dynamic result of one or more relational operations operating on base relations to produce another relation. • Virtual relation that does not necessarily actually exist in the database but is produced upon request, at time of request. • Contents of a view are defined as a query on one or more base relations. • With view resolution, any operations on view are automatically translated into operations on relations from which it is derived. • With view materialization, the view is stored as a temporary table, which is maintained as the underlying base tables are updated.
• If list of column names is specified, it must have same number of items as number of columns produced by subselect. • If omitted, each column takes name of corresponding column in subselect.
CNG 351 - lecture 9 3/11
Example - Create Horizontal View Create view so that manager at branch B003 can only see details for staff who work in his or her office.
CREATE VIEW Manager3Staff
AS SELECT * FROM Staff WHERE branchNo = ‘B003’;
CNG 351 - lecture 9 4/11
Example - Create Vertical View Create view of staff details at branch B003 excluding salaries.
CREATE VIEW Staff3
AS SELECT staffNo, fName, lName, position, sex FROM Staff WHERE branchNo = ‘B003’;
CNG 351 - lecture 9 5/11
SQL - DROP VIEW DROP VIEW ViewName [RESTRICT | CASCADE]
• Causes definition of view to be deleted from database.
• If CASCADE specified, DROP VIEW deletes all dependent objects, in other words, all objects that reference the view. • If RESTRICT is specified and there are other objects that depend for their existence on the continued existence of the view being dropped, the command is rejected. • For example: DROP VIEW Manager3Staff;
CNG 351 - lecture 9 6/11
Advantages of Views • Data independence (can present a consistent, unchanged picture of the structure of the database) • Currency (changes in any of the base tables in the defining query are immediately reflected in the view) • Improved security (specific views to specific users) • Reduced complexity (can simplify queries, repeated, complex queries) • Convenience (part of the database, don’t need to see the complete set of data) • Customization (different users can see tables differently) • Data integrity (WITH CHECK OPTION, ensure integrity of the view)
CNG 351 - lecture 9 7/11
Disadvantages of Views • Update restriction (in some cases a view cannot be updated) • Structure restriction (The structure of the view is defined at the time of its creation, for example SELECT * refers to the current columns, so if the table is updated with new columns, they wil not be in the view) • Performance (View resolution can have performance penalty)
CNG 351 - lecture 9 8/11
View Materialization • View resolution mechanism may be slow, particularly if view is accessed frequently. • View materialization stores view as temporary table when view is first queried. • Thereafter, queries based on materialized view can be faster than recomputing view each time. • Difficulty is maintaining the currency of view while base tables(s) are being updated!!!
CNG 351 - lecture 9 9/11
View Materialization • If insert row into PropertyForRent with rent 400 then view would be unchanged. • If insert row for property PG24 at branch B003 with staffNo = SG19 and rent = 550, then row would appear in materialized view. • If insert row for property PG54 at branch B003 with staffNo = SG37 and rent = 450, then no new row would need to be added to materialized view. • If delete property PG24, row should be deleted from materialized view. • If delete property PG54, then row for PG37 should not be deleted (because of existing property PG21).
CNG 351 - lecture 9 10/11
Summary • A view is a virtual table representing a subset of columns and/or rows from one or more base tables or views. • SQL access control is built around the concepts of authorization identifiers, ownership, and privileges.