0% found this document useful (0 votes)
163 views9 pages

Relational Algebra and Relational Calculus

Tape id format movie_id 0001 DVD 095 0002 DVD 112 0003 VHS 222 0004 DVD 345 0005 VHS 345 0009 VHS 345 Movie (ρid ← movie_id (Tape)) id title 095 Psycho 112 ET 222 Star Wars I 345 Psycho 345 Star Wars I 345 Star Wars I Tape.movie_id = Movie.id 1. The document discusses relational algebra and relational calculus as formal languages for handling relational data through basic operations like projection, selection, join, etc. 2. It provides examples of queries expressed in relational algebra, including finding customer names,

Uploaded by

Dicky Kurniawan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
163 views9 pages

Relational Algebra and Relational Calculus

Tape id format movie_id 0001 DVD 095 0002 DVD 112 0003 VHS 222 0004 DVD 345 0005 VHS 345 0009 VHS 345 Movie (ρid ← movie_id (Tape)) id title 095 Psycho 112 ET 222 Star Wars I 345 Psycho 345 Star Wars I 345 Star Wars I Tape.movie_id = Movie.id 1. The document discusses relational algebra and relational calculus as formal languages for handling relational data through basic operations like projection, selection, join, etc. 2. It provides examples of queries expressed in relational algebra, including finding customer names,

Uploaded by

Dicky Kurniawan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Relational Languages: Example Queries Relational Languages: Relational Algebra & Relational calculus

Queries at Video shop DB


Names of all customers All customers named Anna All movies by George Lucas from 1999 or later All tapes and their corresponding movie All customers who have rented at least one science fiction film 6. All customers whose rented movies all have category suspense 7. Customers that had rented all movies 8. All movies no copy of which are currently on loan 9. Number of tapes for each movie 10. Total receipts of each movie within the last year
4

1. 2. 3. 4. 5.

Relational Algebra Relational Calculus Languages Tuple Calculus Domain Calculus Relational Languages Equivalence

FU-Berlin, DBS I 2006, Hinze / Scholz

Database Design

Miniworld

Relational Algebra: Basics


Procedural query language Consider two relations
R(A1:C1, A2:C2, , Ar:Cr), degree r S(B1:D1, B2:D2, , Bs:Ds), degree s

Functional Requirements

Requirements analysis
Database Requirements

Functional Analysis
High-level transaction specification DBMS-independent DBMS-specific

Conceptual Design
Conceptual schema

5 (+1) Basic Operations Other operations can be derived Schema-compatible relations


= identical schemas (with renaming) same degree of relations [r=s] domains of R(R) and R(S) are pair-wise compatible [ Permutation of indices {1, ..., r}:i, 1 ir: Ci = D(i) ]
5
FU-Berlin, DBS I 2006, Hinze / Scholz

Logical Schema Design


Logical schema in data model of specific DBMS

Application Program Design Transaction Implementation


Application program

FU-Berlin, DBS I 2006, Hinze / Scholz FU-Berlin, DBS I 2006, Hinze / Scholz

Physical Schema Design


Internal schema (for same DBMS)
2

Relational Languages
Data model:
Collection of concepts to describe structure of a DB Basic operations for specifying retrieval and update

Relational Algebra : Basic Operations


Projection

Selection

Formal Languages for handling data


Relational Algebra: procedural language, specifies how to evaluate queries Relation calculus: predicate logic interpretation of data and queries, declarative language, specifies which data to select Both are closed languages: results of queries on relations are relations
3

Union Difference

FU-Berlin, DBS I 2006, Hinze / Scholz

Cartesian Project

A B C I II

(Renaming)

A A B B C C

I II I II I II
6

Relational Algebra : Basic Operations


