27
1 Lecture 6: Views Friday, January 17th, 2003

1 Lecture 6: Views Friday, January 17th, 2003. 2 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,

Embed Size (px)

Citation preview

Page 1: 1 Lecture 6: Views Friday, January 17th, 2003. 2 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,

1

Lecture 6: Views

Friday, January 17th, 2003

Page 2: 1 Lecture 6: Views Friday, January 17th, 2003. 2 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,

2

Updating ViewsHow can I insert a tuple into a table that doesn’t exist?

Employee(ssn, name, department, project, salary)

CREATE VIEW Developers AS SELECT name, project FROM Employee WHERE department = “Development”

CREATE VIEW Developers AS SELECT name, project FROM Employee WHERE department = “Development”

INSERT INTO Developers VALUES(“Joe”, “Optimizer”)

INSERT INTO Developers VALUES(“Joe”, “Optimizer”)

INSERT INTO Employee VALUES(NULL, “Joe”, NULL, “Optimizer”, NULL)

INSERT INTO Employee VALUES(NULL, “Joe”, NULL, “Optimizer”, NULL)

If we make thefollowing insertion:

It becomes:

Page 3: 1 Lecture 6: Views Friday, January 17th, 2003. 2 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,

3

Non-Updatable Views

CREATE VIEW Seattle-view AS

SELECT seller, product, store FROM Person, Purchase WHERE Person.city = “Seattle” AND Person.name = Purchase.buyer

CREATE VIEW Seattle-view AS

SELECT seller, product, store FROM Person, Purchase WHERE Person.city = “Seattle” AND Person.name = Purchase.buyer

How can we add the following tuple to the view?

(“Joe”, “Shoe Model 12345”, “Nine West”)

We need to add “Joe” to Person first, but we don’t have all its attributes

Page 4: 1 Lecture 6: Views Friday, January 17th, 2003. 2 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,

4

Answering Queries Using Views

• What if we want to use a set of views to answer a query.

• Why?– The obvious reason…– Answering queries over web data sources.

• Very cool stuff! (i.e., I did a lot of research on this).

Page 5: 1 Lecture 6: Views Friday, January 17th, 2003. 2 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,

5

Reusing a Materialized View• Suppose I have only the result of SeattleView: SELECT buyer, seller, product, store FROM Person, Purchase WHERE Person.city = ‘Seattle’ AND Person.per-name = Purchase.buyer

• and I want to answer the query SELECT buyer, seller FROM Person, Purchase WHERE Person.city = ‘Seattle’ AND Person.per-name = Purchase.buyer AND Purchase.product=‘gizmo’.

Then, I can rewrite the query using the view.

Page 6: 1 Lecture 6: Views Friday, January 17th, 2003. 2 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,

6

Query Rewriting Using Views

Rewritten query: SELECT buyer, seller FROM SeattleView WHERE product= ‘gizmo’

Original query: SELECT buyer, seller FROM Person, Purchase WHERE Person.city = ‘Seattle’ AND Person.per-name = Purchase.buyer AND Purchase.product=‘gizmo’.

Page 7: 1 Lecture 6: Views Friday, January 17th, 2003. 2 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,

7

Another Example• I still have only the result of SeattleView: SELECT buyer, seller, product, store FROM Person, Purchase WHERE Person.city = ‘Seattle’ AND Person.per-name = Purchase.buyer

• but I want to answer the query SELECT buyer, seller FROM Person, Purchase WHERE Person.city = ‘Seattle’ AND Person.per-name = Purchase.buyer AND Person.Phone LIKE ‘206 543 %’.

Page 8: 1 Lecture 6: Views Friday, January 17th, 2003. 2 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,

8

And Now?• I still have only the result of SeattleView: SELECT buyer, seller, product, store FROM Person, Purchase, Product WHERE Person.city = ‘Seattle’ AND Person.per-name = Purchase.buyer AND Purchase.product = Product.name

• but I want to answer the query SELECT buyer, seller FROM Person, Purchase WHERE Person.city = ‘Seattle’ AND Person.per-name = Purchase.buyer.

