1 Datalog: Logic Instead of Algebra. 2 Datalog: Logic instead of Algebra Each relational-algebra...

7
1 Datalog: Logic Instead of Algebra

Transcript of 1 Datalog: Logic Instead of Algebra. 2 Datalog: Logic instead of Algebra Each relational-algebra...

Page 1: 1 Datalog: Logic Instead of Algebra. 2 Datalog: Logic instead of Algebra Each relational-algebra operator can be mimicked by one or several Database Logic.

1

Datalog:

Logic Instead of Algebra

Page 2: 1 Datalog: Logic Instead of Algebra. 2 Datalog: Logic instead of Algebra Each relational-algebra operator can be mimicked by one or several Database Logic.

2

Datalog: Logic instead of Algebra

Each relational-algebra operator can be mimicked by one or

several Database Logic (Datalog) that consists of if-then

rules. Datalog is inherently a logic of sets

Datalog queries are more powerful than relational algebra;

several rules can express recursions that are not

expressable in algebra

Relations are represented in Datalog as predicates;

predicate (R) is followed by its arguments is called an atom

Predicate returns a boolean value

Page 3: 1 Datalog: Logic Instead of Algebra. 2 Datalog: Logic instead of Algebra Each relational-algebra operator can be mimicked by one or several Database Logic.

3

Datalog: Logic instead of Algebra Rule: head body

Head: relational atom

: read “if”

Body: one or more atoms called subgoals which may be relational

or arithmetic

Example: LongMovie(t,y) Movies(t,y,l,s,p) AND l ≥ 100 It says: LongMovie(t,y) is true whenever we can find tuple in

Movies with: first 2 components as (t,y) and 3rd component as l that is at least 100, and any values in components 4 and 5

Equivalent to assignment statement in relational algebra: LongMovie := ∏title,year (σlength≥100(Movies))

Page 4: 1 Datalog: Logic Instead of Algebra. 2 Datalog: Logic instead of Algebra Each relational-algebra operator can be mimicked by one or several Database Logic.

4

Datalog: Logic instead of Algebra Extensional and Intentional Predicates:

Extensional Predicates (EDB) are predicates whose relations are stored in a database

Intentional Predicates (IDB) are predicates whose relations are computed by applying one or more Datalog rules

As long as there is no negated relational subgoals, evaluating rules when relations are sets apply for bags as well

Relational Algebra and Datalog: assume R(A,B,C), and S(A,B,C): Boolean:

Union: R υ S is equivalent to these 2 rules: U(A,B,C) R(A,B,C) U(A,B,C) S(A,B,C)

Page 5: 1 Datalog: Logic Instead of Algebra. 2 Datalog: Logic instead of Algebra Each relational-algebra operator can be mimicked by one or several Database Logic.

5

Datalog: Logic instead of Algebra Intersection: R ∩ S is equivalent to the following rule:

I(A,B,C) R(A,B,C) AND S(A,B,C) Set Difference: R - S is equivalent to the following rule:

D(A,B,C) R(A,B,C) AND NOT S(A,B,C)

Projection P(A,B) R(A,B,C)

Selection S(A,B,C) R(A,B,C) AND C ≥ 100

Product P(A,B,C,D,E.F) R(A,B,C) AND S(D,E,F)

Joins J(A,B,C,D) R(A,B) AND (S(B,C,D)

Page 6: 1 Datalog: Logic Instead of Algebra. 2 Datalog: Logic instead of Algebra Each relational-algebra operator can be mimicked by one or several Database Logic.

6

Datalog: Logic instead of Algebra

Simulating Multiple Operations with Datalog Example: Algebraic Expression

∏Title,year (σlength ≥ 100(Movies) ∩ σStudioName=‘Fox’(Movies))

Translates into this set of rules: W(t,y,l,g,s,p) Movies(t,y,l,g,s,p) AND l ≥ 100 X(t,y,l,g,s,p) Movies(t,y,l,g,s,p) AND s = ‘Fox’ Y(t,y,l,g,s,p) W(t,y,l,g,s,p) AND X(t,y,l,g,s,p) Answer(t,y) Y(t,y,l,g,s,p)

Page 7: 1 Datalog: Logic Instead of Algebra. 2 Datalog: Logic instead of Algebra Each relational-algebra operator can be mimicked by one or several Database Logic.

7

END