


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
SQL, Recursion, Recursion in SQL, Ancestor Query in SQL3, Fixed Point of a Function, Finding Ancestors, Intuition Behind Fixed-point Iteration, Linear Recursion, Linear vs. Non-linear Recursion, Mutual Recursion, Operational Semantics of "With", Computing Mutual Recursion, Mixing Negation with Recursion, Multiple Minimal Fixed Points, Legal Mix of Negation and Recursion, Dependency, Stratified Negation, Evaluating Stratified Negation, Evaluation Strategy, Stratum, Legal SQL3 Recursion
Typology: Slides
1 / 4
This page cannot be seen from the preview
Don't miss anything!



3
parent child Homer Bart Homer Lisa Marge Bart Marge Lisa Abe Homer Ape Abe
Parent ( parent , child )
Bart Lisa
Homer Marge
Abe
Ape
4
SELECT p1.parent AS grandparent FROM Parent p1, Parent p WHERE p1.child = p2.parent AND p2.child = ’Bart’;
5
Query using the relation defined in WITH clause
Define a a relation recursively
basis
induction
How do we compute such a recursive query?
6
0, because f (0) = 0 / 2 = 0
WITH Ancestor(anc, desc) AS ((SELECT parent, child FROM Parent) UNION (SELECT a1.anc, a2.desc FROM Ancestor a1, Ancestor a WHERE a1.desc = a2.anc)) Think of it as Ancestor = q ( Ancestor )
parent child Homer Bart Homer Lisa Marge Bart Marge Lisa Abe Homer Ape Abe
Parent ( parent , child )
anc desc anc desc Homer Bart Homer Lisa Marge Bart Marge Lisa Abe Homer Ape Abe
anc desc Homer Bart Homer Lisa Marge Bart Marge Lisa Abe Homer Ape Abe Abe Bart Abe Lisa Ape Homer
anc desc Homer Bart Homer Lisa Marge Bart Marge Lisa Abe Homer Ape Abe Abe Bart Abe Lisa Ape Homer Ape Bart Ape Lisa
9
10
WITH Ancestor(anc, desc) AS ((SELECT parent, child FROM Parent) UNION (SELECT a1.anc, a2.desc FROM Ancestor a1, Ancestor a WHERE a1.desc = a2.anc))
WITH Ancestor(anc, desc) AS ((SELECT parent, child FROM Parent) UNION (SELECT anc, child FROM Ancestor, Parent WHERE desc = parent))
11
12
WITH Even(n) AS (SELECT n FROM Natural WHERE n = ANY(SELECT n+1 FROM Odd)), Odd(n) AS ((SELECT n FROM Natural WHERE n = 1) UNION (SELECT n FROM Natural WHERE n = ANY(SELECT n+1 FROM Even)))
One node for each table defined in WITH A directed edge R → S if R is defined in terms of S Label the directed edge “–” if the query defining R is not monotone with respect to S
Called stratified negation
Ancestor
Legal!
Scholarship DeansList
WITH Ancestor(anc, desc) AS ((SELECT parent, child FROM Parent) UNION (SELECT a1.anc, a2.desc FROM Ancestor a1, Ancestor a WHERE a1.desc = a2.anc)), Person(person) AS ((SELECT parent FROM Parent) UNION (SELECT child FROM Parent)), NoCommonAnc(person1, person2) AS ((SELECT p1.person, p2.person FROM Person p1, Person p WHERE p1.person <> p2.person) EXCEPT (SELECT a1.desc, a2.desc FROM Ancestor a1, Ancestor a WHERE a1.anc = a2.anc)) SELECT * FROM NoCommonAnc;
Ancestor
Person
NoCommonAnc
21
Ancestor : stratum 0 Person : stratum 0 NoCommonAnc : stratum 1
Compute tables lowest-stratum first For each stratum, use fixed-point iteration on all nodes in that stratum
Ancestor
Person
NoCommonAnc
22