Projection: <List of attributes>(Relation)
Important concept Reduction of relation schema to selected attributes Reduction of relation tuple to respective attributes Removal of duplicate tuples (set condition!) B (R ) = {t[B] | t R}, B R (R) Example: Names of all customers
Customer
mem_no last_name first_name 001 007 002 011 023 111 .... Mller Katz Maus Hinz Kunz Mller .... Tina Anna Carla Fritz Anna Bert .... address telephone ... ... ... ... ... ... ... .... ... ... ... ... ... ... last_name Mller Katz Maus Hinz Kunz ....

Relational Algebra: Selection predicates


Primitive Predicates
Compare an attribute and a value or two attributes Operands: Attribute names, constants Operators: =, , , , <, >

Boolean row predicates


Combine primitive predicates by AND, OR, NOT and parenthesis Boolean operators: , , Operator preference and brackets as usual
FU-Berlin, DBS I 2006, Hinze / Scholz

FU-Berlin, DBS I 2006, Hinze / Scholz FU-Berlin, DBS I 2006, Hinze / Scholz FU-Berlin, DBS I 2006, Hinze / Scholz

last_name (Customer)

No "second order" predicates


Example: "Rows which have NULL as value of all attributes"
10

Wrong number of customers!


7

Relational Algebra: Basic Operations


Example: Names of all customers Include key attribute into projection:
Customer
mem_no last_name first_name 001 007 002 011 023 111 .... Mller Katz Maus Hinz Kunz Mller .... Tina Anna Carla Fritz Anna Bert .... address telephone ... ... ... ... ... ... ... .... ... ... ... ... ... ... mem_no last_name 001 007 002 011 023 111 .... Mller Katz Maus Hinz Kunz Mller ....

Relational Algebra: Basic Operations


Example: All movies by Lucas from 1999 or later
Movie
id 095 112 345 222 290 100 ... title Psycho ET Star Wars I Psycho Star Wars IV Jaws ... category suspense comedy SciFi suspense ScFi horror ... year 1960 1982 1999 1998 1997 1975 ... director Hitchcock Spielberg Lucas Van Sant Lucas Spielberg ... price length 2.00 1.50 2.00 2.20 2.00 1.50 ... ... ... ... ... ... ... ...

director=Lucas year>=1999 (Customer) mem_no, last_name (Customer)


id 345 ... title Star Wars I ... category SciFi ... year 1999 ... director Lucas ... price length 2.00 ... ... ...
FU-Berlin, DBS I 2006, Hinze / Scholz

11

Relational Algebra: Basic Operations


Selection: <condition>(Relation)
Important concept New relation schema = old relation schema All tuples t R that satisfy condition Condition: qualificator-free Boolean predicate P P (R) = { t | P(t) true, t R} Example: All customers named Anna
Customer
mem_no last_name first_name 001 007 002 011 023 111 .... Mller Katz Maus Hinz Kunz Bla .... Tina Anna Carla Fritz Anna Bert .... address telephone ... ... ... ... ... ... ... .... ... ... ... ... ... ...

Relational Algebra: Basic Operations


Selection properties
P (Q (R) ) = Q (P (R) ) P (P (R) ) = P (R) Q P (R) = Q (P (R) ) Q P (R) = Q (R) P (R) P (R) = R - P (R)

Projection properties
mem_no last_name first_name 007 023 ... Katz Kunz ... Anna Anna ... address telephone ... ... ... ... ... ...

X Y R(R) X(Y(R) ) = X(R) X, Y R(R) X(Y(R) ) = X Y(R) = Y(X(R) )


FU-Berlin, DBS I 2006, Hinze / Scholz

Selection and Projection Property


attr(P) X R(R) X(P (R) ) = P (X(R) ) where attr(P) denotes the set of attributes used in P
12

first_name=Anna (Customer)
9

Relational Algebra: Basic Operations


Union
R and S are schema-compatible R S = {t | t R t S}

Relational Algebra: Additional Operations


Intersection R S
R and S are schema-compatible R S = R- (R-S)

Division R S Difference
R and S are schema-compatible R - S = {t | t R t S} = R \ S
R
FU-Berlin, DBS I 2006, Hinze / Scholz FU-Berlin, DBS I 2006, Hinze / Scholz FU-Berlin, DBS I 2006, Hinze / Scholz

