================================================================ CSE 344 -- Spring 2011 Lecture 09: Datalog and Relational Calculus ================================================================ Reading assignment: 5.3, 5.4 ----------------------------- A datalog rule is: Q(args) :- R1(args1), R2(args2), ... sometimes also written as: Q(args) :- R1(args1) AND R2(args2) AND ... Terminology: the rule head = Q(args) the rule body = R1(args1), R2(args2), ... atom, or subgoal = any one of Ri(argsi) head variables = the variables occurring the head existential variables = all the other variables A datalog program is: rule1 rule2 ... ----------------------- Examples of single rules: Schema: /*****************************************************/ /* Purchase(pid, product, price, quantity) */ /* Product(pname, manufacturer) */ /* (Purchase.product refers to Product.pname) */ /*****************************************************/ Find all products under 9.99: A(y) :- Purchase(x,y,z,u), z < 9.99 Find all manufacturers that manufacture some products under 9.99 A(v) :- Purchase(x,y,z,u), z < 9.99, Product(y, v) Find manfuacturers that manufacture products both < 9.99 and > 999.99 A(v) :- Purchase(x1,y1,z1,u1), z1 < 9.99, Product(y1, v), Purchase(x2,y2,z2,u2), z2 < 9.99, Product(y2, v), Change this query so it returns manfuacturer that sold THE SAME PRODUCT both under 9.99 and over 999.99 Examples of multiple rules: /************************/ /* Friend(name1, name2) */ /* Enemy(name1, name2) */ /************************/ Find Joe's friends, and Joe's friends' friends. A(x) :- Friend('Joe', x) A(x) :- Friend('Joe', z), Friend(z, x) What does this compute ? Mystery(x,y) :- Friend(x,z), Friend(z,y) Whata does this compute ? M1(x,y) :- Friend(x,z), Friend(z,y) M2(x,y) :- M1(x,z), M1(z,y) A(x,y) :- M2(x,z), M2(z,y) ---------------------- We allow only *non-recursive* datalog in ths course, meaning the following: the relation in the head of rule k cannot appera in the body of rules ---------------------- Meaning of a datalog rule = a logical statement ! M(x,y) :- Friend(x,z), Friend(z,y) Means: forall x. forall y. forall z. (Friend(x,z) and Friend(z,y) ==> M(x,y)) and M is the smallest relation that has this property Note: logically equivalent to: forall x. forall y. (exists z. Friend(x,z) and Friend(z,y)) ==> M(x,y) That's why the variables not in the head are called "existential variables". ---------------------- Can we express everythign in the Relational Algebra in datalog ? In class: 1. Union R(A,B) U S(A,B) 2. Selection 3. Join 4. Projections 5. Difference ---------------------- To express difference, we add negation (in class) ---------------------- Find Joe's friends' friends that are not Joe's friends. A(x) :- Friend('Joe',y), Friend(y,x), not Friend('Joe',x) --------------------- what's wrong ? A(x,y) :- Friend('Joe',y), Enemy(y,z) A(z) :- Friend('Joe', x), x != z A(x) :- Friend('Joe', x), not (Friend(x,y)) --------------------- "Safe datalog rule": every variable occurs in a positive atom. Every rule must be safe ! --------------------- **** In Class Find all x such that all their enemies' enemies are their friends Find all x having some friend all of whose enemies are x's enemies. --------------------- **** In class Express the two queries above in SQL. Note: this seems quite straighforward. datalog with negation = simple formalism for SQL --------------------- **** Can you write these two SQL queries WITHOUT subqueries ?