Académique Documents
Professionnel Documents
Culture Documents
Carlo Zaniolo
Department of Computer Science
University of California, Los Angeles
December, 2001
took
Name
Major
Year
'Joe Doe'
cs
senior
'Jim Jones'
cs
junior
'Jim Black'
ee
junior
Name
Course
Grade
'Joe Doe'
cs123
2.7
'Jim Jones'
cs101
3.0
cs143
3.3
'Jim Black'
cs143
3.3
'Jim Black'
cs101
2.7
Rules
firstreq(Name)
student(Name, Major, junior),
took(Name, cs101, Grade1),
took(Name, cs143, Grade2).
Disjunction
Junior-level students who took course cs131 or
course cs151 with grade better than 3.0
scndreq(Name) took(Name, cs131, Grade),
Grade > 3.0, student(Name, Major, junior).
scndreq(Name) took(Name, cs151, Grade),
Grade > 3.0, student(Name, _ , junior).
QUERIES
A closed query; the answer to such query is either yes or not. For
instance,
? firstreq('Jim Black')
An open query: ?firstreq(X)
and its answer: firstreq('Jim Jones')
firstreq('Jim Black')
Much power and convenience in cascading!!!
E.g., Both requirements must be satisfied to enroll in cs298
reqcs298(Name)
firstreq(Name), scndreq(Name).
Relational Model
Table or Relation
Row or Tuple
Column
View
Datalog
Base Predicate
Fact
Argument
Derived Predicate
Negation in Datalog
req_missing(Name) student(Name,_,senior),
req(cs, Course), hastaken(Name, Course).
all_req_sat(Name)
student(Name, _, senior),
req_missing(Name).
explicit quantifiers.
Explicit Quantifiers
Existential
Relational DB Languages
Safe TRC and DRC expressions are equivalent, and there are
mappings that transform any formula in one language into an
equivalent one in the other.
For each TRC or DRC formula there is an equivalent, nonrecursive
Datalog program. The converse is also true, since a nonrecursive
Datalog program can be mapped into an equivalent DRC query.
Another language equivalent to these, is relational algebra (RA).
RA is an operator-based language, and thus provides a useful link
to concrete implementation of these logic-based languages.
Commercial DB Languages
Relational Operators
Cartesian product.
RS ={ t (r R ) (s S) (t[1,, n]=r t[n+1, , n+m]=s)}
If R has n columns and S has m columns, then R S contains
all the possible m+n tuples whose first m components form
a tuple in R and the last n components form a tuple in S.
Thus, R S has m+n columns and R S tuples, where R
and S denote the respective cardinalities of the two
relations.
L1 R = { r[L] r R }
F R = { t t R F}
Additional Operators
S = F ( R S)
Unsafe Rules
An unsafe Rule: to find grades better than the grade Joe Doe
got in cs143, a user might write
bettergrade(G1)
took(Joe Doe, cs143, G), G1 > G.
Infinite answers. Assuming that, say Joe Doe got the grade of 3.3 (i.e.,
B+) in course cs143, then, there are infinitely many numbers that
satisfy the conditions of being greater than 3.3.
Lack of domain independence. A query formula is said to be domain
independent when its answer only depends on the database and the
constants in the query, but not on the domain of interpretation. The set
of values for G1 satisfying the rule above depends on what domain we
assume for numbers: e.g., integer, rational or real.
No relational algebra equivalent. RA expression take DB tables and
constant as operands and return finite relations.
Safety
Mapping [Step 2]
The body of r is translated into the RA expression Bodyr
Bodyr is the cartesian product of all (base or derived)
relations in the body, followed by a selection F, where F
is the conjunction of the following conditions:
(i) inequality for each such goal (e.g., Z > 24.3),
(ii) equality between columns containing the same variables
(iii) equality between a column and the constant therein, e.g.
s(Z,b,Z) q(X,X,Y), p(Y,Z,a), Z > 24.3.
(ii)
(iii)
(i)
$5, b, $5 ( Bodyr)
Step 4: Multiple rules with the same head are translated into
the union or their equivalent expressions.