Professional Documents
Culture Documents
Chapter 25
Motivation
SQL-92 cannot express some queries:
Are we running low on any parts needed
to build a ZX600 sports car?
What is the total component and assembly
cost to build a ZX600 at today's part prices?
Can we extend the query language to cover
such queries?
Yes, by adding recursion.
Datalog
SQL queries can be read as follows:
If some tuples exist in the From tables that
satisfy the Where conditions,
then the Select tuple is in the answer.
Datalog is a query language that has the same
if-then flavor:
New: The answer table can appear in the
From clause, i.e., be defined recursively.
Prolog style syntax is commonly used.
spoke
frame
tire
rim
1
seat
1
pedal
tube
number
wheel
2
1
subpart
trike
part
Example
trike
wheel 3
trike
frame 1
frame seat
frame pedal 1
wheel spoke 2
wheel tire
tire
rim
tire
tube
Assembly instance
implication
body of rule
Example
trike
spoke
trike
spoke
trike
tire
trike
tire
trike
seat
trike
seat
trike
pedal
trike
pedal
wheel rim
wheel rim
wheel tube
wheel tube
Comp tuples
got by applying
Rule 2 once
trike
rim
trike
tube
Comp tuples
got by applying
Rule 2 twice
Comp(Part, Subpt) AS
C1
Comp C2
Fixpoints
Let f be a function that takes values from domain
D and returns values from D. A value v in D is a
fixpoint of f if f(v)=v.
Consider the fn double+, which is applied to a set
of integers and returns a set of integers (I.e., D is
the set of all sets of integers).
E.g., double+({1,2,5})={2,4,10} Union {1,2,5}
The set of all integers is a fixpoint of double+.
The set of all even integers is another fixpoint
of double+; it is smaller than the first fixpoint.
10
Negation
11
12
Stratification
T depends on S if some rule with T in the
head contains S or (recursively) some
predicate that depends on S, in the body.
Stratified program: If T depends on not S,
then S cannot depend on T (or not T).
If a program is stratified, the tables in the
program can be partitioned into strata:
13
Aggregate Operators
14
15
16
17
trike
There is a tuple (S1,S2) in
3
1
SameLev if there is a path
wheel
frame
up from S1 to some node
2
1
1
1
and down to S2 with the
spoke tire
seat pedal
same number of up and
1
1
down edges.
rim
tube
18
SameLev(spoke,seat) :- Assembly(wheel,spoke,2),
SameLev(wheel,frame), Assembly(frame,seat,1).
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke
19
20
21
22
23
Summary
Adding recursion extends relational algebra
and SQL-92 in a fundamental way; included
in SQL:1999, though not the core subset.
Semantics based on iterative fixpoint
evaluation. Programs with negation are
restricted to be stratified to ensure that
semantics is intuitive and unambiguous.
Evaluation must avoid repeated and
unnecessary inferences.
24