Movies Title Director Actor The Trouble with Harry Hitchcock Gwenn The Trouble with Harry Hitchcock Forsythe The Trouble with Harry Hitchcock MacLaine The Trouble with Harry Hitchcock Hitchcock Cries and Whispers Bergman Andersson Cries and Whispers Bergman Sylwan Cries and Whispers Bergman Thulin Cries and Whispers Bergman Ullman Location Theater Address Phone Number Gaumont Opéra 31 bd. des Italiens 47 42 60 33 Saint André des Arts 30 rue Saint André des Arts 43 26 48 18 Le Champo 51 rue des Ecoles 43 54 51 60 Georges V 144 av. des Champs-Elysées 45 62 41 46 Les 7 Montparnassiens 98 bd. du Montparnasse 43 20 32 20 Pariscope Theater Title Schedule Gaumont Opéra Cries and Whispers 20:30 Saint André des Arts The Trouble with Harry 20:15 Georges V Cries and Whispers 22:15 Les 7 Montparnassiens Cries and Whispers 20:45 Figure 3.1: The CINEMA database
(4.1) Who is the director of Cries and Whispers? (4.2) Which theaters feature Cries and Whispers? (4.3) What are the address and phone number of the Le Champo? (4.4) List the names and addresses of theaters featuring a Bergman film. (4.5) Is a film directed by Bergman playing in Paris? (4.6) List the pairs of persons such that the first directed the second in a movie, and vice versa. (4.7) List the names of directors who have acted in a movie they directed. (4.8) List pairs of actors that acted in the same movie. (4.9) On any input produce Apocalypse Now, Coppola as the answer. (4.10) Where can I see Annie Hall or Manhattan? (4.11) What are the films with Allen as actor or director? (4.12) What films with Allen as actor or director are currently featured at the Concorde? (4.13) List all movies that were directed by Hitchcock or that are currently playing at the Rex. (4.14) List all actors and director of the movie Apocalypse Now. Figure 4.1: Examples of conjunctive queries, some of which require union
Relational Model [AHV, Ch. 3] (p.30) - Infinite set att of attributes: A, B,... (sets of attributes are denoted by U, U 1,U 2,...). - For convenience, attributes are totally ordered by att. - Countably infinite set dom of constants: a, b, c,... - Countably infinite set relname of relation names: R 1,R 2,... - sort is a function that associates a finite set of attributes with each relation name R. - For each finite subset U of att there are infinitely many R with sort(r) =U. 5
- A relation schema is a relation name R, sometimes written R[U] when sort(r) =U or R[n], when U = {A 1,A 2,...,A n }. - arity(r) = n, when sort(r) = n. - A database schema R is a finite non-empty set of relation names. Sometimes written R = {R 1 [U 1 ],...,R m [U m ]}. Example: CINEMA = {Movies, Location, Pariscope} sort(movies) ={Title, Director, Actor} sort(location)={theater, Address, Phone Number} sort(pariscope) ={Theater, Title, Schedule} 6
Named vs. Unnamed Perspective (Section 3.2, p.31) Named: attribute names can be explicitly referred to. A tuple over a relation schema R[U] is a function from U to dom. R A B C u a b c u(a) = a u(b) = b u(c) = c Tuples are written A : a, B : b, C : c. For a tuple over R[U] and V U: u[v ]=u V (restriction). u[ab] = A : a, B : b. 7
Unnamed Version (p.32) dom n = dom dom... dom If arity(r) =n, then a tuple over R is an element of dom n. u = a, b, c u(i): i thelementofu u(2) = b Because of att, there is a one-to-one correspondence between the named and unnamed versions: A : a, B : b, C : c a, b, c att 1 :a, 2:b, 3:c 8
Conventional vs. Logic Programming Perspective (Section 3.3, p.32) Conventional: a relation (relation instance) I of R is a finite set of tuples over R. A database instance I over schema R is a mapping I(R) that associates an instance I with each R in R. Logic programming: Let arity(r) =n. A fact over R is an expression of the form R(a 1,a 2,...,a n ) with a i dom for 1 i n. If u = a 1,...,a n we can also write R(u). A relation instance of R is a finite set of facts over R. A database instance of R = {R 1,...,R m } is the union of relation instances over R 1,R 2,...,R m. 9
Example (p.33) Schema = {R,S}, sort(r) =AB, sort(s) =B R A B a b c b a a Named conventional S A d I(R) ={f 1,f 2,f 3 }, I(S) ={g} f 1 (A) =a, f 1 (B) =b, f 2 (A) =c, f 2 (B) =b f 3 (A) =a, f 3 (B) =a, g(a) =d Unnamed conventional I(R) ={ a, b, c, b, a, a }, I(S) ={ d } Named logic programming I = {R(A : a, B : b), R(A : c, B : b), R(A : a, B : a), S(A : d)} 10
Unnamed logic programming I = {R(a, b), R(c, b), R(a, a), S(d)} We shall move freely between the different perspectives. One more concept: Let var be a countably infinite set of variables x, y, z, x 1,y 1,... ranging over dom. A free tuple over R[U] is a function from U to dom var. Free tuples can have variables. An atom over R is an expression R(e 1,e 2,...,e n ), where each e i is a variable or constant. If all e i are constants, R(e 1,e 2,...,e n ) is called a ground atom (a.k.a. fact). 11
Algebraic Languages (Section 4.4, p.52) Two versions: one for the unnamed model and one for the named model Unnamed model: SPC-algebra List the names and addresses of theatres featuring a Bergman film. Step-by-step: I 1 := σ 2= Bergman (Movies) I 2 := I 1 Pariscope I 3 := σ 1=5 (I 2 ) I 4 := π 4 (I 3 ) I 5 := π 2,3 ( σ1=2 (I 4 Location) ) 32
As one expression: π 2,3 (σ 1=2 (π 4 (σ 1=5 (σ 2= Bergman (Movies) Pariscope)) Location)) Another equivalent query: π 4,8 (σ 4=7 (σ 1=5 (σ 2= Bergman (Movies Pariscope Location)))) Is algebra a procedural language? 33
The SPC operators, formally (p.53) Selection (atomic selection): σ j=a (I) = {t I t(j) =a} σ j=k (I) = {t I t(j) =t(k)} Projection: π j1,j 2,...,j n (I) ={ t(j 1 ),t(j 2 ),...,t(j n ) t I} Cartesian product: If arity(i) =n and arity(j) =m, then I J = { t(1),...,t(n),s(1),...,s(m) t I, s J} and arity(i J) =n + m. 34
SPC queries (p.54) The set of SPC-queries (Select, Project, Cartesian Product) consists of: Input relation: Expression R, with arity equal to arity(r). Unary singleton constant: Expression { a }, where a dom, witharity equal to 1. Furthermore, if q 1 and q 2 are SPC-queries, then: Selection: σ j=a (q 1 ) is a SPC query where j arity(q 1 ), with arity = arity(q 1 ). σ j=k (q 1 ) is a SPC query where j, k arity(q 1 ), with arity = arity(q 1 ). Projection: π j1,...,j n (q 1 ) is a SPC query where j 1,...,j n arity(q 1 ),witharity = n. Cartesian product: q 1 q 2 is a SPC query with arity = arity(q 1 )+arity(q 2 ). 35
Define the semantics of SPC queries (exercise). Unsatisfiable queries, e.g. σ 1=a (σ 1=b (R)). Derived Operators: Intersection I J = {t I t J}. I J = def π 1,...,n (σ 1=n+1 (...(σ n=2n (I J))...)), where arity(i) =arity(j) =n. Positive conjunctive selection σ F G (I) = def σ F (σ G (I)) Equijoin Let F = j 1 = k 1 j 2 = k 2... j n = k n. I F J = def σ j1 =m+k 1... j n =m+k n (I J), where arity(i) =m. 36
Named Perspective: SPJR algebra (p.56) Natural join: sort(r) = {A, B, C} sort(s) = {C, D} R S = {t over {A, B, C, D} for some u R and v S t[abc] =u and t[cd]=v} List the names and addresses of theatres featuring a Bergman film π Theater,Address ((σ Director= Bergman (Movies) Pariscope) Location) List pairs of actors that acted in the same movie We need to join Movies with itself (autojoin). We need renaming. 38
The operators of SPJR-algebra (p.57) Selection σ A=a (I) ={t I t(a) =a}, A sort(i) σ A=B (I) ={t I t(a) =t(b)}, A,B sort(i) Projection π A1,A 2,...,A n (I) ={t[a 1,...,A n ] t I}, {A 1,...,A n } sort(i) Natural join I J = {t sort(t) =sort(i) sort(j) and for some u I and v J t[sort(i)] = u and t[sort(j)] = v} Special cases: intersection and cross-product. Renaming (example) R A B C a b c c d e δ AC EF (R) E B F a b c c d e 39
Formally: renaming function f : U att If U = {A, B, C} and f(a) =A,f(B) =B, f(c) =C, we write AC A C.Now: δ f (I) = {t sort(t) =f(sort(i)) and for some s I, t(f(a)) = s(a), for each A sort(i)} Normal form for SPJR-algebra: π B1,...,Bn ({ A 1 : a 1 }... { A m : a m } σ F (δ f1 (R 1 )... δ fk (R k ))) Every SPJR-query can be rewritten into normal form, while preserving equivalence. 40
Lemma 4.4.7 (p.60) The SPC and SPJR algebras are equivalent. Idea of proof: use normal forms: π j1,...,j n ({ a 1 }... { a m }) σ F (R 1... R k ) and π Aj1,...,A j n ({ A 1 : a 1 }... { A m : a m } σ G (δ f1 (R 1 )... δ fk (R k ))) Theorem 4.4.8: The following classes of queries are equivalent: 1. Rule-based conjunctive queries. 2. Tableau queries. 3. Conjunctive calculus queries. 4. Satisfiable SPC-algebra. 5. Satisfiable SPJR-algebra. 41