For queries with all-quantifier R(A1 , ..., Ar, B1, , Bk), S( B1, ..., Bk ) T= R S, T(A1 , ..., Ar) R S = R-S (R) - R-S((R-S (R) x S) - R)
R A a b a b a b a B c c d e a d c S

A a b a b a b a

B c c d e a d e

A a b a

B c c a

A a b a b

B e d d e

B d e

A b

13

16

Relational Algebra: Basic Operations


Cartesian Product T= R x S
Important concept T(A1:C1, A2:C2, ..., Ar:Cr, B1:D1, B2:D2, ..., Bs:Ds ) R x S = {tu | t R, u S} Tupel concatenation of t = (v1, ..., vr), u = (w1, ..., ws) tu := (v1, ..., vr, w1, ..., ws ), degree r + s

Relational Algebra: Additional Operations


Theta- Join: R
S

Important concept

Consider R(A1 , ..., Ar ), S( B1, ..., Bs ) T= R S = {(A1, ...Ar, B1,...Bs) | (A1,...Ar,B1,...Bs ) true}

Boolean predicate formed of primitive predicates a op b, a R (R), b R (S), op {=, , , , <, >} R
FU-Berlin, DBS I 2006, Hinze / Scholz

Rename:
Changes Schema, no new relation Necessary for using relation or attribute more than once in one query Rename relation R in S S(R) Rename attribute B in A A B(R)
14

S == (R x S)

Equijoin: theta-join form R

R.Ai=S.Bj

17

Relational Algebra: Basic Operations


Example: All Tapes and their corresponding movie
Tape
id 0001 0002 0003 0004 0005 0009 .... format DVD DVD VHS DVD VHS VHS .... movie_id 095 112 222 345 345 345 .... id 095 112 345 222 290 100 ... title Psycho ET Star Wars I Psycho Star Wars IV Jaws ...

Relational Algebra: Additional Operations


Example: All Tapes and their corresponding movie
director Hitchcock Spielberg Lucas Van Sant Lucas Spielberg ... price length 2.00 1.50 2.00 2.20 2.00 1.50 ... ... ... ... ... ... ... ...

Movie
category suspense comedy SciFi suspense ScFi horror ... year 1960 1982 1999 1998 1997 1975 ...

Tape

Movie

Tape.movie_id=Movie.id

tape. id 0001 0002 0003 0004 0005 0009 ....

tape. format DVD DVD VHS DVD VHS VHS ....

tape. movie_id 095 112 222 345 345 345 ....

movie. id 095 112 222 345 345 345 ...

movie. title Psycho ET Psycho Star Wars I Star Wars I Star Wars I ...

movie. category suspense comedy suspense ScFi ScFi SciFi ...

movie. year 1960 1982 1998 1999 1999 1999 ...

movie. director ... ... ... ... ... ... ...

movie. price 2.00 1.50 2.20 2.00 2.00 2.00 ...

movie. length ... ... ... ... ... ... ...

Tape.movie_id=Movie.id(Tape x Movie) = t.movie_id=m.id(t(Tape) x m(Movie))


t.id 0001 0002 0003 0004 0005 0009 .... t.format DVD DVD VHS DVD VHS VHS .... t.movie_id 095 112 222 345 345 345 .... m.id 095 112 222 345 345 345 ... m.title Psycho ET Psycho Star Wars I Star Wars I Star Wars I ... m.category suspense comedy suspense ScFi ScFi SciFi ... m.year m.director m.price m.length 1960 1982 1998 1999 1999 1999 ... ... ... ... ... ... ... ... 2.00 1.50 2.20 2.00 2.00 2.00 ... ... ... ... ... ... ... ...
FU-Berlin, DBS I 2006, Hinze / Scholz FU-Berlin, DBS I 2006, Hinze / Scholz

15

18

Relational Algebra: Additional Operations


Natural join: R S
Important concept

