34
Chapter 7: Chapter 7: Relations Relations Relations(7.1) Relations(7.1) n-any Relations & their n-any Relations & their Applications (7.2) Applications (7.2)

Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

Embed Size (px)

Citation preview

Page 1: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

Chapter 7: Chapter 7: RelationsRelations

Relations(7.1)Relations(7.1)

n-any Relations & their Applications n-any Relations & their Applications (7.2)(7.2)

Page 2: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

2

Relations (7.1)Relations (7.1) IntroductionIntroduction

Relationship between a program and its Relationship between a program and its variablesvariables

Integers that are congruent modulo kIntegers that are congruent modulo k

Pairs of cities linked by airline flights in a Pairs of cities linked by airline flights in a networknetwork

Page 3: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

3

Relations (7.1) (cont.)Relations (7.1) (cont.)

Relations & their propertiesRelations & their properties

Definition 1Definition 1

Let A and B be sets. A Let A and B be sets. A binary relation from A binary relation from A to Bto B is a subset of A * B. is a subset of A * B.

In other words, a binary relation from A to B In other words, a binary relation from A to B is a set R of ordered pairs where the first is a set R of ordered pairs where the first element of each ordered pair comes from A element of each ordered pair comes from A and the second element comes from B. and the second element comes from B.

Page 4: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

4

Relations (7.1) (cont.)Relations (7.1) (cont.)

Notation:Notation:

aRb aRb (a, b) (a, b) R R

aRb aRb (a, b) (a, b) R R

RR a ba b

00

11

22

X XX X

XX

XX

0

1

2

a

b

Page 5: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

5

Relations (7.1) (cont.)Relations (7.1) (cont.)

Example:Example:A = set of all citiesA = set of all citiesB = set of the 50 states in the USAB = set of the 50 states in the USADefine the relation R by specifying that Define the relation R by specifying that (a, b) belongs to R if city a is in state b.(a, b) belongs to R if city a is in state b.

