CSE 233 Spring, 2016 Solutions to Prolem Set #1 1. The movie tse onsists of the following two reltions movie: title, iretor, tor sheule: theter, title The first reltion provies titles, iretors, n tors of vrious movies. Assume movie is uniquely ientifie y its title. Do not ssume tht movie hs unique iretor (so movie y Hithok is one for whih one of the iretors is Hithok). The seon reltion provies the titles of urrently plying movies n the theters where they re eing shown. Express the following queries in (i) reltionl lulus n (ii) reltionl lger. () List the theters showing some movie y Hithok. (i) {t : th x (sheule(t, x) movie(x, Hithok, ))} (ii) π th (sheule π title (σ iretor=hithok (movie))) () List the theters showing only movies y Hithok. (i) {t : th x(sh(t, x)) x[sh(t, x) (movie(x, Hith, ))]} With tive omin semntis, the onjunt x(sh(t, x)) is neee to mke sure t is theter. (ii) π th (sh) π th [sh (π title (movie) π title (σ ir=hith (movie)))] 2. The ivision inry opertor on reltions is efine s follows. Given reltions P n Q for whih tt(q) tt(p ), P Q is reltion with ttriutes tt(p ) tt(q) ontining the tuples t for whih {t} Q P. For exmple, if tt(p ) = {A, B} n tt(q) = {B}, P Q is the reltion with ttriute {A} ontining the tuples for whih, P for every tuple Q. Intuitively, is iret implementtion of universl quntifition. 1
(i) Use (n stnr lger opertors) to express the query List the theters showing every movie y Hithok. sheule π title (σ ir=hith (movie)) (ii) Show how P Q n e expresse using the stnr reltionl lger opertors (you n ssume, for simpliity, tht tt(p ) = {A, B} n tt(q) = {B}). π A (P ) π A [π A (P ) Q P ] 3. Consier the following query on the ove sheule reltion: Fin the theters showing more thn one title (i) Express this query in reltionl lulus n reltionl lger. {t : th x 1 x 2 (sh(t, x 1 ) sh(t, x 2 ) x 1 x 2 )} π th (σ title title1 (sheule δ title title1 (sheule))) (σ A B (e) stns for e σ A=B (e)). (ii) ( ) Prove tht every reltionl lger expression efining the ove query must use the ttriute renming opertor δ. Suppose e expresses the query n uses no δ. In the proof elow we lso ssume tht e uses no onstnt reltions (the proof with onstnt reltions is slightly more omplite ut follows the sme lines). However, e my use seletion with onstnts. Let I e the instne of sh: sh th title 1 2 3 where e oes not use seletion with ny of the onstnts in C = {,, 1, 2, 3}. Consier ny reltionl lger expression f over sh 2
using no renming n no onstnts in C (e is one suh expression). Denote y tt(f) the set of ttriutes of the result of f. Oviously, tt(f) {th, title} sine no renming is use. We enote y I the reltion π th (I) π title (I) n Ī = (I) I. We show y struturl inution the following: if f hs one ttriute A {th, title}, then f(i) = or f(i) = π A (I). if tt(f) = {th, title} then f(i) {, I, Ī, (I)} Note tht this shows tht e nnot express the esire query, sine tt(e) = {th} so y the ove e(i) must equl or π th (I), neither of whih is the esire nswer {}. Bsis f = sh. Then tt(f) = {th, title} n f(i) = I. Inution step We onsier the following ses, where f 1 n f 2 re lger expressions over sh ssume, y the inution hypothesis, to stisfy the ove sttement: f = σ A= (f 1 ). Then f(i) =, sine C, so f 1 (I) oes not ontin. f = σ A=B (f 1 ). Then tt(f 1 ) = {th, title}, so A = th n B = title (or onversely). By the inution hypothesis, f 1 (I) {, I, Ī, (I)}, so σ A=B(f 1 (I)) =. f = π A (f 1 ). We n ssume wlog 1 tht tt(f 1 ) = {th, title}. By the inution hypothesis, f 1 (I) {, I, Ī, (I)}. It follows tht f(i) is either empty or equls π A (I). f is f 1 f 2, f 1 f 2, or f 1 f 2. By the inution hypothesis, f j (I) {, π th (I), π title (I), I, Ī, (I)}. By onsiering ll possiilities (sujet to typing onstrints), we see tht f(i) stisfies the sttement. This ompletes the inution n proves (ii). 4. (Automorphisms) (i) (5 points) Show tht CALC queries re invrint uner utomorphisms. In other wors, if ϕ( x) is CALC query without onstnts 1 Without loss of generlity. 3
over shem σ, then for every instne I over σ n one-to-one mpping f : om om, if f(i) = I then f(ϕ(i)) = ϕ(i). Hint: Assume without loss of generlity tht ϕ uses only,,, n use struturl inution on the formul. Let ϕ( x) e CALC formul with free vriles x. following: We prove the ( ) For eh instne I n utomorphism f of I, f(ϕ(i)) ϕ(i). One ( ) is proven, the onverse inlusion ϕ(i) f(ϕ(i)) is shown s follows. Sine f is n utomorphism of I, so is f 1. By ( ) we hve tht f 1 (ϕ(i)) ϕ(i). But then f(f 1 (ϕ(i))) f(ϕ(i)), so ϕ(i) f(ϕ(i)). The proof of ( ) is y struturl inution on the formul. Suppose ϕ( x) is n tom. If ϕ( x) = R( x) then ϕ(i) = I(R), so f(ϕ(i)) = f(i(r)) = I(R) = ϕ(i). If ϕ is x = y then ϕ(i) = {, om(i)} so f(ϕ(i)) = { f(), f() om(i)} = ϕ(i) (sine f(om(i)) = om(i)). For the inution step, we hve three ses: 1. ϕ = ϕ( x, ȳ, z) = α( x, ȳ) β(ȳ, z). Let f(ū), f( v), f( w) f(ϕ(i)), with ū, v, w ϕ(i). Then ū, v α(i) n v, w β(i). Sine f(α(i)) = α(i), f(ū), f( v) α(i) n similrly f( v), f( w) β(i). It follows tht f(ū), f( v), f( w) ϕ(i). We onlue tht f(ϕ(i)) ϕ(i). 2. ϕ = xα(x, ȳ). Let ū ϕ(i). Then there exists v om(i) suh tht v, ū α(i). Sine f(α(i)) = α(i), it follows tht f(v), f(ū) α(i), n f(ū) ϕ(i). Thus, f(ϕ(i)) ϕ(i). 3. ϕ = α( x). Let ū ϕ(i). Then ū om u n ū α(i). But then f(ū) om u n f(ū) α(i). Inee, suppose f(ū) α(i). Sine f 1 is n utomorphism of I, f 1 (α(i)) α(i) y the inution hypothesis, so f 1 (f(ū)) α(i) n ū α(i). However, this is flse. We onlue tht f(ū) α(i), so f(ū) ϕ(i) n f(ϕ(i)) ϕ(i). This ompletes the proof of ( ) n of (i). (ii) (2 points) Let σ onsist of inry reltion R. Using (i), show tht there is no CALC query without onstnts whih on input 4
R proues s nswer Suppose there is CALC query ϕ with no onstnts, with the ove property. Let I e the input in the figure. By (i), f(ϕ(i)) = ϕ(i) for every utomorphism f of I. Consier the 1-1 mping f on om(i) efine y the tle f Clerly f(i) = I. However, f(ϕ(i)) ϕ(i), ontrition. 5