revision for final exam prof. sin-min lee department of computer science

73
Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Upload: elvin-stafford

Post on 02-Jan-2016

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Revision for Final Exam

Prof. Sin-Min Lee

Department of Computer Science

Page 2: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Relational Query Languages

• Languages for describing queries on a relational database

• Structured Query LanguageStructured Query Language (SQL)– Predominant application-level query language– Declarative

• Relational AlgebraRelational Algebra– Intermediate language used within DBMS– Procedural

Page 3: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

What is an Algebra?

• A language based on operators and a domain of values

• Operators map values taken from the domain into other domain values

• Hence, an expression involving operators and arguments produces a value in the domain

• When the domain is a set of all relations (and the operators are as described later), we get the relational algebrarelational algebra

• We refer to the expression as a queryquery and the value produced as the queryquery resultresult

Page 4: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Relational Algebra

• Domain: set of relations• Basic operators: selectselect, projectproject, unionunion, setset

differencedifference, CartesianCartesian productproduct• Derived operators: set intersectionset intersection, divisiondivision, joinjoin• Procedural: Relational expression specifies query

by describing an algorithm (the sequence in which operators are applied) for determining the result of an expression

Page 5: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

The Role of Relational Algebra in a DBMS

Page 6: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Select Operator

• Produce table containing subset of rows of argument table satisfying condition

condition (relation)

• Example:

Person Person Hobby=‘stamps’(PersonPerson)

1123 John 123 Main stamps1123 John 123 Main coins5556 Mary 7 Lake Dr hiking9876 Bart 5 Pine St stamps

1123 John 123 Main stamps9876 Bart 5 Pine St stamps

Id Name Address HobbyId Name Address Hobby

Page 7: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Selection Condition

• Operators: <, , , >, =, • Simple selection condition:

– <attribute> operator <constant>– <attribute> operator <attribute>

• <condition> AND <condition>

• <condition> OR <condition>• NOT <condition>

Page 8: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Selection Condition - Examples

Id>3000 OR Hobby=‘hiking’ (PersonPerson)

Id>3000 AND Id <3999 (PersonPerson)

NOT(Hobby=‘hiking’) (PersonPerson)

Hobby‘hiking’ (PersonPerson)

Page 9: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Project Operator• Produces table containing subset of columns of the

table taken as argument attribute list(relation)

• Example: PersonPerson Name,Hobby(PersonPerson)

1123 John 123 Main stamps1123 John 123 Main coins5556 Mary 7 Lake Dr hiking9876 Bart 5 Pine St stamps

John stampsJohn coinsMary hikingBart stamps

Id Name Address Hobby Name Hobby

Page 10: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Project Operator

1123 John 123 Main stamps1123 John 123 Main coins5556 Mary 7 Lake Dr hiking9876 Bart 5 Pine St stamps

John 123 MainMary 7 Lake DrBart 5 Pine St

Result is a table (no duplicates); can have fewer tuplesthan the original

Id Name Address Hobby Name Address

• Example: PersonPerson Name,Address(PersonPerson)

Page 11: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Expressions

1123 John 123 Main stamps1123 John 123 Main coins5556 Mary 7 Lake Dr hiking9876 Bart 5 Pine St stamps

1123 John9876 Bart

Id Name Address Hobby Id Name

PersonPerson

ResultResult

Id, Name ( Hobby=’stamps’ OR Hobby=’coins’ (PersonPerson) )

Page 12: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Set Operators

• Relation is a set of tuples, so set operations should apply: , , (set difference)

• Result of combining two relations with a set operator is a relation; hence all its elements must be tuples having the same structure

• Hence, scope of set operations limited to union compatible relationsunion compatible relations

Page 13: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Union Compatible Relations

• Two relations are union compatibleunion compatible if– Both have same number of columns– Names of attributes are the same in both– Attributes with the same name in both relations

have the same domain

• Union compatible relations can be combined using unionunion, intersectionintersection, and setset differencedifference

Page 14: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