Page 9: 1 Lecture 6: Views Friday, January 17th, 2003. 2 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,

9

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?

Page 10: 1 Lecture 6: Views Friday, January 17th, 2003. 2 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,

10

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

Page 11: 1 Lecture 6: Views Friday, January 17th, 2003. 2 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,

11

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?

• Why do we care?– We can answer queries more efficiently. – We can query data sources on the WWW in a

principled manner.

• Many, many papers on this problem.

• The best performing algorithm: The MiniCon Algorithm, (Pottinger & (Ha)Levy, 2000).

Page 12: 1 Lecture 6: Views Friday, January 17th, 2003. 2 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,

12

Querying the WWW• Assume a virtual schema of the WWW, e.g.,

– Course(number, university, title, prof, quarter)

• Every data source on the web contains the answer to a view over the virtual schema:

UW database: SELECT number, title, prof FROM Course WHERE univ=‘UW’ AND quarter=‘2/02’Stanford database: SELECT number, title, prof, quarter FROM Course WHERE univ=‘Stanford’User query: find all professors who teach “database systems”

Page 13: 1 Lecture 6: Views Friday, January 17th, 2003. 2 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,

13

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

Page 14: 1 Lecture 6: Views Friday, January 17th, 2003. 2 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,

14

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

Mostcomplex

Page 15: 1 Lecture 6: Views Friday, January 17th, 2003. 2 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,

15

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))

Page 16: 1 Lecture 6: Views Friday, January 17th, 2003. 2 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,

16

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))

Page 17: 1 Lecture 6: Views Friday, January 17th, 2003. 2 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,

17

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

Page 18: 1 Lecture 6: Views Friday, January 17th, 2003. 2 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,

18

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

Referentialintegrity

constraints

Page 19: 1 Lecture 6: Views Friday, January 17th, 2003. 2 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,

19

Name Category

Gizmo gadget

Camera Photo

OneClick Photo

ProdName Store

Gizmo Wiz

Camera Ritz

Camera Wiz

Product Purchase

Page 20: 1 Lecture 6: Views Friday, January 17th, 2003. 2 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,

20

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)

Page 21: 1 Lecture 6: Views Friday, January 17th, 2003. 2 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,

21

Name Category

Gizmo gadget

Camera Photo

OneClick Photo

ProdName Store

Gizmo Wiz

Camera Ritz

Camera Wiz

Product Purchase

What happens during updates ?

Types of updates:

• In Purchase: insert/update

• In Product: delete/update

Page 22: 1 Lecture 6: Views Friday, January 17th, 2003. 2 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,

22

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

Page 23: 1 Lecture 6: Views Friday, January 17th, 2003. 2 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,

23

Constraints on Attributes and Tuples

• Constraints on attributes:NOT NULL -- obvious meaning...CHECK condition -- any condition !

• Constraints on tuplesCHECK condition

Page 24: 1 Lecture 6: Views Friday, January 17th, 2003. 2 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,

24

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)

Whatis the difference from

Foreign-Key ?

Page 25: 1 Lecture 6: Views Friday, January 17th, 2003. 2 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,

25

General Assertions

CREATE ASSERTION myAssert CHECK NOT EXISTS(

SELECT Product.nameFROM Product, PurchaseWHERE Product.name = Purchase.prodNameGROUP BY Product.nameHAVING count(*) > 200)

CREATE ASSERTION myAssert CHECK NOT EXISTS(

SELECT Product.nameFROM Product, PurchaseWHERE Product.name = Purchase.prodNameGROUP BY Product.nameHAVING count(*) > 200)

Page 26: 1 Lecture 6: Views Friday, January 17th, 2003. 2 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,

26

Final Comments on Constraints

• Can give them names, and alter later– Read in the book !!!

• We need to understand exactly when they are checked

• We need to understand exactly what actions are taken if they fail

Page 27: 1 Lecture 6: Views Friday, January 17th, 2003. 2 Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,

27

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...)