Download Datalog - Database Systems - Lecture Slides and more Slides Database Management Systems (DBMS) in PDF only on Docsity!
Datalog
Logical Rules
Recursion
Logic As a Query Language
- If-then logical rules have been used in many systems. - Important example: EII (Enterprise Information Integration).
- Nonrecursive rules are equivalent to the core relational algebra.
- Recursive rules extend relational algebra and appear in SQL-99.
EII – (2)
- Approach 2: Describe how JoeMenu can be used as a view to help answer queries about Sells and other relations.
JoeMenu(b, p) <- Sells(’Joe’’s Bar’, b, p)
- More about information integration later.
A Logical Rule
- Our first example of a rule uses the relations Frequents(drinker, bar), Likes(drinker, beer), and Sells(bar, beer, price).
- The rule is a query asking for “happy” drinkers --- those that frequent a bar that serves a beer that they like.
Subgoals Are Atoms
- An atom is a predicate , or relation name with variables or constants as arguments.
- The head is an atom; the body is the AND of one or more atoms.
- Convention: Predicates begin with a capital, variables begin with lower-case.
Example: Atom
Sells(bar, beer, p)
8
The predicate = name of a relation
Arguments are variables (or constants).
Example: Interpretation
Happy(d) <- Frequents(d,bar) AND
Likes(d,beer) AND Sells(bar,beer,p)
10
Distinguished variable
Nondistinguished variables
Interpretation: drinker d is happy if there exist a bar, a beer, and a price p such that d frequents the bar, likes the beer, and the bar sells the beer at price p.
Applying a Rule
- Approach 1: consider all combinations of values of the variables.
- If all subgoals are true, then evaluate the head.
- The resulting head is a tuple in the result.
A Glitch (Fixed Later)
- Relations are finite sets.
- We want rule evaluations to be finite and lead to finite results.
- “Unsafe” rules like P(x)<-Q(y) have infinite results, even if Q is finite.
- Even P(x)<-Q(x) requires examining an infinity of x -values.
Applying a Rule – (2)
- Approach 2: For each subgoal, consider all tuples that make the subgoal true.
- If a selection of tuples define a single value for each variable, then add the head to the result.
- Leads to finite search for P(x)<-Q(x), but P(x)<- Q(y) is problematic.
Arithmetic Subgoals
- In addition to relations as predicates, a predicate for a subgoal of the body can be an arithmetic comparison.
- We write arithmetic subgoals in the usual way, e.g., x < y.
Example: Arithmetic
- A beer is “cheap” if there are at least two bars that sell it for under $2.
Cheap(beer) <- Sells(bar1,beer,p1) AND
Sells(bar2,beer,p2) AND p1 < 2. AND p2 < 2.00 AND bar1 <> bar
Safe Rules
- A rule is safe if:
- Each distinguished variable,
- Each variable in an arithmetic subgoal, and
- Each variable in a negated subgoal, also appears in a nonnegated, relational subgoal.
- Safe rules prevent infinite results.
Example: Unsafe Rules
- Each of the following is unsafe and not allowed:
- S(x) <- R(y)
- S(x) <- R(y) AND NOT R(x)
- S(x) <- R(y) AND x < y
- In each case, an infinity of x ’s can satisfy the rule, even if R is a finite relation.