Download Recursive - Advanced Database System - Lecture Slides and more Slides Database Management Systems (DBMS) in PDF only on Docsity!
Recursive SQL, Deductive
Databases, Query Evaluation
Motivation
- Can SQL-92 express queries:
- Are we running low on any parts needed to build a ZX600 sports car?
- What is total component and assembly cost to build ZX600 at today's part prices?
- Can we extend the query language to cover such queries? - Yes, by adding recursion.
Example
trike wheel 3 trike frame 1 frame seat 1 frame pedal 1 wheel spoke 2 wheel tire 1 tire rim 1 tire tube 1 Assembly instance
part subpart number
trike
wheel frame
spoke tire seat pedal
rim tube
3 1
2 1 1 1 1 1
Example
- Query : “Find all components of trike !”
- Can you write relational algebra query to compute answer on the given instance of Assembly****?
trike wheel 3 trike frame 1 frame seat 1 frame pedal 1 wheel spoke 2 wheel tire 1 tire rim 1 tire tube 1 (^) Assembly instance
part subpart number
trike
wheel frame
spoke tire seat pedal rim tube
3 1
2 1 1 1 1 1
Problem with R.A. / SQL-
- Intuitively,
- we must join Assembly with itself to deduce that trike contains spoke and tire.
- Takes us one level down Assembly hierarchy.
- To find components that are yet one level deeper (e.g., rim), we need another join.
- To find all components, need as many joins as there are levels in the given instance!
Problem with R.A. and SQL-
- Conclude :
- we need as many joins as there are levels in the given instance!
- Problem :
- For any RA expression, we can create an Assembly instance for which some answers are not computed by including more levels than number of joins in expression!
Datalog
Datalog
Datalog : Relational QL inspired by Prolog.
Program : A collection of rules
Rule : If RHS exists, must be in LHS result.
Using Rule to Deduce New Tuples
- Example: Comp(Part, Subpt) :- Assembly(Part, Subpt, Qty).
- By setting (Part=trike, Subpt=wheel, Qty=3) in rule, we deduce that tuple < trike, wheel > is in relation Comp.
- This is called an inference using the rule.
- Rule Application :
- Given a set of tuples, we apply rule by making all possible inferences with tuples in body.
Example of Datalog
- Conjunctive queries : PROJECT (SELECT ( JOIN )).
- Conjunctive queries with UNION: Several rules.
- Conjunctive queries are “monotonic” : Applying to superset of instances will return a larger equal result.
Comp(Part, Subpt) :- Assembly(Part, Subpt, Qty), Qty>
trike wheel 3 trike frame 1 frame seat 1 frame pedal 1 wheel spoke 2 wheel tire 1 tire rim 1 tire tube 1 Assembly instance
trike spoke trike tire trike seat trike pedal wheel rim wheel tube Comp tuples by applying Rule 2 once
Comp(Part, Subpt) :- Assembly(Part, Part2, Qty), Comp(Part2, Subpt).
trike spoke trike tire trike seat trike pedal wheel rim wheel tube trike rim trike tube
Comp tuples by applying Rule 2 twice
Datalog vs. SQL Notation
- A collection of Datalog rules can be rewritten
in SQL syntax with recursion
Comp(Part, Subpt) :- Assembly(Part, Subpt, Qty). Comp(Part, Subpt) :- Assembly(Part, Part2, Qty), Comp(Part2, Subpt).
Datalog vs. SQL Notation
- Or, modify query to have selection:
WITH RECURSIVE Comp(Part, Subpt) AS (SELECT A1.Part, A1.Subpt FROM Assembly A1) UNION (SELECT A2.Part, C1.Subpt FROM Assembly A2, Comp C WHERE A2.Subpt=C1.Part); SELECT * FROM Comp C Where C2.part = trike ;
Theoretical
Foundations
(least fixpoint semantics – conceptual evaluation strategy a la relational algebra )