Relational Algebra and Relational Calculus
Relational Algebra and Relational Calculus
1. 2. 3. 4. 5.
Relational Algebra Relational Calculus Languages Tuple Calculus Domain Calculus Relational Languages Equivalence
Database Design
Miniworld
Functional Requirements
Requirements analysis
Database Requirements
Functional Analysis
High-level transaction specification DBMS-independent DBMS-specific
Conceptual Design
Conceptual schema
FU-Berlin, DBS I 2006, Hinze / Scholz FU-Berlin, DBS I 2006, Hinze / Scholz
Relational Languages
Data model:
Collection of concepts to describe structure of a DB Basic operations for specifying retrieval and update
Selection
Union Difference
Cartesian Project
A B C I II
(Renaming)
A A B B C C
I II I II I II
6
FU-Berlin, DBS I 2006, Hinze / Scholz FU-Berlin, DBS I 2006, Hinze / Scholz FU-Berlin, DBS I 2006, Hinze / Scholz
last_name (Customer)
11
Projection properties
mem_no last_name first_name 007 023 ... Katz Kunz ... Anna Anna ... address telephone ... ... ... ... ... ...
first_name=Anna (Customer)
9
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
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)
R.Ai=S.Bj
17
Movie
category suspense comedy SciFi suspense ScFi horror ... year 1960 1982 1999 1998 1997 1975 ...
Tape
Movie
Tape.movie_id=Movie.id
movie. title Psycho ET Psycho Star Wars I Star Wars I Star Wars I ...
15
18
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
R.A2=S.B1
d e
A1 a b -
A2 c d -
B1 d e
B2 f g
19
title Psycho ET Psycho Star Wars I Star Wars I Star Wars I ...
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
20
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
21
24
member_no=member
tape_id=id
movie))
movie.id (movie)
movie movie
movie_id=movie.id
movie movie
movie - ( movie
movie_id=movie.id
(until=NULL (rental)
tape_id=id
tape))
rental
tape
25
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
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
27
movie_id=movie.id tape_id=id
rental
rental
until=NULL tape
28
lnr Lecture
title
29
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
34
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
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
Join
R
P
S equivalent to { r | r R s S P}
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
Difference
R S equivalent to {t | t R t S }
40
Formula domain:
all attribute data of referenced relations in DB, and constants of the formula
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
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
(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
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
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
49
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