1 Lecture 03 Views, Constraints Tuesday, January 23, 2007.

Slides:



Advertisements
Similar presentations
1 Lecture 5: SQL Schema & Views. 2 Data Definition in SQL So far we have see the Data Manipulation Language, DML Next: Data Definition Language (DDL)
Advertisements

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
1 Constraints, Triggers and Active Databases Chapter 9.
CSE 544 Constraints Lecture #3 Friday, January 13, 2011 Dan Suciu , Winter
Chapter 7 Notes on Foreign Keys Local and Global Constraints Triggers.
SQL Constraints and Triggers
1 Lecture 03: Advanced SQL. 2 Outline Unions, intersections, differences Subqueries, Aggregations, NULLs Modifying databases, Indexes, Views Reading:
Lecture 03: Views and Constraints Wednesday, October 13, 2010 Dan Suciu -- CSEP544 Fall
CSE 544 Data Models and Views Lecture #4 Wednesday, January 19, 2011 Dan Suciu , Winter
1 Lecture 04 Entity/Relationship Modelling. 2 Outline E/R model (Chapter 5) From E/R diagrams to relational schemas (Chapter 5) Constraints in SQL (Chapter.
1 Introduction to Database Systems CSE 444 Lecture 05: Views, Constraints April 9, 2008.
1 Data Definition in SQL So far we have see the Data Manipulation Language, DML Next: Data Definition Language (DDL) Data types: Defines the types. Data.
1 Lecture 06: SQL Friday, January 14, Outline Indexes Defining Views (6.7) Constraints (Chapter 7) We begin E/R diagrams (Chapter 2)
End of SQL: Triggers, Impedance Mismatch and Transactions February 6 th, 2004.
1 Lecture 05: SQL Wednesday, October 8, Outline Outer joins (6.3.8) Database Modifications (6.5) Defining Relation Schema in SQL (6.6) Indexes.
1 INTERSECT and EXCEPT: (may no be in MySQL) (SELECT R.A, R.B FROM R) INTERSECT (SELECT S.A, S.B FROM S) (SELECT R.A, R.B FROM R) INTERSECT (SELECT S.A,
1 Lecture 02: Basic SQL. 2 Outline Data in SQL Simple Queries in SQL Queries with more than one relation Reading: Chapter 3, “Simple Queries” from SQL.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #15 M.P. Johnson Stern School of Business, NYU Spring, 2005.
Database Systems More SQL Database Design -- More SQL1.
Correlated Queries SELECT title FROM Movie AS Old WHERE year < ANY (SELECT year FROM Movie WHERE title = Old.title); Movie (title, year, director, length)
SQL: Constraints and Triggers Chapter 6 Ullman and Widom Certain properties we’d like our database to hold Modification of the database may break these.
Exercises Product ( pname, price, category, maker) Purchase (buyer, seller, store, product) Company (cname, stock price, country) Person( per-name, phone.
1 Lecture 4: More SQL Monday, January 13th, 2003.
1 Lecture 7: End of Normal Forms Outerjoins, Schema Creation and Views Wednesday, January 28th, 2004.
SQL (almost end) April 26 th, Agenda HAVING clause Views Modifying views Reusing views.
Lecture #19 May 13 th, Agenda Trip Report End of constraints: triggers. Systems aspects of SQL – read chapter 8 in the book. Going under the lid!
1 Lecture 04: SQL Wednesday, January 11, Outline Two Examples Nulls (6.1.6) Outer joins (6.3.8) Database Modifications (6.5)
1 Lecture 6: Views Friday, January 17th, Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,
1 SQL Constraints and Programming. 2 Agenda Constraints in SQL Systems aspects of SQL.
1 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index.
Phase 2, Answering queries using views. February 2 nd, 2004.
1 SQL Constraints and Programming. 2 Agenda Constraints in SQL Systems aspects of SQL.
IST 210 Constraints and Triggers. IST Constraints and Triggers Constraint: relationship among data elements DBMS should enforce the constraints.
Fall 2001Database Systems1 Triggers Assertions –Assertions describe rules that should hold for a given database. –An assertion is checked anytime a table.
Advanced SQL: Triggers & Assertions
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
Advanced SQL Concepts - Checking of Constraints CIS 4301 Lecture Notes Lecture /6/2006.
Different Constraint Types Type Where Declared When activated Guaranteed to hold? Attribute with attribute on insertion not if CHECK or update subquery.
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 
Lecture 03: Normal Forms, Constraints, Views Wednesday, October 12, 2011 Dan Suciu -- CSEP544 Fall
Constraining Attribute Values Constrain invalid values –NOT NULL –gender CHAR(1) CHECK (gender IN (‘F’, ‘M’)) –MovieName CHAR(30) CHECK (MovieName IN (SELECT.
1 Introduction to Database Systems CSE 444 Lecture 04: SQL April 7, 2008.
1 Lecture 5: Outerjoins, Schema Creation and Views Wednesday, January 15th, 2003.
Aggregation SELECT Sum(price) FROM Product WHERE manufacturer=“Toyota” SQL supports several aggregation operations: SUM, MIN, MAX, AVG, COUNT Except COUNT,
1 Lecture 06 Data Modeling: E/R Diagrams Wednesday, January 18, 2006.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
1 Lecture 05: SQL Wednesday, October 8, Outline Database Modifications (6.5) Defining Relation Schema in SQL (6.6) Indexes Defining Views (6.7)
Murali Mani Constraints. Murali Mani Keys: Primary keys and unique CREATE TABLE Student ( sNum int, sName varchar (20), dept char (2), CONSTRAINT key.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1 Constraints and Triggers in SQL. 2 Constraints are conditions that must hold on all valid relation instances SQL2 provides a variety of techniques for.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Lecture 05: SQL Wednesday, January 12, 2005.
Constraints and Triggers
Foreign Keys Local and Global Constraints Triggers
Introduction to Database Systems CSE 444 Lecture 04: SQL
Cse 344 May 14th – Entities.
Cse 344 May 18th – Loss and views.
Lecture 06: SQL Systems Aspects
Advanced SQL: Views & Triggers
Lecture 05 Views, Constraints
Lecture 12: SQL Friday, October 20, 2000.
Lecture 4: SQL Thursday, January 11, 2001.
Lecture 06: SQL Monday, October 11, 2004.
CSE544 SQL Monday, April 5, 2004.
Lecture 05: SQL Wednesday, October 9, 2002.
Lecture 14: SQL Wednesday, October 31, 2001.
Presentation transcript:

1 Lecture 03 Views, Constraints Tuesday, January 23, 2007

2 Outline Integrity constraints: Chapter 5.7 Triggers: Chapter 5.8; Also recommended: the other textbook Views: Chapters 3.6, 25.8, 25.9 We discuss here material that is NOT covered in ANY books

3 Constraints in SQL A constraint = a property that we’d like our database to hold The system will enforce the constraint by taking some actions: –forbid an update –or perform compensating updates

4 Constraints in SQL Constraints in SQL: Keys, foreign keys Attribute-level constraints Tuple-level constraints Global constraints: assertions The more complex the constraint, the harder it is to check and to enforce simplest Most complex

5 Keys OR: CREATE TABLE Product ( name CHAR(30) PRIMARY KEY, category VARCHAR(20)) CREATE TABLE Product ( name CHAR(30) PRIMARY KEY, category VARCHAR(20)) CREATE TABLE Product ( name CHAR(30), category VARCHAR(20) PRIMARY KEY (name)) CREATE TABLE Product ( name CHAR(30), category VARCHAR(20) PRIMARY KEY (name)) Product(name, category)

6 Keys with Multiple Attributes CREATE TABLE Product ( name CHAR(30), category VARCHAR(20), price INT, PRIMARY KEY (name, category)) CREATE TABLE Product ( name CHAR(30), category VARCHAR(20), price INT, PRIMARY KEY (name, category)) NameCategoryPrice GizmoGadget10 CameraPhoto20 GizmoPhoto30 GizmoGadget40 Product(name, category, price)

7 Other Keys CREATE TABLE Product ( productID CHAR(10), name CHAR(30), category VARCHAR(20), price INT, PRIMARY KEY (productID), UNIQUE (name, category)) CREATE TABLE Product ( productID CHAR(10), name CHAR(30), category VARCHAR(20), price INT, PRIMARY KEY (productID), UNIQUE (name, category)) There is at most one PRIMARY KEY; there can be many UNIQUE

8 Foreign Key Constraints CREATE TABLE Purchase ( prodName CHAR(30) REFERENCES Product(name), date DATETIME) CREATE TABLE Purchase ( prodName CHAR(30) REFERENCES Product(name), date DATETIME) prodName is a foreign key to Product(name) name must be a key in Product Referential integrity constraints May write just Product (why ?)

9 NameCategory Gizmogadget CameraPhoto OneClickPhoto ProdNameStore GizmoWiz CameraRitz CameraWiz ProductPurchase

10 Foreign Key Constraints OR (name, category) must be a PRIMARY KEY CREATE TABLE Purchase ( prodName CHAR(30), category VARCHAR(20), date DATETIME, FOREIGN KEY (prodName, category) REFERENCES Product(name, category) CREATE TABLE Purchase ( prodName CHAR(30), category VARCHAR(20), date DATETIME, FOREIGN KEY (prodName, category) REFERENCES Product(name, category)

11 NameCategory Gizmogadget CameraPhoto OneClickPhoto ProdNameStore GizmoWiz CameraRitz CameraWiz ProductPurchase What happens during updates ? Types of updates: In Purchase: insert/update In Product: delete/update

12 What happens during updates ? SQL has three policies for maintaining referential integrity: Reject violating modifications (default) Cascade: after a delete/update do a delete/update Set-null set foreign-key field to NULL READING ASSIGNEMNT: 7.1.5, 7.1.6

13 Constraints on Attributes and Tuples Constraints on attributes: NOT NULL-- obvious meaning... CHECK condition-- any condition ! Constraints on tuples CHECK condition

14 CREATE TABLE Purchase ( prodName CHAR(30), date DATETIME NOT NULL) CREATE TABLE Purchase ( prodName CHAR(30), date DATETIME NOT NULL)

15 CREATE TABLE Purchase ( prodName CHAR(30) CHECK (prodName IN SELECT Product.name FROM Product), date DATETIME NOT NULL) CREATE TABLE Purchase ( prodName CHAR(30) CHECK (prodName IN SELECT Product.name FROM Product), date DATETIME NOT NULL) What is the difference from Foreign-Key ?

16 General Assertions CREATE ASSERTION myAssert CHECK NOT EXISTS( SELECT Product.name FROM Product, Purchase WHERE Product.name = Purchase.prodName GROUP BY Product.name HAVING count(*) > 200) CREATE ASSERTION myAssert CHECK NOT EXISTS( SELECT Product.name FROM Product, Purchase WHERE Product.name = Purchase.prodName GROUP BY Product.name HAVING count(*) > 200)

17 Comments on Constraints Can give them names, and alter later We need to understand exactly when they are checked We need to understand exactly what actions are taken if they fail

18 Semantic Optimization Apply constraints to rewrite the query Simple example: SELET x.a FROM R x, S y WHERE x.fk=y.key same as SELECT x.a FROM R.x More advanced optimizations possible using complex constraints

19 Triggers Trigger = a procedure invoked by the DBMS in response to an update to the database Trigger = Event + Condition + Action Recommended reading: Chapt. 7 from The Complete Book

20 Triggers in SQL A trigger contains an event, a condition, an action. Event = INSERT, DELETE, UPDATE Condition = any WHERE condition (may refer to the old and the new values) Action = more inserts, deletes, updates Many, many more bells and whistles... Read in the book (it only scratches the surface...)

21 Triggers Enable the database programmer to specify: when to check a constraint, what exactly to do. A trigger has 3 parts: An event (e.g., update to an attribute) A condition (e.g., a query to check) An action (deletion, update, insertion) When the event happens, the system will check the constraint, and if satisfied, will perform the action. NOTE: triggers may cause cascading effects. Database vendors did not wait for standards with triggers!

22 Elements of Triggers (in SQL3) Timing of action execution: before, after or instead of triggering event The action can refer to both the old and new state of the database. Update events may specify a particular column or set of columns. A condition is specified with a WHEN clause. The action can be performed either for once for every tuple, or once for all the tuples that are changed by the database operation.

23 Example: Row Level Trigger CREATE TRIGGER InsertPromotions AFTER UPDATE OF price ON Product REFERENCING OLD AS OldTuple NEW AS NewTuple FOR EACH ROW WHEN (OldTuple.price > NewTuple.price) INSERT INTO Promotions(name, discount) VALUES OldTuple.name, (OldTuple.price-NewTuple.price)*100/OldTuple.price Event Condition Action

24 EVENTS INSERT, DELETE, UPDATE Trigger can be: –AFTER event –INSTEAD of event

25 Scope FOR EACH ROW = trigger executed for every row affected by update –OLD ROW –NEW ROW FOR EACH STATEMENT = trigger executed once for the entire statement –OLD TABLE –NEW TABLE

26 Statement Level Trigger CREATE TRIGGER average-price-preserve INSTEAD OF UPDATE OF price ON Product REFERENCING OLD_TABLE AS OldStuff NEW_TABLE AS NewStuff FOR EACH STATEMENT WHEN (1000 < (SELECT AVG (price) FROM ((Product EXCEPT OldStuff) UNION NewStuff)) DELETE FROM Product WHERE (name, price, company) IN OldStuff; INSERT INTO Product (SELECT * FROM NewStuff)

27 Bad Things Can Happen CREATE TRIGGER Bad-trigger AFTER UPDATE OF price IN Product REFERENCING OLD AS OldTuple NEW AS NewTuple FOR EACH ROW WHEN (NewTuple.price > 50) UPDATE Product SET price = NewTuple.price * 2 WHERE name = NewTuple.name

28 Trigers v.s. Integrity Constraints Triggers can be used to enforce ICs More versatile: –Your project: ORDER should always “get” the address from CUSTOMER May have other usages: –User alerts, generate log events for auditing Hard to understand –E.g. recursive triggers

29 Views Views are relations, except that they are not physically stored. For presenting different information to different users Employee(ssn, name, department, project, salary) Payroll has access to Employee, others only to Developers CREATE VIEW Developers AS SELECT name, project FROM Employee WHERE department = ‘Development’ CREATE VIEW Developers AS SELECT name, project FROM Employee WHERE department = ‘Development’

30 CREATE VIEW CustomerPrice AS SELECT x.customer, y.price FROM Purchase x, Product y WHERE x.product = y.pname CREATE VIEW CustomerPrice AS SELECT x.customer, y.price FROM Purchase x, Product y WHERE x.product = y.pname Example Purchase(customer, product, store) Product(pname, price) CustomerPrice(customer, price) “virtual table”

31 SELECT u.customer, v.store FROM CustomerPrice u, Purchase v WHERE u.customer = v.customer AND u.price > 100 SELECT u.customer, v.store FROM CustomerPrice u, Purchase v WHERE u.customer = v.customer AND u.price > 100 We can later use the view: Purchase(customer, product, store) Product(pname, price) CustomerPrice(customer, price)

32 Types of Views Virtual views: –Used in databases –Computed only on-demand – slow at runtime –Always up to date Materialized views –Used in data warehouses –Pre-computed offline – fast at runtime –May have stale data

33 Issues in Virtual Views Query Modification Applications Updating views Query minimization

34 Queries Over Views: Query Modification SELECT u.customer, v.store FROM CustomerPrice u, Purchase v WHERE u.customer = v.customer AND u.price > 100 SELECT u.customer, v.store FROM CustomerPrice u, Purchase v WHERE u.customer = v.customer AND u.price > 100 CREATE VIEW CustomerPrice AS SELECT x.customer, y.price FROM Purchase x, Product y WHERE x.product = y.pname CREATE VIEW CustomerPrice AS SELECT x.customer, y.price FROM Purchase x, Product y WHERE x.product = y.pname View: Query:

35 Queries Over Views: Query Modification SELECT u.customer, v.store FROM (SELECT x.customer, y.price FROM Purchase x, Product y WHERE x.product = y.pname) u, Purchase v WHERE u.customer = v.customer AND u.price > 100 SELECT u.customer, v.store FROM (SELECT x.customer, y.price FROM Purchase x, Product y WHERE x.product = y.pname) u, Purchase v WHERE u.customer = v.customer AND u.price > 100 Modified query:

36 Queries Over Views: Query Modification SELECT x.customer, v.store FROM Purchase x, Product y, Purchase v, WHERE x.customer = v.customer AND y.price > 100 AND x.product = y.pname SELECT x.customer, v.store FROM Purchase x, Product y, Purchase v, WHERE x.customer = v.customer AND y.price > 100 AND x.product = y.pname Modified and rewritten query:

37 But What About This ? SELECT DISTINCT u.customer, v.store FROM CustomerPrice u, Purchase v WHERE u.customer = v.customer AND u.price > 100 SELECT DISTINCT u.customer, v.store FROM CustomerPrice u, Purchase v WHERE u.customer = v.customer AND u.price > 100 ??

38 Answer SELECT DISTINCT u.customer, v.store FROM CustomerPrice u, Purchase v WHERE u.customer = v.customer AND u.price > 100 SELECT DISTINCT u.customer, v.store FROM CustomerPrice u, Purchase v WHERE u.customer = v.customer AND u.price > 100 SELECT DISTINCT x.customer, v.store FROM Purchase x, Product y, Purchase v, WHERE x.customer = v.customer AND y.price > 100 AND x.product = y.pname SELECT DISTINCT x.customer, v.store FROM Purchase x, Product y, Purchase v, WHERE x.customer = v.customer AND y.price > 100 AND x.product = y.pname

39 Set v.s. Bag Semantics SELECT DISTINCT a,b,c FROM R, S, T WHERE... SELECT DISTINCT a,b,c FROM R, S, T WHERE... SELECT a,b,c FROM R, S, T WHERE... SELECT a,b,c FROM R, S, T WHERE... Set semantics Bag semantics

40 Inlining Queries: Sets/Sets SELECT DISTINCT a,b,c FROM (SELECT DISTINCT u,v FROM R,S WHERE …), T WHERE... SELECT DISTINCT a,b,c FROM (SELECT DISTINCT u,v FROM R,S WHERE …), T WHERE... SELECT DISTINCT a,b,c FROM R, S, T WHERE... SELECT DISTINCT a,b,c FROM R, S, T WHERE...

41 Inlining Queries: Sets/Bags SELECT DISTINCT a,b,c FROM (SELECT u,v FROM R,S WHERE …), T WHERE... SELECT DISTINCT a,b,c FROM (SELECT u,v FROM R,S WHERE …), T WHERE... SELECT DISTINCT a,b,c FROM R, S, T WHERE... SELECT DISTINCT a,b,c FROM R, S, T WHERE...

42 Inlining Queries: Bags/Bags SELECT a,b,c FROM (SELECT u,v FROM R,S WHERE …), T WHERE... SELECT a,b,c FROM (SELECT u,v FROM R,S WHERE …), T WHERE... SELECT a,b,c FROM R, S, T WHERE... SELECT a,b,c FROM R, S, T WHERE...

43 Inlining Queries: Bags/Sets SELECT a,b,c FROM (SELECT DISTINCT u,v FROM R,S WHERE …), T WHERE... SELECT a,b,c FROM (SELECT DISTINCT u,v FROM R,S WHERE …), T WHERE... NO

44 Applications of Virtual Views Logical data independence Typical examples: –Vertical data partitioning –Horizontal data partitioning Security –Table V reveals only what the users are allowed to know

45 Vertical Partitioning SSNNameAddressResumePicture MaryHustonClob1…Blob1… SueSeattleClob2…Blob2… JoanSeattleClob3…Blob3… AnnPortlandClob4…Blob4… Resumes SSNNameAddress MaryHuston SueSeattle... SSNResume Clob1… Clob2… SSNPicture Blob1… Blob2… T1 T2T3

46 Vertical Partitioning CREATE VIEW Resumes AS SELECT T1.ssn, T1.name, T1.address, T2.resume, T3.picture FROM T1,T2,T3 WHERE T1.ssn=T2.ssn and T2.ssn=T3.ssn CREATE VIEW Resumes AS SELECT T1.ssn, T1.name, T1.address, T2.resume, T3.picture FROM T1,T2,T3 WHERE T1.ssn=T2.ssn and T2.ssn=T3.ssn When do we use vertical partitioning ?

47 Vertical Partitioning SELECT address FROM Resumes WHERE name = ‘Sue’ SELECT address FROM Resumes WHERE name = ‘Sue’ Which of the tables T1, T2, T3 will be queried by the system ?

48 Vertical Partitioning Applications: When some fields are large, and rarely accessed –E.g. Picture In distributed databases –Customer personal info at one site, customer profile at another In data integration –T1 comes from one source –T2 comes from a different source

49 Horizontal Partitioning SSNNameCityCountry MaryHustonUSA SueSeattleUSA JoanSeattleUSA AnnPortlandUSA --FrankCalgaryCanada --JeanMontrealCanada Customers SSNNameCityCountry MaryHustonUSA CustomersInHuston SSNNameCityCountry SueSeattleUSA JoanSeattleUSA CustomersInSeattle SSNNameCityCountry --FrankCalgaryCanada --JeanMontrealCanada CustomersInCanada

50 Horizontal Partitioning CREATE VIEW Customers AS CustomersInHuston UNION ALL CustomersInSeattle UNION ALL... CREATE VIEW Customers AS CustomersInHuston UNION ALL CustomersInSeattle UNION ALL...

51 Horizontal Partitioning SELECT name FROM Cusotmers WHERE city = ‘Seattle’ SELECT name FROM Cusotmers WHERE city = ‘Seattle’ Which tables are inspected by the system ? WHY ???

52 Horizontal Partitioning CREATE VIEW Customers AS (SELECT * FROM CustomersInHuston WHERE city = ‘Huston’) UNION ALL (SELECT * FROM CustomersInSeattle WHERE city = ‘Seattle’) UNION ALL... CREATE VIEW Customers AS (SELECT * FROM CustomersInHuston WHERE city = ‘Huston’) UNION ALL (SELECT * FROM CustomersInSeattle WHERE city = ‘Seattle’) UNION ALL... Better:

53 Horizontal Partitioning SELECT name FROM Cusotmers WHERE city = ‘Seattle’ SELECT name FROM Cusotmers WHERE city = ‘Seattle’ SELECT name FROM CusotmersInSeattle SELECT name FROM CusotmersInSeattle

54 Horizontal Partitioning Applications: Optimizations: –E.g. archived applications and active applications Distributed databases Data integration

55 Views and Security CREATE VIEW PublicCustomers SELECT Name, Address FROM Customers CREATE VIEW PublicCustomers SELECT Name, Address FROM Customers NameAddressBalance MaryHuston SueSeattle-240 JoanSeattle AnnPortland-520 Fred is allowed to see this Customers: Fred is not allowed to see this

56 Views and Security NameAddressBalance MaryHuston SueSeattle-240 JoanSeattle AnnPortland-520 CREATE VIEW BadCreditCustomers SELECT * FROM Customers WHERE Balance < 0 CREATE VIEW BadCreditCustomers SELECT * FROM Customers WHERE Balance < 0 Customers: John is allowed to see only <0 balances

57 CREATE VIEW Expensive-Product AS SELECT pname FROM Product WHERE price > 100 CREATE VIEW Expensive-Product AS SELECT pname FROM Product WHERE price > 100 Updating Views INSERT INTO Expensive-Product VALUES(‘Gizmo’) INSERT INTO Expensive-Product VALUES(‘Gizmo’) INSERT INTO Product VALUES(‘Gizmo’, NULL) INSERT INTO Product VALUES(‘Gizmo’, NULL) Purchase(customer, product, store) Product(pname, price) Updateable view

58 CREATE VIEW AcmePurchase AS SELECT customer, product FROM Purchase WHERE store = ‘AcmeStore’ CREATE VIEW AcmePurchase AS SELECT customer, product FROM Purchase WHERE store = ‘AcmeStore’ Updating Views INSERT INTO Toy-Product VALUES(‘Joe’, ‘Gizmo’) INSERT INTO Toy-Product VALUES(‘Joe’, ‘Gizmo’) INSERT INTO Product VALUES(‘Joe’,’Gizmo’,NULL) INSERT INTO Product VALUES(‘Joe’,’Gizmo’,NULL) Note this Purchase(customer, product, store) Product(pname, price) Updateable view

59 Updating Views INSERT INTO CustomerPrice VALUES(‘Joe’, 200) ? ? ? ? ? Non-updateable view Most views are non-updateable CREATE VIEW CustomerPrice AS SELECT x.customer, y.price FROM Purchase x, Product y WHERE x.product = y.pname CREATE VIEW CustomerPrice AS SELECT x.customer, y.price FROM Purchase x, Product y WHERE x.product = y.pname Purchase(customer, product, store) Product(pname, price)

60 Query Minimization CREATE VIEW CheapOrders AS SELECT x.cid,x.pid,x.date,y.name,y.price FROM Order x, Product y WHERE x.pid = y.pid and y.price < 100 CREATE VIEW LightOrders AS SELECT a.cid,a.pid,a.date,b.name,b.price FROM Order a, Product b WHERE a.pid = b.pid and b.weight < 100 CREATE VIEW CheapOrders AS SELECT x.cid,x.pid,x.date,y.name,y.price FROM Order x, Product y WHERE x.pid = y.pid and y.price < 100 CREATE VIEW LightOrders AS SELECT a.cid,a.pid,a.date,b.name,b.price FROM Order a, Product b WHERE a.pid = b.pid and b.weight < 100 Order(cid, pid, date) Product(pid, name, weight, price) SELECT u.cid FROM CheapOrders u, LightOrders v WHERE u.pid = v.pid and u.cid = v.cid SELECT u.cid FROM CheapOrders u, LightOrders v WHERE u.pid = v.pid and u.cid = v.cid Customers who bought a cheap, light product

61 Query Minimization CREATE VIEW CheapOrders AS SELECT x.cid,x.pid,x.date,y.name,y.price FROM Order x, Product y WHERE x.pid = y.pid and y.price < 100 CREATE VIEW LightOrders AS SELECT a.cid,a.pid,a.date,b.name,b.price FROM Order a, Product b WHERE a.pid = b.pid and b.weight < 100 CREATE VIEW CheapOrders AS SELECT x.cid,x.pid,x.date,y.name,y.price FROM Order x, Product y WHERE x.pid = y.pid and y.price < 100 CREATE VIEW LightOrders AS SELECT a.cid,a.pid,a.date,b.name,b.price FROM Order a, Product b WHERE a.pid = b.pid and b.weight < 100 Order(cid, pid, date) Product(pid, name, weight, price) SELECT u.cid FROM CheapOrders u, LightOrders v WHERE u.pid = v.pid and u.cid = v.cid SELECT u.cid FROM CheapOrders u, LightOrders v WHERE u.pid = v.pid and u.cid = v.cid SELECT a.cid FROM Order x, Product y Order a, Product b WHERE.... SELECT a.cid FROM Order x, Product y Order a, Product b WHERE.... Redundant Orders and Products

62 Query Minimization under Bag Semantics Rule 1: If x, y are tuple variables over the same table and x.id = y.id, then combine x, y into a single variable Rule 2: If x ranges over S, y ranges over T, and the only condition on y is x.fk = y.key, then remove T from the query

63 SELECT a.cid FROM Order x, Product y, Order a, Product b WHERE x.pid = y.pid and a.pid = b.pid and y.price < 100 and b.weight < 10 and x.cid = a.cid and x.pid = a.pid SELECT a.cid FROM Order x, Product y, Order a, Product b WHERE x.pid = y.pid and a.pid = b.pid and y.price < 100 and b.weight < 10 and x.cid = a.cid and x.pid = a.pid SELECT a.cid FROM Order x, Product y, Product b WHERE x.pid = y.pid and x.pid = b.pid and y.price < 100 and b.weight < 10 SELECT a.cid FROM Order x, Product y, Product b WHERE x.pid = y.pid and x.pid = b.pid and y.price < 100 and b.weight < 10 x = a SELECT a.cid FROM Order x, Product y WHERE x.pid = y.pid and y.price <100 and x.weight < 10 SELECT a.cid FROM Order x, Product y WHERE x.pid = y.pid and y.price <100 and x.weight < 10 y = b

64 Query Minimization under Set Semantics SELECT DISTINCT x.pid FROM Product x, Product y, Product z WHERE x.category = y.category and y.price > 100 and x.category = z.category and z.price > 500 and z.weight > 10 SELECT DISTINCT x.pid FROM Product x, Product y, Product z WHERE x.category = y.category and y.price > 100 and x.category = z.category and z.price > 500 and z.weight > 10 SELECT DISTINCT x.pid FROM Product x, Product z WHERE x.category = z.category and z.price > 500 and z.weight > 10 SELECT DISTINCT x.pid FROM Product x, Product z WHERE x.category = z.category and z.price > 500 and z.weight > 10 Same as:

65 Query Minimization under Set Semantics Rule 3: Let Q’ be the query obtained by removing the tuple variable x from Q. If there exists a homomorphism from Q to Q’ then Q’ is equivalent to Q, hence one can safely remove x. Definition. A homomorphism from Q to Q’ is mapping h from the tuple variables of Q to those of Q’ s.t. for every predicate P in the WHERE clause of Q, the predicate h(P) is logically implied by the WHERE clause in Q’

66 Homomorphism SELECT DISTINCT x.pid FROM Product x, Product y, Product z WHERE x.category = y.category and y.price > 100 and x.category = z.category and z.price > 500 and z.weight > 10 SELECT DISTINCT x.pid FROM Product x, Product y, Product z WHERE x.category = y.category and y.price > 100 and x.category = z.category and z.price > 500 and z.weight > 10 SELECT DISTINCT x’.pid FROM Product x’, Product z’ WHERE x’.category = z’.category and z’.price > 500 and z’.weight > 10 SELECT DISTINCT x’.pid FROM Product x’, Product z’ WHERE x’.category = z’.category and z’.price > 500 and z’.weight > 10 Q Q’ H(x) = x’, H(y) = H(z) = z’

67 Materialized Views Examples: Indexes Join indexes Views in data warehouses Distribution/replication

68 Issues with Materialized Views Synchronization –View becomes stale when base tables get updated Query rewriting using views –Much harder than query modification View selection –Given a choice, which views should we materialize ?

69 View Synchronization Immediate synchronization = after each update Deferred synchronization –Lazy = at query time –Periodic –Forced = manual Which one is best for: indexes, data warehouses, replication ?

70 Denormalization: Story From the Trenches Graduate Admissions: Application(id, name, school) GRE(id, score, year) /* normalization ! */ Very common query: List(id, name, school, GRE-some-average-or-last-score) VERY SLOW ! Solution: Application(id,name,school,GRE) De-normalized; computed field; materialized view Synchronized periodically (once per night).

71 CREATE VIEW FullOrder AS SELECT x.cid,x.pid,x.date,y.name,y.price FROM Order x, Product y WHERE x.pid = y.pid CREATE VIEW FullOrder AS SELECT x.cid,x.pid,x.date,y.name,y.price FROM Order x, Product y WHERE x.pid = y.pid Incremental View Update UPDATE Product SET price = price / 2 WHERE pid = ‘12345’ UPDATE Product SET price = price / 2 WHERE pid = ‘12345’ Order(cid, pid, date) Product(pid, name, price) UPDATE FullOrder SET price = price / 2 WHERE pid = ‘12345’ UPDATE FullOrder SET price = price / 2 WHERE pid = ‘12345’ No need to recompute the entire view !

72 CREATE VIEW Categories AS SELECT DISTINCT category FROM Product CREATE VIEW Categories AS SELECT DISTINCT category FROM Product Incremental View Update DELETE Product WHERE pid = ‘12345’ DELETE Product WHERE pid = ‘12345’ Product(pid, name, category, price) DELETE Categories WHERE category in (SELECT category FROM Product WHERE pid = ‘12345’) DELETE Categories WHERE category in (SELECT category FROM Product WHERE pid = ‘12345’) It doesn’t work ! Why ? How can we fix it ?

73 Answering Queries Using Views What if we want to use a set of views to answer a query. Why? –The obvious reason…

74 Reusing a Materialized View Suppose I have only the result of SeattleView: SELECT y.buyer, y.seller, y.product, y.store FROM Person x, Purchase y WHERE x.city = ‘Seattle’ AND x.pname = y.buyer and I want to answer the query SELECT y.buyer, y.seller FROM Person x, Purchase y WHERE x.city = ‘Seattle’ AND x..pname = y.buyer AND y.product=‘gizmo’. Then, I can rewrite the query using the view.

75 Query Rewriting Using Views Rewritten query: SELECT buyer, seller FROM SeattleView WHERE product= ‘gizmo’ Original query: SELECT y.buyer, y.seller FROM Person x, Purchase y WHERE x.city = ‘Seattle’ AND x..pname = y.buyer AND y.product=‘gizmo’.

76 Another Example I still have only the result of SeattleView: SELECT y.buyer, y.seller, y.product, y.store FROM Person x, Purchase y WHERE x.city = ‘Seattle’ AND x.pname = y.buyer but I want to answer the query SELECT y.buyer, y.seller FROM Person x, Purchase y WHERE x.city = ‘Seattle’ AND x.pname = y.buyer AND x.Phone LIKE ‘ %’.

77 And Now? I still have only the result of SeattleOtherView: SELECT y.buyer, y.seller, y.product, y.store FROM Person x, Purchase y, Product z WHERE x.city = ‘Seattle’ AND x.pname = y.buyer AND y.product = z.name AND z.price < 100 but I want to answer the query SELECT y.buyer, y.seller FROM Person x, Purchase y WHERE x.city = ‘Seattle’ AND x.pname = y.buyer.

78 And Now? I still have only the result of: SELECT seller, buyer, Sum(Price) FROM Purchase WHERE Purchase.store = ‘The Bon’ Group By seller, buyer but I want to answer the query SELECT seller, Sum(Price) FROM Purchase WHERE Person.store = ‘The Bon’ Group By seller And what if it’s the other way around?

79 Finally… I still have only the result of: SELECT seller, buyer, Count(*) FROM Purchase WHERE Purchase.store = ‘The Bon’ Group By seller, buyer but I want to answer the query SELECT seller, Count(*) FROM Purchase WHERE Person.store = ‘The Bon’ Group By seller

80 The General Problem Given a set of views V1,…,Vn, and a query Q, can we answer Q using only the answers to V1,…,Vn?

81 Application 1: Horizontal Partition CREATE VIEW CustomersInHuston AS SELECT * FROM Customers WHERE city=‘Huston’ CREATE VIEW CustomersInSeattle AS SELECT * FROM Customers WHERE city=‘Seattle’.... CREATE VIEW CustomersInHuston AS SELECT * FROM Customers WHERE city=‘Huston’ CREATE VIEW CustomersInSeattle AS SELECT * FROM Customers WHERE city=‘Seattle’.... No more unions !

82 Application 1: Horizontal Partition SELECT name FROM Customer WHERE city = ‘Seattle’ SELECT name FROM Customer WHERE city = ‘Seattle’ Rewrite using available views: This is query rewriting using views SELECT name FROM CustomersInSeattle SELECT name FROM CustomersInSeattle

83 CREATE INDEX W ON Product(weight) CREATE INDEX P ON Product(price) CREATE INDEX W ON Product(weight) CREATE INDEX P ON Product(price) Application 2: Aggressive Use of Indexes SELECT weight, price FROM Product WHERE weight > 10 and price < 100 SELECT weight, price FROM Product WHERE weight > 10 and price < 100 Product(pid, name, weight, price, …many other attributes) Which files are needed to answer the query ? DMBS stores three files: ProductWP (big)(smaller)

84 CREATE VIEW W AS SELECT pid, weight FROM Product CREATE VIEW P AS SELECT pid, weight FROM Product CREATE VIEW W AS SELECT pid, weight FROM Product CREATE VIEW P AS SELECT pid, weight FROM Product Indexes ARE Views Product(pid, name, weight, price, …many other attributes) CREATE INDEX W ON Product(weight) CREATE INDEX P ON Product(price) CREATE INDEX W ON Product(weight) CREATE INDEX P ON Product(price)

85 Indexes ARE Views Product(pid, name, weight, price, …many other attributes) SELECT weight, price FROM Product WHERE weight > 10 and price < 100 SELECT weight, price FROM Product WHERE weight > 10 and price < 100 CREATE VIEW W AS SELECT pid, weight FROM Product CREATE VIEW P AS SELECT pid, weight FROM Product CREATE VIEW W AS SELECT pid, weight FROM Product CREATE VIEW P AS SELECT pid, weight FROM Product SELECT weight, price FROM W, P WHERE weight > 10 and price < 100 and W.pid = P.pid SELECT weight, price FROM W, P WHERE weight > 10 and price < 100 and W.pid = P.pid This, too, is query rewriting using views

86 Application 3: Semantic Caching Queries Q1, Q2, … have been executed, and their results are stored in main memory Now we need to compute a new query Q Sometimes we can use the prior results in answering Q This, too, is a form of query rewriting using views (why ?)