Relational Algebra: Additional Operations


Left outer join: (NULL,NULL, s) rows for each s S which does have join row in S according to join predicate P
R S A2 c d A1 a b B1 d e B2 f g

Consider R(A1 , ..., Ar, C1, , Ck), S( B1, ..., Bs, C1, , Ck ) dom( Cl) equal in R and S, 1 lk 1 ir 1 js : Ai Bj

R.A2=S.B1

A1 a b

A2 c d

B1 d

B2 f

R R
FU-Berlin, DBS I 2006, Hinze / Scholz

S = A1,..., Ar,R.C1,...,R.Ck,B1,..., Bs (R.C1=S.C1 R. C k =S. C k (R x S)) S has degree r + s + k


A1 a b
FU-Berlin, DBS I 2006, Hinze / Scholz

Full outer join: tupels of both R and S are included


R A2 c d B1 S B2 f g

R.A2=S.B1

d e

A1 a b -

A2 c d -

B1 d e

B2 f g

Left and right outer join not commutative


22

19

Relational Algebra: Additional Operations


Example: All Tapes and their corresponding movie Tape (movie_id id (Movie))

Relational Algebra: Additional Operations


Semi-Joins
R S = R (R) (R S) = S R All tuples in R that have potential join partners in S (semi-join of R with S)
R S A2 c d A1 a b B1 d e B2 f g

id 0001 0002 0003 0004 0005 0009 ....


FU-Berlin, DBS I 2006, Hinze / Scholz

format DVD DVD VHS DVD VHS VHS ....

movie_id 095 112 222 345 345 345 ....

title Psycho ET Psycho Star Wars I Star Wars I Star Wars I ...

category suspense comedy suspense ScFi ScFi SciFi ...

year 1960 1982 1998 1999 1999 1999 ...

director ... ... ... ... ... ... ...

price 2.00 1.50 2.20 2.00 2.00 2.00 ...

length ... ... ... ... ... ... ...


FU-Berlin, DBS I 2006, Hinze / Scholz

R.A2=S.B1

A1 b

A2 d

R A1 a b A2 c d

R.A2=S.B1

B1 d e

B2 f g

B1 d

B2 f

Semi-join not commutative


23

20

Relational Algebra: Additional Operations


Inner joins (Natural join, Theta-join)
Non-matching tuples are lost Associative and commutative

Relational Algebra: Examples


All Customers who have rented at least one science fiction film mem_no, last_name (customer
member_no=member

rental

tape_id=id

tape
movie_id=movie.id

(category=SciFi (movie)))

Outer join R

S
All Customers whose rented movies all have category suspense customer (customer
member=mem_no

Non-matching tuples included Right outer join: (r, NULL,.,NULL) - rows for each r R which does have join row in S according to join predicate P
FU-Berlin, DBS I 2006, Hinze / Scholz FU-Berlin, DBS I 2006, Hinze / Scholz

( rental

Tape_id=id

R A1 a b A2 c d

( tape

R.A2=S.B1

B1 d e

B2 f g

A1 b -

A2 d -

B1 d e

B2 f g

movie_id=movie.id

(category suspense (movie)))))

21

24

Relational Algebra: Examples


Customers that had rented all movies (mem_no, movie.id (customer tape
movie_id=movie.id

Relational Algebra: Operator Tree


Example: All movies no copy of which are currently on loan

member_no=member

tape_id=id

movie))

movie.id (movie)

movie movie

movie_id=movie.id

movie movie

until=NULL All movies no copy of which are currently on loan


tape_id=id
FU-Berlin, DBS I 2006, Hinze / Scholz FU-Berlin, DBS I 2006, Hinze / Scholz FU-Berlin, DBS I 2006, Hinze / Scholz FU-Berlin, DBS I 2006, Hinze / Scholz

movie - ( movie

movie_id=movie.id

(until=NULL (rental)

tape_id=id

tape))

rental

tape

25

less tuples to evaluate

Relational Algebra: Examples

Relational Algebra: Operations