ExampleTables: PersonPerson (SSN, Name, Address, Hobby) ProfessorProfessor (Id, Name, Office, Phone)are not union compatible. But Name (PersonPerson) and Name (ProfessorProfessor)

are union compatible so

Name (PersonPerson) - Name (ProfessorProfessor)

makes sense.

Page 15: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Cartesian Product• If RR and SS are two relations, RR SS is the set of all

concatenated tuples <x,y>, where x is a tuple in RR and y is a tuple in S S (but see naming problem next)(but see naming problem next)

• RR SS is expensive to compute:– Factor of two in the size of each row– Quadratic in the number of rows

A B C D A B C D x1 x2 y1 y2 x1 x2 y1 y2 x3 x4 y3 y4 x1 x2 y3 y4 x3 x4 y1 y2 RR SS x3 x4 y3 y4 RR SS

Page 16: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Renaming in Cartesian ProductResult of expression evaluation is a relation.

Attributes of relation must have distinct names.

So what do we do if they don’t?E.g., suppose R(A,B) and S(A,C)

and we wish to compute RR SS .

One solution is to rename the attributes of the answer:

RR S( R.A, R.B, S.A, S.C)S( R.A, R.B, S.A, S.C)

Although onlyAlthough only A A needs to be renamed, it is“cleaner” to needs to be renamed, it is“cleaner” to rename them all.rename them all.

Page 17: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Renaming Operator• Previous solution is used whenever possible but it

won’t work when R is the same as S.

• Renaming operator resolves this. It allows to assign any desired names, say A1, A2,… An , to the attributes of the n column relation produced by expression expr with the syntax

expr [A1, A2, … An]

Page 18: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Example

This is a relation with 4 attributes: StudId, CrsCode1, ProfId, CrsCode2

TranscriptTranscript (StudId, CrsCode, Semester, Grade)

TeachingTeaching (ProfId, CrsCode, Semester)

  StudId, CrsCode (TranscriptTranscript)[StudId, CrsCode1]

ProfId, CrsCode(TeachingTeaching) [ProfId, CrsCode2]

Page 19: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Derived Operation: Join

A (generalgeneral or thetatheta) join join of R and S is the expression R join-condition S

where join-condition is a conjunction of terms: Ai oper Bi

in which Ai is an attribute of R; Bi is an attribute of S; and oper is one of =, <, >, , .

The meaning is:

join-condition´ (R S)

where join-condition and join-condition´ are the same, except for possible renamings of attributes caused by the Cartesian product.

Page 20: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Theta Join – Example