R. in are

)New Jersey Bank, Red

)California,(Cupertino

)Michigan,Arbor Ann(

)Maine,Bangor(

)Colorado,Boulder(

Page 6: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

6

Relations (7.1) (cont.)Relations (7.1) (cont.)

Functions as relationsFunctions as relations

The graph of a function f is the set of ordered The graph of a function f is the set of ordered pairs (a, b) such that b = f(a)pairs (a, b) such that b = f(a)

The graph of f is a subset of A * B The graph of f is a subset of A * B it is a it is a relation from A to Brelation from A to B

Conversely, if R is a relation from A to B such Conversely, if R is a relation from A to B such that every element in A is the first element of that every element in A is the first element of exactly one ordered pair of R, then a function exactly one ordered pair of R, then a function can be defined with R as its graphcan be defined with R as its graph

Page 7: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

7

Relations (7.1) (cont.)Relations (7.1) (cont.) Relations on a setRelations on a set

Definition 2Definition 2

A A relationrelation on the set A is a relation from A to A. on the set A is a relation from A to A.

Example:Example: A = set {1, 2, 3, 4}. Which ordered A = set {1, 2, 3, 4}. Which ordered pairs are in the relation R = {(a, b) | a divides b}pairs are in the relation R = {(a, b) | a divides b}

Solution:Solution: Since (a, b) is in R if and only if a and b Since (a, b) is in R if and only if a and b are positive integers not exceeding 4 such that a are positive integers not exceeding 4 such that a divides bdivides b

R = {(1,1), (1,2), (1.3), (1.4), (2,2), (2,4), (3,3), R = {(1,1), (1,2), (1.3), (1.4), (2,2), (2,4), (3,3), (4,4)}(4,4)}

Page 8: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

8

Relations (7.1) (cont.)Relations (7.1) (cont.)

Properties of RelationsProperties of Relations

Definition 3Definition 3

A relation R on a set A is called A relation R on a set A is called reflexivereflexive if if (a, a) (a, a) R for every element a R for every element a A. A.

Page 9: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

9

Relations (7.1) (cont.)Relations (7.1) (cont.)

Example (a):Example (a): Consider the following Consider the following relations on {1, 2, 3, 4}relations on {1, 2, 3, 4}

RR11 = {(1,1), (1,2), (2,1), (2,2), (3,4), (4,1), (4,4)} = {(1,1), (1,2), (2,1), (2,2), (3,4), (4,1), (4,4)}RR22 = {(1,1), (1,2), (2,1)} = {(1,1), (1,2), (2,1)}RR33 = {(1,1), (1,2), (1,4), (2,1), (2,2), (3,3), (3,4), = {(1,1), (1,2), (1,4), (2,1), (2,2), (3,3), (3,4), (4,1), (4,4)}(4,1), (4,4)}RR44 = {(2,1), (3,1), (3,2), (4,1), (4,2), (4,3)} = {(2,1), (3,1), (3,2), (4,1), (4,2), (4,3)}RR55 = {(1,1), (1,2), (1,3), (1,4), (2,2), (2,3), (2,4), = {(1,1), (1,2), (1,3), (1,4), (2,2), (2,3), (2,4), (3,3), (3,4), (4,4)}(3,3), (3,4), (4,4)}RR66 = {(3,4)} = {(3,4)}

Which of these relations are reflexive?Which of these relations are reflexive?

Page 10: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

10

Relations (7.1) (cont.)Relations (7.1) (cont.)

Solution:Solution:

RR33 and R and R55: reflexive : reflexive both contain all both contain all pairs of the form (a, a): (1,1), (2,2), (3,3) pairs of the form (a, a): (1,1), (2,2), (3,3) & (4,4).& (4,4).

RR11, R, R22, R, R44 and R and R66: not reflexive : not reflexive not not contain all of these ordered pairs. (3,3) contain all of these ordered pairs. (3,3) is not in any of these relations.is not in any of these relations.RR11 = {(1,1), (1,2), (2,1), (2,2), (3,4), (4,1), (4,4)} = {(1,1), (1,2), (2,1), (2,2), (3,4), (4,1), (4,4)}RR22 = {(1,1), (1,2), (2,1)} = {(1,1), (1,2), (2,1)}RR33 = {(1,1), (1,2), (1,4), (2,1), (2,2), (3,3), (3,4), = {(1,1), (1,2), (1,4), (2,1), (2,2), (3,3), (3,4), (4,1), (4,4)}(4,1), (4,4)}RR44 = {(2,1), (3,1), (3,2), (4,1), (4,2), (4,3)} = {(2,1), (3,1), (3,2), (4,1), (4,2), (4,3)}RR55 = {(1,1), (1,2), (1,3), (1,4), (2,2), (2,3), (2,4), = {(1,1), (1,2), (1,3), (1,4), (2,2), (2,3), (2,4), (3,3), (3,4), (4,4)}(3,3), (3,4), (4,4)}RR66 = {(3,4)} = {(3,4)}

Page 11: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

11

Relations (7.1) (cont.)Relations (7.1) (cont.)

Definition 4:Definition 4:

A relation R on a set A is called A relation R on a set A is called symmetricsymmetric if (b, a) if (b, a) R whenever (a, b) R whenever (a, b) R, for all a, R, for all a, b b A. A. A relation R on a set A such that (a, b) A relation R on a set A such that (a, b) R and (b, a) R and (b, a) R only if a = b, for all a, b R only if a = b, for all a, b A, is called A, is called antisymmetricantisymmetric..

Page 12: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

12

Relations (7.1) (cont.)Relations (7.1) (cont.) Example:Example: Which of the relations from example (a) are Which of the relations from example (a) are

symmetric and which are antisymmetric?symmetric and which are antisymmetric?

Solution:Solution:

RR22 & R & R33: symmetric : symmetric each case (b, a) belongs to each case (b, a) belongs to the relation whenever (a, b) does.the relation whenever (a, b) does.For RFor R22: only thing to check that both (1,2) & (2,1) belong : only thing to check that both (1,2) & (2,1) belong to the relationto the relationFor RFor R33: it is necessary to check that both (1,2) & (2,1) : it is necessary to check that both (1,2) & (2,1) belong to the relation.belong to the relation.None of the other relations is symmetric: find a pair (a, None of the other relations is symmetric: find a pair (a, b) so that it is in the relation but (b, a) is not.b) so that it is in the relation but (b, a) is not.

RR11 = {(1,1), (1,2), (2,1), (2,2), (3,4), (4,1), (4,4)} = {(1,1), (1,2), (2,1), (2,2), (3,4), (4,1), (4,4)}RR22 = {(1,1), (1,2), (2,1)} = {(1,1), (1,2), (2,1)}RR33 = {(1,1), (1,2), (1,4), (2,1), (2,2), (3,3), (4,1), = {(1,1), (1,2), (1,4), (2,1), (2,2), (3,3), (4,1), (4,4)}(4,4)}RR44 = {(2,1), (3,1), (3,2), (4,1), (4,2), (4,3)} = {(2,1), (3,1), (3,2), (4,1), (4,2), (4,3)}RR55 = {(1,1), (1,2), (1,3), (1,4), (2,2), (2,3), (2,4), = {(1,1), (1,2), (1,3), (1,4), (2,2), (2,3), (2,4), (3,3), (3,4), (4,4)}(3,3), (3,4), (4,4)}RR66 = {(3,4)} = {(3,4)}

Page 13: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

13

Relations (7.1) (cont.)Relations (7.1) (cont.)Solution (cont.):Solution (cont.):

RR44, R, R55 and R and R66: antisymmetric : antisymmetric for each of for each of these relations there is no pair of elements a these relations there is no pair of elements a and b withand b with a a b such that both (a, b) and (b, a) belong b such that both (a, b) and (b, a) belong to the relation.to the relation.None of the other relations is antisymmetric.: None of the other relations is antisymmetric.: find a pair (a, b) with a find a pair (a, b) with a b so that (a, b) and b so that (a, b) and (b, a) are both in the relation.(b, a) are both in the relation.

RR11 = {(1,1), (1,2), (2,1), (2,2), (3,4), (4,1), (4,4)} = {(1,1), (1,2), (2,1), (2,2), (3,4), (4,1), (4,4)}RR22 = {(1,1), (1,2), (2,1)} = {(1,1), (1,2), (2,1)}RR33 = {(1,1), (1,2), (1,4), (2,1), (2,2), (3,3), (3,4), = {(1,1), (1,2), (1,4), (2,1), (2,2), (3,3), (3,4), (4,1), (4,4)}(4,1), (4,4)}RR44 = {(2,1), (3,1), (3,2), (4,1), (4,2), (4,3)} = {(2,1), (3,1), (3,2), (4,1), (4,2), (4,3)}RR55 = {(1,1), (1,2), (1,3), (1,4), (2,2), (2,3), (2,4), = {(1,1), (1,2), (1,3), (1,4), (2,2), (2,3), (2,4), (3,3), (3,4), (4,4)}(3,3), (3,4), (4,4)}RR66 = {(3,4)} = {(3,4)}

Page 14: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

14

Relations (7.1) (cont.)Relations (7.1) (cont.)

Definition 5:Definition 5:

A relation R on a set A is called A relation R on a set A is called transitivetransitive if whenever (a, b) if whenever (a, b) R and R and (b,c) (b,c) R, then R, then (a, c) (a, c) R, for all a, b, c R, for all a, b, c R. R.

Page 15: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

15

Relations (7.1) (cont.)Relations (7.1) (cont.) Example:Example: Which of the relations in example Which of the relations in example

(a) are transitive?(a) are transitive? RR44 , R , R55 & R & R66 : transitive : transitive verify that if (a, b) verify that if (a, b)

and (b, c) belong to this relation then (a, c) belongs and (b, c) belong to this relation then (a, c) belongs also to the relationalso to the relationRR44 transitive since (3,2) and (2,1), (4,2) and (2,1), transitive since (3,2) and (2,1), (4,2) and (2,1), (4,3) and (3,1), and (4,3) and (3,2) are the only (4,3) and (3,1), and (4,3) and (3,2) are the only such sets of pairs, and (3,1) , (4,1) and (4,2) belong such sets of pairs, and (3,1) , (4,1) and (4,2) belong to Rto R44..Same reasoning for RSame reasoning for R55 and R and R66..

RR11 : not transitive : not transitive (3,4) and (4,1) belong to R (3,4) and (4,1) belong to R11, , but (3,1) does not.but (3,1) does not.

RR22 : not transitive : not transitive (2,1) and (1,2) belong to R (2,1) and (1,2) belong to R22, , but (2,2) does not.but (2,2) does not.

RR33 : not transitive : not transitive (4,1) and (1,2) belong to R (4,1) and (1,2) belong to R33, , but (4,2) does not.but (4,2) does not.

RR11 = {(1,1), (1,2), (2,1), (2,2), (3,4), (4,1), (4,4)} = {(1,1), (1,2), (2,1), (2,2), (3,4), (4,1), (4,4)}RR22 = {(1,1), (1,2), (2,1)} = {(1,1), (1,2), (2,1)}RR33 = {(1,1), (1,2), (1,4), (2,1), (2,2), (3,3), (3,4), = {(1,1), (1,2), (1,4), (2,1), (2,2), (3,3), (3,4), (4,1), (4,4)}(4,1), (4,4)}RR44 = {(2,1), (3,1), (3,2), (4,1), (4,2), (4,3)} = {(2,1), (3,1), (3,2), (4,1), (4,2), (4,3)}RR55 = {(1,1), (1,2), (1,3), (1,4), (2,2), (2,3), (2,4), = {(1,1), (1,2), (1,3), (1,4), (2,2), (2,3), (2,4), (3,3), (3,4), (4,4)}(3,3), (3,4), (4,4)}RR66 = {(3,4)} = {(3,4)}

Page 16: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

16

Relations (7.1) (cont.)Relations (7.1) (cont.)

Combining relationsCombining relations

Example:Example:

Let A = {1, 2, 3} and B = {1, 2, 3, 4, }. The relations Let A = {1, 2, 3} and B = {1, 2, 3, 4, }. The relations RR11 = {(1,1), (2,2), (3,3)} and = {(1,1), (2,2), (3,3)} and RR22 = {(1,1), (1,2), (1,3), (1,4)} can be combined to = {(1,1), (1,2), (1,3), (1,4)} can be combined to obtain:obtain:

RR11 R R22 = {(1,1), (1,2), (1,3), (1,4), (2,2), (3,3)} = {(1,1), (1,2), (1,3), (1,4), (2,2), (3,3)}

RR1 1 R R22 = {(1,1)} = {(1,1)}

RR11 – R – R22 = {(2,2), (3,3)} = {(2,2), (3,3)}RR22 – R – R11 = {(1,2), (1,3), (1,4)} = {(1,2), (1,3), (1,4)}

Page 17: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

17

Relations (7.1) (cont.)Relations (7.1) (cont.) Definition 6:Definition 6:

Let R be a relation from a set A to a set Let R be a relation from a set A to a set B and S a relation from B to a set C.B and S a relation from B to a set C.The The compositecomposite of R and S is the relation of R and S is the relation consisting of ordered pairs (a, c), where consisting of ordered pairs (a, c), where a a A, c A, c C, and for which there exists C, and for which there exists an element b an element b B such that (a, b) B such that (a, b) R R and (b, c) and (b, c) S. We denote the composite S. We denote the composite of R and S by S of R and S by S R. R.

Page 18: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

18

Relations (7.1) (cont.)Relations (7.1) (cont.) Example:Example: What is the composite of the relations R What is the composite of the relations R

and S where R is the relation from {1,2,3} to and S where R is the relation from {1,2,3} to {1,2,3,4} with R = {(1,1), (1,4), (2,3), (3,1), {1,2,3,4} with R = {(1,1), (1,4), (2,3), (3,1), (3,4)} and S is the relation from {1,2,3,4} to (3,4)} and S is the relation from {1,2,3,4} to {0,1,2} with S = {(1,0), (2,0), (3,1), (3,2), (4,1)}?{0,1,2} with S = {(1,0), (2,0), (3,1), (3,2), (4,1)}?

Solution:Solution: S S R is constructed using all ordered R is constructed using all ordered pairs in R and ordered pairs in S, where the pairs in R and ordered pairs in S, where the second element of the ordered in R agrees with second element of the ordered in R agrees with the first element of the ordered pair in S.the first element of the ordered pair in S.For example, the ordered pairs (2,3) in R and For example, the ordered pairs (2,3) in R and (3,1) in S produce the ordered pair (2,1) in S(3,1) in S produce the ordered pair (2,1) in S R. R. Computing all the ordered pairs in the composite, Computing all the ordered pairs in the composite, we findwe find

SS R = ((1,0), (1,1), (2,1), (2,2), (3,0), (3,1)} R = ((1,0), (1,1), (2,1), (2,2), (3,0), (3,1)}

Page 19: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

19

N-ary Relations & their N-ary Relations & their Applications (7.2)Applications (7.2)

Relationship among elements of Relationship among elements of more than 2 setsmore than 2 sets often arise: n-ary often arise: n-ary relationsrelations

Airline, flight number, starting point, Airline, flight number, starting point, destination, departure time, arrival destination, departure time, arrival timetime

Page 20: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

20

N-ary Relations & their N-ary Relations & their Applications (7.2) (cont.)Applications (7.2) (cont.)

N-ary relationsN-ary relations

Definition 1:Definition 1:

Let ALet A11, A, A22, …, A, …, Ann be sets. An n-ary relation on be sets. An n-ary relation on these sets is a subset of Athese sets is a subset of A11 * A * A22 *…* A *…* Ann where A where Aii are the are the domainsdomains of the relation, and n is called of the relation, and n is called its its degreedegree..

Example:Example: Let R be the relation on N * N * N Let R be the relation on N * N * N consisting of triples (a, b, c) where a, b, and c consisting of triples (a, b, c) where a, b, and c are integers with a<b<c. Then (1,2,3) are integers with a<b<c. Then (1,2,3) R, but R, but (2,4,3) (2,4,3) R. The degree of this relation is 3. Its R. The degree of this relation is 3. Its domains are equal to the set of integers.domains are equal to the set of integers.

Page 21: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

21

N-ary Relations & their N-ary Relations & their Applications (7.2) (cont.)Applications (7.2) (cont.)

Databases & RelationsDatabases & Relations

Relational database modelRelational database model has been has been developed for information processingdeveloped for information processing

A database consists of records, which are n-A database consists of records, which are n-tuples made up of fieldstuples made up of fields

The fields contains information such as:The fields contains information such as: NameName Student #Student # MajorMajor Grade point average of the studentGrade point average of the student

Page 22: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

22

N-ary Relations & their N-ary Relations & their Applications (7.2) (cont.)Applications (7.2) (cont.)

The relational database model The relational database model represents a database of records or n-represents a database of records or n-ary relationary relation

The relation is R(Student-Name, Id-The relation is R(Student-Name, Id-number, Major, GPA)number, Major, GPA)

Page 23: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

23

N-ary Relations & their N-ary Relations & their Applications (7.2) (cont.)Applications (7.2) (cont.)

Example of recordsExample of records

(Smith, 3214, Mathematics, 3.9)(Smith, 3214, Mathematics, 3.9)

(Stevens, 1412, Computer Science, 4.0)(Stevens, 1412, Computer Science, 4.0)(Rao, 6633, Physics, 3.5)(Rao, 6633, Physics, 3.5)(Adams, 1320, Biology, 3.0)(Adams, 1320, Biology, 3.0)(Lee, 1030, Computer Science, 3.7)(Lee, 1030, Computer Science, 3.7)

Page 24: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

24

N-ary Relations & their N-ary Relations & their Applications (7.2) (cont.)Applications (7.2) (cont.)

Students Students NamesNames

ID #ID # MajorMajor GPAGPA

SmithSmith

StevensStevens

RaoRao

AdamsAdams

LeeLee

32143214

14121412

66336633

13201320

10301030

MathematicsMathematics

Computer Computer ScienceScience

PhysicsPhysics

BiologyBiology

Computer Computer ScienceScience

3.93.9

4.04.0

3.53.5

3.03.0

3.73.7

TABLE A: Students

Page 25: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

25

N-ary Relations & their N-ary Relations & their Applications (7.2) (cont.)Applications (7.2) (cont.) Operations on n-ary relationsOperations on n-ary relations

There are varieties of operations that are There are varieties of operations that are applied on n-ary relations in order to create applied on n-ary relations in order to create new relations that answer eventual queries new relations that answer eventual queries of a databaseof a database

Definition 2:Definition 2:

Let R be an n-ary relation and C a condition Let R be an n-ary relation and C a condition that elements in R may satisfy. Then the that elements in R may satisfy. Then the selection operatorselection operator s sCC maps n-ary relation R maps n-ary relation R to the n-ary relation of all n-tuples from R to the n-ary relation of all n-tuples from R that satisfy the condition C.that satisfy the condition C.

Page 26: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

26

N-ary Relations & their N-ary Relations & their Applications (7.2) (cont.)Applications (7.2) (cont.)

Example: Example:

if sif sCC = “Major = “computer science” = “Major = “computer science” GPA > 3.5” then the result of this GPA > 3.5” then the result of this selection consists of the 2 four-tuples:selection consists of the 2 four-tuples:

(Stevens, 1412, Computer Science, 4.0)(Stevens, 1412, Computer Science, 4.0)

(Lee, 1030, Computer Science, 3.7)(Lee, 1030, Computer Science, 3.7)

Page 27: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

27

N-ary Relations & their N-ary Relations & their Applications (7.2) (cont.)Applications (7.2) (cont.)

Definition 3:Definition 3:

The The projectionprojection maps the n- maps the n-tuple (atuple (a11,, aa22, …, a, …, ann) to the m-tuple) to the m-tuplewhere m where m n. n.

In other words, the projection In other words, the projection deletes n – m of the components of n- deletes n – m of the components of n-tuple, leaving the ituple, leaving the i11th, ith, i22th, …, and ith, …, and immth th components. components.

mi,...,2i,1iP

)a,...,a,a(mi2i1i

mi,...,2i,1iP

Page 28: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

28

N-ary Relations & their N-ary Relations & their Applications (7.2) (cont.)Applications (7.2) (cont.)

Example:Example: What relation results when the What relation results when the projection Pprojection P1,41,4 is applied to the relation in Table is applied to the relation in Table A? A?

Solution: When the projection PSolution: When the projection P1,41,4 is used, the second and is used, the second and third columns of the table are deleted, and pairs third columns of the table are deleted, and pairs representing student names and GPA are obtained. Table representing student names and GPA are obtained. Table B displays the results of this projection.B displays the results of this projection.

Students Students NamesNames

GPAGPA

SmithSmith

StevensStevens

RaoRao

AdamsAdams

LeeLee

3.93.9

4.04.0

3.53.5

3.03.0

3.73.7

TABLE B:GPAs

Page 29: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

29

N-ary Relations & their N-ary Relations & their Applications (7.2) (cont.)Applications (7.2) (cont.)

Definition 4:Definition 4:

Let R be a relation of degree m and S a Let R be a relation of degree m and S a relation of degree n. The relation of degree n. The joinjoin J Jpp(R,S), where (R,S), where p p m and p m and p n, is a relation of degree n, is a relation of degree m + n – p that consists of all (m + n – p)-m + n – p that consists of all (m + n – p)-tuples (atuples (a11, a, a22, …, a, …, am-pm-p, c, c11, c, c22, …, c, …, cpp, b, b11, b, b22, …, , …, bbn-pn-p), where the m-tuple (a), where the m-tuple (a11, a, a22, …, a, …, am-pm-p, c, c11, , cc22, …, c, …, cpp) belongs to R and the n-tuple (c) belongs to R and the n-tuple (c11, , cc22, …, c, …, cpp, b, b11, b, b22, …, b, …, bn-pn-p) belongs to S.) belongs to S.

Page 30: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

30

N-ary Relations & their N-ary Relations & their Applications (7.2) (cont.)Applications (7.2) (cont.)

Example:Example: What relation results when the What relation results when the operator Joperator J22 is used to combine the relation is used to combine the relation displayed in tables C and D?displayed in tables C and D?

Page 31: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

31

ProfessorProfessor DptDpt Course #Course #

CruzCruz

CruzCruz

FarberFarber

FarberFarber

GrammerGrammer

GrammerGrammer

RosenRosen

RosenRosen

ZoologyZoology

ZoologyZoology

PsychologyPsychology

PsychologyPsychology

PhysicsPhysics

PhysicsPhysics

Computer ScienceComputer Science

MathematicsMathematics

335335

412412

501501

617617

544544

551551

518518

575575

DptDpt Course Course ##

RoomRoom TimeTime

Computer ScienceComputer Science

MathematicsMathematics

MathematicsMathematics

PhysicsPhysics

PsychologyPsychology

PsychologyPsychology

ZoologyZoology

ZoologyZoology

518518

575575

611611

544544

501501

617617

335335

412412

N521N521

N502N502

N521N521

B505B505

A100A100

A110A110

A100A100

A100A100

2:00 PM2:00 PM

3:00 PM3:00 PM

4:00 PM4:00 PM

4:00 PM4:00 PM

3:00 PM3:00 PM

11:00 AM11:00 AM

9:00 AM9:00 AM

8:00 AM8:00 AM

TABLE C:TeachingAssignments

TABLE D:ClassSchedule

Page 32: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

32

N-ary Relations & their N-ary Relations & their Applications (7.2) (cont.)Applications (7.2) (cont.)

Solution:Solution: The join J The join J2 2 produces the relation produces the relation shown in Table Eshown in Table E

ProfessorProfessor DptDpt Course Course ##

RoomRoom TimeTime

CruzCruz

CruzCruz

FarberFarber

FarberFarber

GrammerGrammer

RosenRosen

RosenRosen

ZoologyZoology

ZoologyZoology

PsychologyPsychology

PsychologyPsychology

PhysicsPhysics

Computer Computer ScienceScience

MathematicsMathematics

335335

412412

501501

617617

544544

518518

575575

A100A100

A100A100

A100A100

A110A110

B505B505

N521N521

N502N502

9:00 AM9:00 AM

8:00 AM8:00 AM

3:00 PM3:00 PM

11:00 11:00 AMAM

4:00 PM4:00 PM

2:00 PM2:00 PM

3:00 PM3:00 PM

Table E:Teaching Schedule

Page 33: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

33

N-ary Relations & their N-ary Relations & their Applications (7.2) (cont.)Applications (7.2) (cont.)

Example:Example: We will illustrate how SQL (Structured We will illustrate how SQL (Structured Query Language) is used to express queries by Query Language) is used to express queries by showing how SQL can be employed to make a query showing how SQL can be employed to make a query about airline flights using Table F. The SQL about airline flights using Table F. The SQL statementsstatements

SELECT departure_timeSELECT departure_timeFROM FlightsFROM FlightsWHERE destination = ‘Detroit’WHERE destination = ‘Detroit’

are used to find theare used to find the projection projection P P55 (on the (on the departure_time attribute) of the departure_time attribute) of the selectionselection of 5-tuples of 5-tuples in the flights database that satisfy the condition: in the flights database that satisfy the condition: destination = ‘Detroit’. The output would be a list destination = ‘Detroit’. The output would be a list containing the times of flights that have Detroit as containing the times of flights that have Detroit as their destination, namely, 08:10, 08:47, and 9:44.their destination, namely, 08:10, 08:47, and 9:44.

Page 34: Chapter 7: Relations Relations(7.1) Relations(7.1) n-any Relations & their Applications (7.2) n-any Relations & their Applications (7.2)

34

N-ary Relations & their N-ary Relations & their Applications (7.2) (cont.)Applications (7.2) (cont.)

AirlineAirline Flight #Flight # GateGate DestinationDestination Departure Departure timetime

NadirNadir

AcmeAcme

AcmeAcme

AcmeAcme

NadirNadir

AcmeAcme

NadirNadir

122122

221221

122122

323323

199199

222222

322322

3434

2222

3333

3434

1313

2222

3434

DetroitDetroit

DenverDenver

AnchorageAnchorage

HonoluluHonolulu

DetroitDetroit

DenverDenver

DetroitDetroit

08:1008:10

08:1708:17

08:2208:22

08:3008:30

08”4708”47

09:1009:10

09:4409:44

Table F: Flights