Basic set of RA operations {, , x , - , }
RA expressions using different operators of RA can be expressed by the above operators only

Number of tapes for each movie No count!

Missing operations in RA:


Transitive closure

Example: Lecture(lnr, ) predecessor successor Require(preLNR, succLNR) require Total receipts of each movie within the last year No count!
FU-Berlin, DBS I 2006, Hinze / Scholz

Query: All lectures required for lecture XYZ Predicates on tables, e.g. arithmetic functions: SUM, AVERAGE, MAXIMUM, MINIMUM, or COUNT, grouping

26

Relational Algebra: Operator Tree


Important concept

Relational Algebra: Summary


Relational Algebra:
Formal language for handling data in relational model Procedural language, how to retrieve data No practical relevance for querying DB Formal basis for query optimization

Data structure representing RA expression More clearly structured Evaluation by recursive evaluation of the tree Basis for algebraic optimization
Implementation of Algebraic Optimization by transformation of operator tree Interchange of operations according to the laws of RA e.g., (R P(S)) faster than (P (R S)) Reduction of number of tuples to evaluate No change of time complexity in general

Important terms & concepts


Union R S Difference R S Selection <predicate>(R) Projection <attribute list>(R) Cartesian Product R x S Joins R <predicate> S Operator tree
30

27

movie_id=movie.id tape_id=id

FU-Berlin, DBS I 2006, Hinze / Scholz

rental

rental

until=NULL tape

28

lnr Lecture

title

29

Relational Calculus: Languages


Non-procedural, declarative query language Two types of languages
Tuple calculus Variables in expressions represent a row of a relation (tuple variable) Example: {c.last_name, c.first_name | c Costumer}

Relational Calculus: Atoms and Formulae


1. Every atom is a formula. 2. If F1 is a formula so are F1 and (F1) 3. If F1, F2 are formulae so are F1F2, F1F2, F1F2 4. If F with t free is formula so are t (F), t (F)

Domain calculus Variables represent domain values of attributes of a relation of the DB (domain variables) Example: {[l,f] | m,a,t( [m,l,f,a,t] Customer )}
31

FU-Berlin, DBS I 2006, Hinze / Scholz FU-Berlin, DBS I 2006, Hinze / Scholz FU-Berlin, DBS I 2006, Hinze / Scholz

FU-Berlin, DBS I 2006, Hinze / Scholz

5. Nothing else is a formula

34

Tuple Calculus: Introduction


Queries in tuple calculus
Form: {t | F(t)} with tupel variable t F is formula t is the only free variable of formula F Answer of query: set of tuples t from DB with F(t) = TRUE Important concept

Tuple Calculus: Examples


