revision for midterm 3 revision 3 prof. sin-min lee department of computer science

68
Revision for Midterm 3 revision 3 Prof. Sin-Min Lee Department of Computer Science

Post on 20-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Revision for Midterm 3 revision 3 Prof. Sin-Min Lee Department of Computer Science

Revision for Midterm 3 revision 3

Prof. Sin-Min Lee

Department of Computer Science

Page 2: Revision for Midterm 3 revision 3 Prof. Sin-Min Lee Department of Computer Science
Page 3: Revision for Midterm 3 revision 3 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 4: Revision for Midterm 3 revision 3 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 5: Revision for Midterm 3 revision 3 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 6: Revision for Midterm 3 revision 3 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 7: Revision for Midterm 3 revision 3 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 8: Revision for Midterm 3 revision 3 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 9: Revision for Midterm 3 revision 3 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 10: Revision for Midterm 3 revision 3 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 11: Revision for Midterm 3 revision 3 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 12: Revision for Midterm 3 revision 3 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 13: Revision for Midterm 3 revision 3 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 14: Revision for Midterm 3 revision 3 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 15: Revision for Midterm 3 revision 3 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 16: Revision for Midterm 3 revision 3 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 17: Revision for Midterm 3 revision 3 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 18: Revision for Midterm 3 revision 3 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 19: Revision for Midterm 3 revision 3 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 20: Revision for Midterm 3 revision 3 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 21: Revision for Midterm 3 revision 3 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 22: Revision for Midterm 3 revision 3 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 23: Revision for Midterm 3 revision 3 Prof. Sin-Min Lee Department of Computer Science

Division (cont’d)

Page 24: Revision for Midterm 3 revision 3 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 25: Revision for Midterm 3 revision 3 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 26: Revision for Midterm 3 revision 3 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 27: Revision for Midterm 3 revision 3 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 28: Revision for Midterm 3 revision 3 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 29: Revision for Midterm 3 revision 3 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 30: Revision for Midterm 3 revision 3 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 31: Revision for Midterm 3 revision 3 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 32: Revision for Midterm 3 revision 3 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 33: Revision for Midterm 3 revision 3 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 34: Revision for Midterm 3 revision 3 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 35: Revision for Midterm 3 revision 3 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 36: Revision for Midterm 3 revision 3 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 37: Revision for Midterm 3 revision 3 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 38: Revision for Midterm 3 revision 3 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 39: Revision for Midterm 3 revision 3 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 40: Revision for Midterm 3 revision 3 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 41: Revision for Midterm 3 revision 3 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 42: Revision for Midterm 3 revision 3 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 43: Revision for Midterm 3 revision 3 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 44: Revision for Midterm 3 revision 3 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 45: Revision for Midterm 3 revision 3 Prof. Sin-Min Lee Department of Computer Science

Normalization

Review on Keys • superkey: a set of attributes which will uniquely

identify each tuple in a relation• candidate key: a minimal superkey• primary key: a chosen candidate key• secondary key: all the rest of candiate keys• prime attribute: an attribute that is a part of a

candidate key (key column)• nonprime attribute: a nonkey column

Page 46: Revision for Midterm 3 revision 3 Prof. Sin-Min Lee Department of Computer Science

Normalization

Functional Dependency Type by Keys • ‘whole (candidate) key nonprime attribute’: full

FD (no violation)• ‘partial key nonprime attribute’: partial FD

(violation of 2NF)• ‘nonprime attribute nonprime attribute’:

transitive FD (violation of 3NF)• ‘not a whole key prime attribute’: violation of

BCNF

Page 47: Revision for Midterm 3 revision 3 Prof. Sin-Min Lee Department of Computer Science
Page 48: Revision for Midterm 3 revision 3 Prof. Sin-Min Lee Department of Computer Science
Page 49: Revision for Midterm 3 revision 3 Prof. Sin-Min Lee Department of Computer Science

Perform lossless-join decompositions of each of the following scheme into BCNF schemes: R(A, B, C, D, E) with dependency set {AB CDE, C D, D E}

A B C D A B C D

C D D EA B C E A B C D

C DD E A B C A B C

Page 50: Revision for Midterm 3 revision 3 Prof. Sin-Min Lee Department of Computer Science

Given the FDs {B D, AB C, D B} and the relation {A, B, C, D}, give a two distinct lossless join decomposition to BNCF indicating the keys of each of the resulting relations.

A B C D

B D A B C

A B C D

B D A C D

Page 51: Revision for Midterm 3 revision 3 Prof. Sin-Min Lee Department of Computer Science

