











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!












SQL Recursion
WITH stu that lo oks like Datalog rules an SQL query ab out EDB, IDB
Rule =
[RECURSIVE] R (
Example
Find Sally's cousins, using EDB Par(child, parent).
WITH Sib(x,y) AS SELECT p1.child, p2,child FROM Par p1, Par p WHERE p1.parent = p2.parent AND p1.child <> p2.child, RECURSIVE Cousin(x,y) AS Sib UNION (SELECT p1.child, p2.child FROM Par p1, Par p2, Cousin WHERE p1.parent = Cousin.x AND p2.parent = Cousin.y ) SELECT y FROM Cousin WHERE x = 'Sally';
Monotonicity Example
In addition to certain negations, an aggregation can cause nonmonotonicity.
Sells(bar , beer , price)
SELECT AVG(price) FROM Sells WHERE bar = 'Joe''s Bar';
Adding to Sells a tuple that gives a new b eer Jo e sells will usually change the average price of b eer at Jo e's.
Thus, the former result, which might b e a single tuple like (2:78) b ecomes another single tuple like (2:81), and the old tuple is lost.
Generalizing Stratum Graph to SQL
No de for each relation de ned by a \rule."
No de for each sub query in the \b o dy" of a rule.
Arc P! Q if
a) P is \head" of a rule, and Q is a relation app earing in the FROM list of the rule (not in the FROM list of a sub query), as argument of a UNION, etc. b) P is head of a rule, and Q is a sub query directly used in that rule (not nested within some larger sub query). c) P is a sub query, and Q is a relation or sub query used directly within P [analogous to (a) and (b) for rule heads].
Lab el the arc if P is not monotone in Q.
Requirement for legal SQL recursion: nite strata only.
A Nonmonotonic Example
Change the UNION to EXCEPT in the rule for Cousin.
RECURSIVE Cousin(x,y) AS Sib EXCEPT (SELECT p1.child, p2.child FROM Par p1, Par p2, Cousin WHERE p1.parent = Cousin.x AND p2.parent = Cousin.y )
Now, adding to the result of the sub query can delete Cousin facts; i.e., Cousin is nonmonotone in S Q.
Sib Cousin
S Q