Employee(Employee(Name,Id,MngrId,SalaryName,Id,MngrId,Salary) Manager(Manager(Name,Id,SalaryName,Id,Salary)

Output the names of all employees that earnmore than their managers.EmployeeEmployee.Name (EmployeeEmployee MngrId=Id AND Salary>Salary ManagerManager)

The join yields a table with attributes: EmployeeEmployee.Name, EmployeeEmployee.Id, EmployeeEmployee.Salary, Employee.MngrId

ManagerManager.Name, ManagerManager.Id, ManagerManager.Salary

Page 21: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Relational Algebra

• Relational algebra operations operate on relations and produce relations (“closure”)f: Relation -> Relation f: Relation x Relation

-> Relation• Six basic operations:

– Projection (R)– Selection (R)– Union R1 [ R2

– Difference R1 – R2

– Product R1 £ R2

– (Rename) (R)

Page 22: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Equijoin Join - Example

Name,CrsCode(StudentStudent Id=StudId Grade=‘A’ (TranscriptTranscript))

Id Name Addr Status111 John ….. …..222 Mary ….. …..333 Bill ….. …..444 Joe ….. …..

StudId CrsCode Sem Grade 111 CSE305 S00 B 222 CSE306 S99 A 333 CSE304 F99 A

Mary CSE306Bill CSE304

The equijoin is used veryfrequently since it combinesrelated data in different relations.

StudentStudent TranscriptTranscript

EquijoinEquijoin: Join condition is a conjunction of equalities.

Page 23: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Natural Join• Special case of equijoin + a special projection

– join condition equates all and only those attributes with the same name (condition doesn’t have to be explicitly stated)

– duplicate columns eliminated (projected out) from the result

TranscriptTranscript (StudId, CrsCode, Sem, Grade)Teaching (Teaching (ProfId, CrsCode, Sem)

TranscriptTranscript TeachingTeaching = StudId, Transcript.CrsCode, Transcript.Sem, Grade, ProfId

( TranscriptTranscript CrsCode=CrsCode AND Sem=Sem Sem Teaching Teaching ) [StudId, CrsCode, Sem, Grade, ProfId ]

Page 24: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Natural Join (cont’d)

• More generally:RR SS = attr-list (join-cond (RR × SS) )

where attr-list = attributes (RR) attributes (SS)(duplicates are eliminated) and join-cond has the form: A1 = A1 AND … AND An = An

where {A1 … An} = attributes(RR) attributes(SS)

Page 25: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Natural Join Example

• List all Ids of students who took at least two different courses:

StudId ( CrsCode CrsCode2 ( TranscriptTranscript

TranscriptTranscript [StudId, CrsCode2, Sem2, Grade2] )) We don’t want to join on CrsCode, Sem, and Grade attributes, hence renaming!

Page 26: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Example Data Instance

sid name

1 Jill

2 Qun

3 Nitin

4 Marty

fid name

1 Ives

2 Saul

8 Roth

sid exp-grade cid

1 A 550-0103

1 A 700-1003

3 A 700-1003

3 C 500-0103

4 C 500-0103

cid subj sem

550-0103 DB F03

700-1003 AI S03

501-0103 Arch F03

fid cid

1 550-0103

2 700-1003

8 501-0103

STUDENT Takes COURSE

PROFESSOR Teaches

Page 27: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Natural Join and IntersectionNatural join: special case of join where is implicit – attributes with same name must be equal:

STUDENT Takes ⋈ ´ STUDENT ⋈STUDENT.sid = Takes.sid Takes

Intersection: as with set operations, derivable from difference

A-B B-A

A B

A B

Page 28: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Division

• A somewhat messy operation that can be expressed in terms of the operations we have already defined

• Used to express queries such as “The fid's of faculty who have taught all subjects”

• Paraphrased: “The fid’s of professors for which there does not exist a subject that they haven’t taught”

Page 29: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Division Using Our Existing Operators

• All possible teaching assignments: Allpairs:

• NotTaught, all (fid,subj) pairs for which professor fid has not taught subj:

• Answer is all faculty not in NotTaught:

fid,subj (PROFESSOR £ subj(COURSE))

Allpairs - fid,subj(Teaches COURSE)⋈fid(PROFESSOR) - fid(NotTaught)

´ fid(PROFESSOR) - fid(fid,subj (PROFESSOR £ subj(COURSE)) -

fid,subj(Teaches COURSE))⋈

Page 30: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Division: R1 R2

• Requirement: schema(R1) ¾ schema(R2)• Result schema: schema(R1) – schema(R2)• “Professors who have taught all courses”:

• What about “Courses that have been taught by all faculty”?

fid (fid,subj(Teaches ⋈ COURSE) subj(COURSE))

Page 31: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Division

• Goal: Produce the tuples in one relation, r, that match all tuples in another relation, s– rr (A1, …An, B1, …Bm)– ss (B1 …Bm)– rr/ss, with attributes A1, …An, is the set of all

tuples <a> such that for every tuple <b> in ss, <a,b> is in rr

• Can be expressed in terms of projection, set difference, and cross-product

Page 32: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Division (cont’d)

Page 33: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Division - Example• List the Ids of students who have passed all

courses that were taught in spring 2000• Numerator:

– StudId and CrsCode for every course passed by every student:

StudId, CrsCode (Grade ‘F’ (TranscriptTranscript) )

• Denominator:– CrsCode of all courses taught in spring 2000

CrsCode (Semester=‘S2000’ (TeachingTeaching) )

• Result is numerator/denominator

Page 34: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Relational Calculus

• Important features:– Declarative formal query languages for relational mode

l

– Based on the branch mathematical logic known as predicate calculus

– Two types of RC:• 1) tuple relational calculus

• 2) domain relational calculus

– A single statement can be used to perform a query

Page 35: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Tuple Relational Calculus

• based on specifying a number of tuple variables

• a tuple variable refers to any tuple

Page 36: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Generic Form

• {t | COND (t)} – where– t is a tuple variable and – COND(t) is Boolean expression involving t

Page 37: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Simple example 1

• To find all employees whose salary is greater than $50,000– {t| EMPLOYEE(t) and t.Salary>5000}

• where

• EMPLOYEE(t) specifies the range of tuple variable t

– The above operation selects all the attributes

Page 38: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Simple example 2

• To find only the names of employees whose salary is greater than $50,000– {t.FNAME, t.NAME| EMPLOYEE(t) and

t.Salary>5000}

• The above is equivalent to• SELECT T.FNAME, T.LNAME• FROM EMPLOYEE T• WHERE T.SALARY > 5000

Page 39: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Elements of a tuple calculus

• In general, we need to specify the following in a tuple calculus expression:– Range Relation (I.e, R(t)) = FROM– Selected combination= WHERE– Requested attributes= SELECT

Page 40: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

More Example:Q0

• Retrieve the birthrate and address of the employee(s) whose name is ‘John B. Smith’

• {t.BDATE, t.ADDRESS| EMPLOYEE(t) AND t.FNAME=‘John’ AND t.MINIT=‘B” AND t.LNAME=‘Smith}

Page 41: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Formal Specification of tuple Relational Calculus

• A general format:• {t1.A1, t2.A2,…,tn.An |COND ( t1 ,t2 ,…, tn, tn+1,

tn+2,…,tn+m)}– where– t1,…,tn+m are tuple var

– Ai : attributeR(ti)– COND (formula)

• Where COND corresponds to statement about the world, which can be True or False

Page 42: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Elements of formula

• A formula is made of Predicate Calculus atoms:– an atom of the from R(ti)– ti.A op tj.B op{=, <,>,..}– F1 And F2 where F1 and F2 are formulas– F1 OR F2– Not (F1)– F’=(t) (F) or F’= (t) (F)

Y friends (Y, John) X likes(X, ICE_CREAM)

Page 43: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Example Queries Using the Existential Quantifier

• Retrieve the name and address of all employees who work for the ‘ Research ’ department

• {t.FNAME, t.LNAME, t.ADDRESS| EMPLOYEE(t) AND ( d) (DEPARTMENT (d) AND d.DNAME=‘Research’ AND d.DNUMBER=t.DNO)}

Page 44: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

More Example

• For every project located in ‘Stafford’, retrieve the project number, the controlling department number, and the last name, birthrate, and address of the manger of that department.

Page 45: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Cont.

• {p.PNUMBER,p.DNUM,m.LNAME,m.BDATE, m.ADDRESS|PROJECT(p) and EMPLOYEE(M) and P.PLOCATION=‘Stafford’ and ( d) (DEPARTMENT(D) AND P.DNUM=d.DNUMBER and d.MGRSSN=m.SSN))}

Page 46: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Safe Expressions

• A safe expression R.C:– An expression that is guaranteed to generate a fi

nite number of rows (tuples)

• Example:– {t | not EMPLOYESS(t))} results values not bei