Definition of MVD

• A multivalued dependency (MVD) X ->->Y is an assertion that if two tuples of a relation agree on all the attributes of X, then their components in the set of attributes Y may be swapped, and the result will be two tuples that are also in the relation.

Page 52: Revision for Midterm 3 revision 3 Prof. Sin-Min Lee Department of Computer Science

Example

• The name-addr-phones-beersLiked example illustrated the MVD

name->->phones

and the MVD

name ->-> beersLiked.

Page 53: Revision for Midterm 3 revision 3 Prof. Sin-Min Lee Department of Computer Science

Picture of MVD X ->->Y

X Y others

equal

exchange

Page 54: Revision for Midterm 3 revision 3 Prof. Sin-Min Lee Department of Computer Science

MVD Rules

• Every FD is an MVD.– If X ->Y, then swapping Y ’s between two tuples that

agree on X doesn’t change the tuples.– Therefore, the “new” tuples are surely in the

relation, and we know X ->->Y.

• Complementation : If X ->->Y, and Z is all the other attributes, then X ->->Z.

Page 55: Revision for Midterm 3 revision 3 Prof. Sin-Min Lee Department of Computer Science
Page 56: Revision for Midterm 3 revision 3 Prof. Sin-Min Lee Department of Computer Science
Page 57: Revision for Midterm 3 revision 3 Prof. Sin-Min Lee Department of Computer Science
Page 58: Revision for Midterm 3 revision 3 Prof. Sin-Min Lee Department of Computer Science

Fourth Normal Form

• The redundancy that comes from MVD’s is not removable by putting the database schema in BCNF.

• There is a stronger normal form, called 4NF, that (intuitively) treats MVD’s as FD’s when it comes to decomposition, but not when determining keys of the relation.

Page 59: Revision for Midterm 3 revision 3 Prof. Sin-Min Lee Department of Computer Science

4NF Definition

• A relation R is in 4NF if whenever X ->->Y is a nontrivial MVD, then X is a superkey.

– “Nontrivial means that:1. Y is not a subset of X, and

2. X and Y are not, together, all the attributes.

– Note that the definition of “superkey” still depends on FD’s only.

Page 60: Revision for Midterm 3 revision 3 Prof. Sin-Min Lee Department of Computer Science

BCNF Versus 4NF

• Remember that every FD X ->Y is also an MVD, X ->->Y.

• Thus, if R is in 4NF, it is certainly in BCNF.– Because any BCNF violation is a 4NF

violation.

• But R could be in BCNF and not 4NF, because MVD’s are “invisible” to BCNF.

Page 61: Revision for Midterm 3 revision 3 Prof. Sin-Min Lee Department of Computer Science

Normalization

Good Decomposition • dependency preserving decomposition

- it is undesirable to lose functional dependencies during decomposition

• lossless join decomposition

- join of decomposed relations should be able to create the original relation (no spurious tuples)

Page 62: Revision for Midterm 3 revision 3 Prof. Sin-Min Lee Department of Computer Science
Page 63: Revision for Midterm 3 revision 3 Prof. Sin-Min Lee Department of Computer Science

Decomposition and 4NF

• If X ->->Y is a 4NF violation for relation R, we can decompose R using the same technique as for BCNF.

1. XY is one of the decomposed relations.

2. All but Y – X is the other.

Page 64: Revision for Midterm 3 revision 3 Prof. Sin-Min Lee Department of Computer Science
Page 65: Revision for Midterm 3 revision 3 Prof. Sin-Min Lee Department of Computer Science
Page 66: Revision for Midterm 3 revision 3 Prof. Sin-Min Lee Department of Computer Science

Example

Drinkers(name, addr, phones, beersLiked)

FD: name -> addr

MVD’s: name ->-> phones

name ->-> beersLiked

• Key is {name, phones, beersLiked}.

• All dependencies violate 4NF.

Page 67: Revision for Midterm 3 revision 3 Prof. Sin-Min Lee Department of Computer Science

Example, Continued

• Decompose using name -> addr:

1. Drinkers1(name, addr) In 4NF, only dependency is name -> addr.

2. Drinkers2(name, phones, beersLiked) Not in 4NF. MVD’s name ->-> phones and

name ->-> beersLiked apply. No FD’s, so all three attributes form the key.

Page 68: Revision for Midterm 3 revision 3 Prof. Sin-Min Lee Department of Computer Science

Example: Decompose Drinkers2

• Either MVD name ->-> phones or name ->-> beersLiked tells us to decompose to:– Drinkers3(name, phones)– Drinkers4(name, beersLiked)