Chapter 10 Views. Topics in this Chapter What are Views For? View Retrievals View Updates Snapshots SQL Facilities.

Slides:



Advertisements
Similar presentations
Views-basics 1. 2 Introduction a view is a perspective of the database different users may need to see the database differently; this is achieved through.
Advertisements

Data manipulation operations on views 1. 2 Outline retrieval operations in theory in practice (SQL92 and PostgreSQL) update operations in theory - basics.
1 Term 2, 2004, Lecture 6, Views and SecurityMarian Ursu, Department of Computing, Goldsmiths College Views and Security 3.
Database Languages Chapter 7. The Relational Algebra.
Unit 5 The Network Model  5.1 The Network Model  5.2 IDMS.
Database Administration, Integrity and Performance.
Chapter 3 An Introduction to Relational Databases.
Murali Mani SQL: Updates (DML) and Views (DDL). Murali Mani SQL DML (Updating the Data) Insert Delete Update.
Domains, Relations & Base RelVars (Ch
Chapter 6 Relations. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Topics in this Chapter Tuples Relation Types Relation Values Relation.
Introduction Chapter 1. Reference Book  Database Systems Thomas Connolly, Carolyn Begg, Anne Strachan Addison-Wesley 1999 ISBN:
Chapter 6 Relations. Topics in this Chapter Tuples Relation Types Relation Values Relation Variables SQL Facilities.
Database Systems Lecture # 8 11 th Feb,2011. The Relational Model of Data The term relation is basically just a mathematical term for a table. DBMS products.
Lecture 2 The Relational Model. Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical relations.
Data Manipulation 11 After this lecture, you should be able to:  Understand the differences between SQL (Structured Query Language) and other programming.
6.8 Case Study: E-R for Supplier-and-Parts Database
Chapter 9 Integrity. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-2 Topics in this Chapter Predicates and Propositions Internal vs.
Views: Limiting Access to Data A view is a named select statement that is stored in a database as an object. It allows you to view a subset of rows or.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
3-1 Unit 3 The Relational Model. 3-2 Wei-Pang Yang, Information Management, NDHU Outline  3.1 Introduction  3.2 Relational Data Structure  3.3 Relational.
Chapter 3 The Relational Model. 2 Chapter 3 - Objectives u Terminology of relational model. u How tables are used to represent data. u Connection between.
3-1 Unit 3 The Relational Model. 3-2 Wei-Pang Yang, Information Management, NDHU Outline  3.1 Introduction  3.2 Relational Data Structure  3.3 Relational.
Logical Database Design ( 補 ) Unit 7 Logical Database Design ( 補 )
Chapter 7 Relational Algebra. Topics in this Chapter Closure Revisited The Original Algebra: Syntax and Semantics What is the Algebra For? Further Points.
Further Normalization I
Data Definition After this lecture, you should be able to:
Unit III. Views A table that is derived from other tables Considered as a virtual table Does not store data.
Lecture No 14 Functional Dependencies & Normalization ( III ) Mar 04 th 2011 Database Systems.
Data Manipulation 21 After this lecture, you should be able to:  Use SQL SELECT statement effectively to retrieve the data from multiple related tables.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Unit 5 The Network Model  5.1 Data Modeling Issues  5.2 The Network Model  5.3 IDMS.
Algebra1 After this lecture, you should be able to:  Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Oracle 11g: SQL Chapter 4 Constraints.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Chapter 13 Views Oracle 10g: SQL. Oracle 10g: SQL2 Objectives Create a view, using CREATE VIEW command or the CREATE OR REPLACE VIEW command Employ the.
Chapter 4 An Introduction to SQL. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-2 Topics in this Chapter SQL: History and Overview The.
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E) 1.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Perform DFS from A A B C D E F G. Perform BFS from A A B C D E F G.
An Introduction to SQL For CS Overview of SQL  It is the standard language for relational systems, although imperfect  Supports data definition.
Chapter 10 Views. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.10-2 Topics in this Chapter What are Views For? View Retrievals View Updates.
Relations Prof. Yin-Fu Huang CSIE, NYUST Chapter 6.
Types Prof. Yin-Fu Huang CSIE, NYUST Chapter 5. Advanced Database SystemYin-Fu Huang Relation, tuple, cardinality, attribute, degree, domain, primary.
Unit 2 DB2 and SQL. 2-2 Wei-Pang Yang, Information Management, NDHU Outline of Unit Overview 2.2 Data Definition 2.3 Data Manipulation 2.4 The System.
Integrity Prof. Yin-Fu Huang CSIE, NYUST Chapter 9.
Views Prof. Yin-Fu Huang CSIE, NYUST Chapter 10. Advanced Database System Yin-Fu Huang 10.1Introduction Example: Var Good_Supplier View (S Where Status.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Data and Applications Security Developments and Directions Dr. Bhavani Thuraisingham The University of Texas at Dallas Lecture #2 Supporting Technologies:
Chapter 4 An Introduction to SQL. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-2 Topics in this Chapter SQL: History and Overview The.
Relations with Stored and Inherited Attributes Witold Litwin Dauphine University June 13, 2016.
Structured Query Language IV Asma Ahmad (C. J. Date) Database Systems.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Chapter 4 An Introduction to SQL.
STRUCTURE OF PRESENTATION :
Lecture 3 : Structured Query Language (SQL)
Chapter 10 Views.
Unit 2 DB2 and SQL.
STRUCTURE OF PRESENTATION :
STRUCTURE OF PRESENTATION :
STRUCTURE OF PRESENTATION :
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
پايگاه داده ها.
Φροντιστήριο SQL (από το βιβλίο του Date)
Chapter 2 Views.
Unit 7 Normalization (表格正規化).
STRUCTURE OF PRESENTATION :
Question 1: Basic Concepts (45 %)
STRUCTURE OF PRESENTATION :
Presentation transcript:

Chapter 10 Views

Topics in this Chapter What are Views For? View Retrievals View Updates Snapshots SQL Facilities

Views A view is created by applying an expression of relational algebra to a base relvar A view is a derived or virtual relvar Updates applied to the base relvar are immediately visible to the view Updates to values in the view “really” only update the base relvar by reiterating the original algebraic expression

What are Views For? A view can make subsequent queries much simpler (analogous to a macro) Views allow the same data to be seen by different users in different ways at the same time Views provide automatic security for hidden data Views can promote logical data independence

Logical Data Indendence Logical data independence means the immunity of users and user programs to changes in the logical structure of the database Growth aspect: New attributes or relvars can be added without affecting existing user programs Restructuring aspect: If a base relvar is split, the “old” structure can be emulated by a view; the “old” programs can now access the view

Base Relvars and Views The Principle of Interchangeability (of Base and Derived Relvars): –There are no arbitrary and unnecessary distinctions between base and derived relvars The Principle of Database Relativity: –From a user’s point of view, base relvars and views are logically equivalent

View Retrievals A relational expression is a relation- valued function It takes in relation(s) as arguments, and returns a relation A view is created using a relational expression, in which the relation returned is the view

View Updates A view is a relvar, and so is updateable by definition Earlier discussions of view updates tended to limit them It is in principle not necessary to limit them, apart from constraint enforcement View updates must adhere to the Golden Rule: no relvar must ever be allowed to violate its own predicate

Snapshots Snapshots are derived relvars, but they are not views Snapshots are the result of a relational operation that generates a read only (stored) relvar that is refreshed periodically An important case of controlled redundancy, used often for point in time reporting Snapshots have come to be misnamed “materialized views,” an oxymoron

SQL Facilities CREATE VIEW AS [WITH [ ] CHECK OPTION ] ; WITH CHECK OPTION is used for INSERTs and SELECTs: will make them fail if integrity constraints are violated; the default is that they will succeed regardless Can only be used with updateable views, as defined in SQL, which is narrower

Syntax: CREATE VIEW [ ( ) ] AS [ WITH [CASCADE | LOCAL] CHECK OPTION ] ; some limitations on the subqueries—no “conceptual” reason—just implementation difficulty or oversight except for “ORDER BY” makes no sense since tables have no order by default, the VIEW “inherits” column names— optionally it can be given new ones

| S# | SNAME | STATUS | CITY | | S1 | Smith | 20 | London | | S2 | Jones | 10 | Paris | | S3 | Blake | 30 | Paris | | S4 | Clark | 20 | London | | S5 | Adams | 30 | Athens | CREATE VIEW GOOD_SUPPLIERS AS SELECT S#, STATUS, CITY FROM S WHERE STATUS > 15; | S# | SNAME | STATUS | CITY | | S1 | | 20 | London | | | | | | | S3 | | 30 | Paris | | S4 | | 20 | London | | S5 | | 30 | Athens | Example

CREATE VIEW GOOD_SUPPLIERS AS SELECT S#, STATUS, CITY FROM S WHERE STATUS > 15; The VIEW GOOD_SUPPLIERS: | snum | status | city | | S1 | 20 | London | | S3 | 30 | Paris | | S4 | 20 | London | | S5 | 30 | Athens | The definition of the view is stored, the table is not. Example the definition

The table doesn’t really exist—just the “knowledge” of how it is “composed.” Thus, SELECT * FROM GOOD_SUPPLIERS WHERE CITY <> ‘London’; is effectively SELECT S#, STATUS, CITY FROM S WHERE CITY <> ‘London’ AND STATUS > 15; as though the SELECT from the query were merged with the SELECT from the definition.

The VIEW GOOD_SUPPLIERS: | snum | status | city | | S1 | 20 | London | | S3 | 30 | Paris | | S4 | 20 | London | | S5 | 30 | Athens | Updating Views DELETE FROM GOOD_SUPPLIERS WHERE S# = ‘S4’; deletes the “S4” row from S (and thus from the view) INSERT INTO GOOD_SUPPLIERS VALUES (‘S6’, 35, ‘Tokyo’); inserts row into S (with NULL SNAME) UPDATE GOOD_SUPPLIERS SET CITY = ‘Madrid’ WHERE S# = ‘S3’; changes S3’s CITY to Madrid in S (and thus in the view)

CREATE VIEW S#_CITY AS SELECT S#, CITY FROM S; | S# | CITY | | S1 | London | | S2 | Paris | | S3 | Paris | | S4 | London | | S5 | Athens | theoretically updatable CREATE VIEW STATUS_CITY AS SELECT STATUS, CITY FROM S; | STATUS | CITY | | 20 | London | | 10 | Paris | | 30 | Paris | | 20 | London | | 30 | Athens | theoretically not DML on Views

A View Updating Mechanism View updates must apply to appropriate underlying persistent relvars INSERT and DELETE should be inverses of each other UPDATE can be treated as a DELETE and an INSERT, so long as no intermediate actions or checks permitted View updates should be compatible with all relational operators

A View Updating Mechanism – UNION Let view UV be defined as follows: VAR UV VIEW ( S WHERE STATUS > 25 ) UNION ( S WHERE CITY = ‘Paris’ ) ; Insert the following tuple into the view: ( S6, Smith, 50, Rome) This will insert the tuple into the suppliers base relvar, and thus appear in the view Similar effect for DELETE and UPDATE

CREATE VIEW SHIPDETAIL AS SELECT S.S#, SNAME, P.P#, PNAME, COLOR, QTY FROM SP, S, P WHERE SP.S# = S.S# AND SP.P# = P.P#; SHIPDETAIL: | S# | SNAME | P# | PNAME | COLOR | QTY | | S1 | Smith | P1 | Nut | Red | 300 | | S1 | Smith | P2 | Bolt | Green | 200 | | S1 | Smith | P3 | Screw | Blue | 400 | | S1 | Smith | P4 | Screw | Red | 200 | | S1 | Smith | P5 | Cam | Blue | 100 | | S1 | Smith | P6 | Cog | Red | 100 | | S2 | Jones | P1 | Nut | Red | 300 | | S2 | Jones | P2 | Bolt | Green | 400 | | S3 | Blake | P2 | Bolt | Green | 200 | | S4 | Clark | P2 | Bolt | Green | 200 | | S4 | Clark | P4 | Screw | Red | 300 | | S4 | Clark | P5 | Cam | Blue | 400 | VAR SHIPDETAIL VIEW ( ( (SP JOIN S) JOIN P) WHERE SP.S# = S.S# AND SP.P# = P.P# ) { S#, SNAME, P#, PNAME, COLOR, QTY } Tutorial D View with three underlying base tables (relvars) to be updated. SQL

DROP VIEW “forgets” the definition DROP TABLE causes any views based on the base table to be dropped no “ALTER VIEW” just drop one, create a new one an ALTER TABLE (theoretically) has no effect on any views based on that table