Formula properties
t R(F(t)) = ( t R(F(t)) t R(F(t)) = ( t R(F(t))

Tuple Calculus Examples:


Names of all customers {c.last_name, c.first_name | c Costumer} All customers named Anna {c | c Costumer c. first_name = Anna}
FU-Berlin, DBS I 2006, Hinze / Scholz

Example:
All customers named Anna {c | c Costumer c. first_name = Anna}

Free variable: no existence- or all-quantor (, ) Formulae made of atoms Atoms evaluate to TRUE or FALSE
32

All movies by George Lucas from 1999 or later {m.id, m.title | m Movie m.director=Lucas m.year>=1999 }
35

Tuple Calculus: Atom Forms


t R
with relation name R and tuple variable t Alternative notation: R(t) Example: c Costumer

Tuple Calculus: Examples


All Tapes and their corresponding movie {t.id, m.title | t Tape mMovie t. movie_id = m.id}

(t.A operator s.B)


t,s tuple variables A,B attribute names of of relations on which t, s ranges operator {=, , , , <, >} Example: c.member_no=r.member
FU-Berlin, DBS I 2006, Hinze / Scholz

All Customers who have rented at least one science fiction film {c | c Costumer r Rental( c.member_no=r.member t Tape(r.tape_id=t.id m Movie(t.movie_id=m.id m.category=SciFi )))}

(t.A operator c)
c constant value Example: c. first_name = Anna
33

36

Tuple Calculus: Examples


All Customers whose rented movies all have category suspense {c | c Costumer rRental( c.member_no=r.member t Tape(r.tape_id=t.id m Movie(t.movie_id=m.id m.category=suspense )))}

Tuple Calculus vs Relational Algebra


Selection
<predicate>(R) equivalent to { r | r R <predicate> }

Projection, cross product


R.a, S.b (R X S) equivalent to { r.a, s.b | r R s S }

Join
R
P

S equivalent to { r | r R s S P}

{c | c Costumer rRental( c.member_no=r.member t Tape(r.tape_id=t.id m Movie(t.movie_id = m.id m.category suspense )))}


37

Union
R S equivalent to { t | t R t S }

FU-Berlin, DBS I 2006, Hinze / Scholz FU-Berlin, DBS I 2006, Hinze / Scholz FU-Berlin, DBS I 2006, Hinze / Scholz

FU-Berlin, DBS I 2006, Hinze / Scholz

Difference
R S equivalent to {t | t R t S }
40

Tuple Calculus: Examples


Customers that had rented all movies {c | c Customer m Movie( t Tape(t.movie_id=m.id rRental(r.tape_id=t.id c.member_no=r.member ))} All movies no copy of which are currently on loan {m | m Movie t Tape(t.movie_id=m.id r Rental(r.tape_id=t.id r.until=NULL))}
38
FU-Berlin, DBS I 2006, Hinze / Scholz

Tuple Calculus: Safe expression


Solution set for { t | t R} ?
All tuples NOT belonging to R, infinite set? Important concept

Formula domain:
all attribute data of referenced relations in DB, and constants of the formula

Safe expression: tuple calculus expression is safe if result is subset of domain


Idea: safe if all free tuple variables restricted in F Example: {x | x T x R } safe
41

Tuple Calculus: Examples

Tuple Calculus: Practical Use


Tuple calculus basis for DB language QUEL
In 70s used in Ingres (University of Berkeley) Commercial INGRES now SQL

Number of tapes for each movie No count! Total receipts of each movie within the last year No count!

Examples:
All customers named Anna RANGE of c is Customer RETRIEVE (c.mem_no, c.last_name, c.first_name) WHERE c. first_name = Anna All movies by George Lucas from 1999 or later RANGE of m is Movie RETRIEVE (m.title) WHERE m.director=Lucas AND year>=1999
42
FU-Berlin, DBS I 2006, Hinze / Scholz

39

Domain Calculus: Introduction


Queries in domain calculus
Form: {[a,b,c] | F([a,b,c])} with a,b,c domain variables F is formula a, b, c are the only free variables of formula F Answer of query: set of tuples t from DB with F(t) = TRUE

Domain Calculus: Examples


All Customers who have rented at least one science fiction film {[m,f,l] | a,t ([m,f,l,a,t] Costumer rti,rm,rf,ru ( [rti,rm,rf,ru] Rental m=rm ti,tf,tmi ( [ti,tf,tmi] Tape rti=ti mi,mt,mc,my,md,mp,ml ([mi,mt,mc,my,md,mp,ml] Movie tmi=mi mc=SciFi ))))} All Customers whose rented movies all have category suspense {[m,f,l] | a,t ([m,f,l,a,t] Costumer rti,rm,rf,ru ( [rti,rm,rf,ru] Rental m=rm ti,tf,tmi ( [ti,tf,tmi] Tape rti=ti mi,mt,mc,my,md,mp,ml ([mi,mt,mc,my,md,mp,ml] Movie tmi=mi mc=suspense ))))}

Example:
All customers named Anna {[m,f,l] | a,t ([m,f,l,a,t] Costumer f= Anna)}
FU-Berlin, DBS I 2006, Hinze / Scholz FU-Berlin, DBS I 2006, Hinze / Scholz FU-Berlin, DBS I 2006, Hinze / Scholz FU-Berlin, DBS I 2006, Hinze / Scholz

Domain variables represent sets of possible attribute values (domains) Formulae made of atoms Atoms evaluate to TRUE or FALSE

43

46

Domain Calculus: Atom Forms


[a1,,an] R
with relation R of grade n Domain variables a1,,an according to order in schema of R Example: [m,f,l,a,t] Costumer

Domain Calculus: Examples


Customers that had rented all movies {[m,f,l] | a,t ([m,f,l,a,t] Costumer mi,mt,mc,my,md,mp,ml ([mi,mt,mc,my,md,mp,ml] Movie ti,tf,tmi ( [ti,tf,tmi] Tape tmi=mi rti,rm,rf,ru ( [rti,rm,rf,ru] Rental m=rm rti=ti )))} All movies no copy of which are currently on loan
FU-Berlin, DBS I 2006, Hinze / Scholz

(ai operator aj)


operator {=, , , , <, >} ai, aj domain variables Example: mn = r

(ai operator c)
c constant value Example: f= Anna
44

{[i,t,d] | c,y,p,l ( [i,t,c,y,d,p,l] Movie ti,tf,tmi ( [ti,tf,tmi] Tape tmi=i rti,rm,rf,ru ( [rti,rm,rf,ru] Rental rti=ti )))}
47

Domain Calculus: Examples


Names of all customers {[l,f] | m,a,t ([m,f,l,a,t] Costumer)} All customers named Anna {[m,f,l] | a,t ([m,f,l,a,t] Costumer f= Anna)} All movies by George Lucas from 1999 or later {[i,t] | c,y,d,p,l ([i,t,c,y,d,p,l] Movie d=Lucas y>=1999) } All Tapes and their corresponding movie {[ti,t] | tf,mi ([ti,tf,mi] Tape i,c,y,d,p,l ([i,t,c,y,d,p,l] Movie mi = i))}
FU-Berlin, DBS I 2006, Hinze / Scholz

Domain Calculus: Examples

Number of tapes for each movie No count! Total receipts of each movie within the last year No count!

45

48

Domain Calculus
Safe expression {[a1,,an] | F(a1,,an)}:
1. Constants ci(1in) in domain of F if [c1,,cn] in solution 2. For all a1(F(a1)) F true only for elements of domain of F 3. For all a1(F(a1)) F true only iff true for all elements of domain of F 2.+3. necessary since domain variables not bound to relations

Relational Languages: Conclusion


Relational completeness:
Important concept query language for relational model is complete if at least as expressive as relational algebra base line for DB query languages: every query language should be as expressive as relational algebra

Equivalent expressivenesses:
Relational Algebra Tuple calculus restricted to safe expressions Domain calculus restricted to safe expressions
FU-Berlin, DBS I 2006, Hinze / Scholz

Formula domain:
all attribute data of referenced relations in DB, and constants of the formula
FU-Berlin, DBS I 2006, Hinze / Scholz FU-Berlin, DBS I 2006, Hinze / Scholz FU-Berlin, DBS I 2006, Hinze / Scholz

Proof using induction: RA expressions TC expressions DC expressions RA expressions


52

49

Domain Calculus: Practical Use


Domain calculus basis for DB language QBE
QBE = query by example One of the first graphical query languages Interface option for DB2 (IBM) templates of relations on screen Users fill in constants (), examples (_), output (P. ) Example: All movies by George Lucas from 1999 or later Movie id P.1 title category year director price P.bla 1999 Lucas length

each column in template as implicit domain variable {[i,t] | c,y,d,p,l ([i,t,c,y,d,p,l] Movie d=Lucas y>=1999) }
50

Relational Calculus: Summary


Relational Calculus:
Formal languages for handling data in relational model Declarative language, which data to retrieve Basis for QUEL, QBE, SQL

Important terms & concepts


Tuple Calculus Domain Calculus Bound and free variables Safe expression Relational Completeness
51

You might also like