ng in its domain (I.e., EMPLOYEE)

Page 47: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Domain Relational Calculus (DRC)

• Another type of formal predicate calculus-based language

• QBE is based on DRC

• The language shares a lot of similarities with the tuple calculus

Page 48: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

DRC

• The only difference is the type of variables:– variables range over singles values from domai

ns of attributes

• An expression of DRC is:– {x1, x2,…,xn|COND(x1,x2,…,xn, xn+2,…,xn+m)}

• where x1,x2,…,xn+m are domain var range over attrib

uters

• COND is a condition (or formula)

Page 49: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Examples

• Retrieve the birthdates and address of the employee whose name is ‘John B. Smith’

• {uv| (q)(r)(s) (EMPLOYEE(qrstuvwxyz) and q=‘John’ and r=‘B’ and s=‘Smith’

Page 50: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Alternative notation

• Ssign the constants ‘John’, ‘B’, and ‘Smith’ directly

• {uv|EMPLOYEE (‘John’, ’B’, ’Smith’ ,t ,u ,v ,x ,y ,z)}

Page 51: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

More example

• Retrieve the name and address of all employees who work for the ‘Reseach’ department

• {qsv | ( z) EMPLOYEE(qrstuvwxyz) and ( l) ( m) (DEPARTMENT (lmno) and l=‘Research’ and m=z))}

Page 52: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

More example

• List the names of managers who have at least on e dependent

• {sq| ( t) EMPLOYEE(qrstuvwxyz) and (( j)( DEPARTMENT (hijk) and (( l) | (DEPENTENT (lmnop) and t=j and t=l))))}

Page 53: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

QBE

• Query-By-Example– Supports graphical query language based on

DRC– Implemented in commercial db such as

Access/Paradox– Query can be specified by filling in templates

of relations– Fig 9.5

Page 54: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Summary

• It can be shown that any query that can be expressed in the relational algebra, it can also be expressed in domain and tuple relational calculus

Page 55: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Quiz

• In what sense doe R.C differ from R.A, and in what sense are they similar?

Page 56: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

The Big Picture: SQL to Algebra toQuery Plan to Web Page

SELECT * FROM STUDENT, Takes, COURSE

WHERE STUDENT.sid = Takes.sID AND Takes.cID = cid

STUDENT

Takes COURSE

Merge

Hash

by cid by cidOptimizer

ExecutionEngine

StorageSubsystem

Web Server / UI / etc

Query Plan – anoperator tree

Page 57: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Relational Calculus: A Logical Way of

Expressing Query Operations• First-order logic (FOL) can also be thought of as a

query language, and can be used in two ways:– Tuple relational calculus– Domain relational calculus– Difference is the level at which variables are used: for at

tributes (domains) or for tuples

• The calculus is non-procedural (declarative) as compared to the algebra– More like what we’ll see in SQL– More convenient to express certain things

Page 58: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Domain Relational Calculus

Queries have form:

{<x1,x2, …, xn>| p}

Predicate: boolean expression over x1,x2, …, xn

– Precise operations depend on the domain and query language – may include special functions, etc.

– Assume the following at minimum:<xi,xj,…> R X op Y X op const const op X

where op is , , , , ,

xi,xj,… are domain variables

domain variables

predicate

Page 59: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

More Complex Predicates

Starting with these atomic predicates, build up new predicates by the following rules:– Logical connectives: If p and q are predicates, then so are

pq, pq, p, and pq• (x>2) (x<4)• (x>2) (x>0)

– Existential quantification: If p is a predicate, then so is x.p

x. (x>2) (x<4)

– Universal quantification: If p is a predicate, then so is x.p

x.x>2 x. y.y>x

Page 60: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Some Examples

• Faculty ids

• Course names for courses with students expecting a “C”

• Courses taken by Jill

Page 61: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Logical Equivalences

• There are two logical equivalences that will be heavily used:– pq p q

(Whenever p is true, q must also be true.) x. p(x) x. p(x)

(p is true for all x)

• The second can be a lot easier to check!

Page 62: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Free and Bound Variables

• A variable v is bound in a predicate p when p is of the form v… or v…

• A variable occurs free in p if it occurs in a position where it is not bound by an enclosing or

• Examples: – x is free in x>2– x is bound in x.x>y

Page 63: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Can Rename Bound Variables Only

• When a variable is bound one can replace it with some other variable without altering the meaning of the expression, providing there are no name clashes

• Example: x.x>2 is equivalent to y.y>2

• Otherwise, the variable is defined outside our “scope”…

Page 64: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Safety

• Pitfall in what we have done so far – how do we interpret: {<sid,name>| <sid,name> STUDENT}

– Set of all binary tuples that are not students: an infinite set (and unsafe query)

• A query is safe if no matter how we instantiate the relations, it always produces a finite answer– Domain independent: answer is the same regardless of the domain

in which it is evaluated– Unfortunately, both this definition of safety and domain independ

ence are semantic conditions, and are undecidable

Page 65: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Safety and Termination Guarantees

• There are syntactic conditions that are used to guarantee “safe” formulas– The definition is complicated, and we won’t discuss it;

you can find it in Ullman’s Principles of Database and Knowledge-Base Systems

– The formulas that are expressible in real query languages based on relational calculus are all “safe”

• Many DB languages include additional features, like recursion, that must be restricted in certain ways to guarantee termination and consistent answers

Page 66: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Mini-Quiz

How do you write:– Which students have taken more than one

course from the same professor?

– What is the highest course number offered?

Page 67: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Translating from RA to DRC

• Core of relational algebra: , , , x, -• We need to work our way through the structure of

an RA expression, translating each possible form.– Let TR[e] be the translation of RA expression e into D

RC.

• Relation names: For the RA expression R, the DRC expression is {<x1,x2, …, xn>| <x1,x2, …, xn> R}

Page 68: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Selection: TR[ R]

• Suppose we have (e’), where e’ is another RA expression that translates as:

TR[e’]= {<x1,x2, …, xn>| p}• Then the translation of c(e’) is

{<x1,x2, …, xn>| p’}where ’ is obtained from by replacing each attribute with the corresponding variable

• Example: TR[#1=#2 #4>2.5R] (if R has arity 4) is

{<x1,x2, x3, x4>|< x1,x2, x3, x4> R x1=x2 x4>2.5}

Page 69: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Projection: TR[i1,…,im(e)]

• If TR[e]= {<x1,x2, …, xn>| p} then TR[i1,i2,…,im

(e)]=

{<x i1,x i2

, …, x im >| xj1,xj2

, …, xjk.p},

where xj1,xj2

, …, xjk are variables in x1,x2, …, xn

that are not in x i1,x i2

, …, x im

• Example: With R as before,#1,#3 (R)={<x1,x3>| x2,x4. <x1,x2, x3,x4> R}

Page 70: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Union: TR[R1 R2]

• R1 and R2 must have the same arity• For e1 e2, where e1, e2 are algebra expressions

TR[e1]={<x1,…,xn>|p} and TR[e2]={<y1,…yn>|q}

• Relabel the variables in the second:TR[e2]={< x1,…,xn>|q’}

• This may involve relabeling bound variables in q to avoid clashesTR[e1e2]={<x1,…,xn>|pq’}.

• Example: TR[R1 R2] = {< x1,x2, x3,x4>| <x1,x2, x3,x4>R1 <x1,x2, x3,x4>R2

Page 71: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Other Binary Operators

• Difference: The same conditions hold as for unionIf TR[e1]={<x1,…,xn>|p} and TR[e2]={< x1,…,xn>|q}

Then TR[e1- e2]= {<x1,…,xn>|pq}

• Product: If TR[e1]={<x1,…,xn>|p} and TR[e2]={< y1,…,ym>|q}

Then TR[e1 e2]= {<x1,…,xn, y1,…,ym >| pq}

• Example: TR[RS]= {<x1,…,xn, y1,…,ym >|

<x1,…,xn> R <y1,…,ym > S }

Page 72: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Summary

• Can translate relational algebra into (domain) relational calculus.

• Given syntactic restrictions that guarantee safety of DRC query, can translate back to relational algebra

• These are the principles behind initial development of relational databases– SQL is close to calculus; query plan is close to algebra– Great example of theory leading to practice!

Page 73: Revision for Final Exam Prof. Sin-Min Lee Department of Computer Science

Limitations of the Relational Algebra / Calculus

Can’t do:– Aggregate operations– Recursive queries– Complex (non-tabular) structures

• Most of these are expressible in SQL, OQL,