Professional Documents
Culture Documents
1
Updating Views
How can I insert a tuple into a table that doesn’t exist?
CREATE
CREATEVIEW
VIEW Developers
DevelopersASAS
SELECT
SELECTname,
name,project
project
FROM
FROM Employee
Employee
WHERE
WHEREdepartment
department==“Development”
“Development”
It becomes: INSERT
INSERTINTO
INTO Employee
Employee
VALUES(NULL,
VALUES(NULL,“Joe”,
“Joe”,NULL,
NULL,“Optimizer”,
“Optimizer”,NULL)
NULL)
2
Non-Updatable Views
CREATE
CREATEVIEW
VIEW Seattle-view
Seattle-view AS
AS
SELECT
SELECT seller,
seller,product,
product,store
store
FROM
FROM Person,
Person,Purchase
Purchase
WHERE
WHERE Person.city
Person.city==“Seattle”
“Seattle” AND
AND
Person.name
Person.name==Purchase.buyer
Purchase.buyer
We need to add “Joe” to Person first, but we don’t have all its attributes
3
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).
4
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.
5
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’.
6
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 %’.
7
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.
8
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
9
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
10
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). 11
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”
12
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
13
Constraints in SQL
Constraints in SQL:
• Keys, foreign keys simplest
• Attribute-level constraints
• Tuple-level constraints
Most
• Global constraints: assertions complex
OR:
CREATE
CREATETABLE
TABLEProduct
Product((
name
nameCHAR(30),
CHAR(30),
category
categoryVARCHAR(20)
VARCHAR(20)
PRIMARY
PRIMARYKEYKEY(name))
(name))
15
Keys with Multiple Attributes
CREATE
CREATETABLE
TABLE Product
Product ((
name
name CHAR(30),
CHAR(30),
category
category VARCHAR(20),
VARCHAR(20),
price
price INT,
INT,
PRIMARY
PRIMARYKEY KEY (name,
(name, category))
category))
16
Other Keys
CREATE
CREATE TABLE
TABLE Product
Product ((
productID
productID CHAR(10),
CHAR(10),
name
name CHAR(30),
CHAR(30),
category
category VARCHAR(20),
VARCHAR(20),
price
price INT,
INT,
PRIMARY
PRIMARY KEY KEY (productID),
(productID),
UNIQUE
UNIQUE (name,
(name, category))
category))
19
Foreign Key Constraints
• OR
CREATE
CREATE TABLE
TABLE Purchase
Purchase ((
prodName
prodName CHAR(30),
CHAR(30),
category
category VARCHAR(20),
VARCHAR(20),
date
date DATETIME,
DATETIME,
FOREIGN
FOREIGN KEY
KEY (prodName,
(prodName, category)
category)
REFERENCES
REFERENCES Product(name,
Product(name, category)
category)
• (name, category) must be a PRIMARY
KEY
20
What happens during updates ?
Types of updates:
• In Purchase: insert/update
• In Product: delete/update
Product Purchase
Name Category ProdName Store
23
What
is the difference from
Foreign-Key ?
CREATE
CREATE TABLE
TABLE Purchase
Purchase ((
prodName
prodName CHAR(30)
CHAR(30)
CHECK
CHECK (prodName
(prodName ININ
SELECT
SELECT Product.name
Product.name
FROM
FROM Product),
Product),
date
date DATETIME
DATETIME NOT
NOT NULL)
NULL)
24
General Assertions
CREATE
CREATE ASSERTION
ASSERTION myAssert
myAssert CHECK
CHECK
NOT
NOT EXISTS(
EXISTS(
SELECT
SELECT Product.name
Product.name
FROM
FROM Product,
Product, Purchase
Purchase
WHERE
WHERE Product.name
Product.name == Purchase.prodName
Purchase.prodName
GROUP
GROUP BY
BY Product.name
Product.name
HAVING
HAVING count(*)
count(*) >> 200)
200)
25
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
26
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...)
27