### Prelude to Relationships - Mathematical relations

• Prelude to Relationship: Relation ---- a well-known mathematical concept

• OK, we have covered the E (Entity) in the ER-model

Next up: the R (Relationship) in the ER-model

• Cartesian product:

• Given two sets:

• A = {a1, a1, ..., am}
• B = {b1, b1, ..., bn}

• The cartesian product A × B of the sets A = {a1, a2, ..., , am} and B = {b1, b2, ..., , bn} is:

 set of all pairs of tuples { (a1,b1), (a1,b2), ... (a1,bn), (a2,b1), (a2,b2), ... (a2,bn), ... (am,b1), (am,b2), ... (am,bn)}

Example:

 ``` A = {a, b, c} B = {3, 7} A × B = { (a,3), (a,7), (b,3), (b,7), (c,3), (c,7) } ```

• Relation:

• Given two sets:

• A = {a1, a1, ..., am}
• B = {b1, b1, ..., bn}

• A relation R between the 2 sets A and B (in Mathematics) is:

 a subset of the cartesian product A × B of the sets A and B

Example 1:

• Given the sets:

 Students = {John, James, Mary} Courses = {CS170, CS255, CS377, CS450, MATH110, MATH221}

An example of a relation between Students and Courses is:

 Enrollment = {(John, CS170), (John, CS255), (Mary, CS170)}

Example 2:

• Given the sets:

 Students = {John, James, Mary} Majors = {Math, CS, Chem, Phys}

An example of a relation between Students and Majors is:

 Majoring = {(John, Math), (John, Chem), (Mary, CS)}

• Higher degree relations

• Higher degree cartesian products:

• The cartesian product of 3 sets A, B and C is:

 the set of all tuples (a, b, c) where a ∈ A,   b ∈ B and c ∈ C

Example:

 ``` A = {a, b} B = {x, y} C = {1, 4} A × B × C = { (a,x,1), (a,x,2), (a,y,1), (a,y,2), (b,x,1), (b,x,2), (b,y,1), (b,y,2) } ```

• Higher degree relations:

• Given 3 sets:

• A = {a1, a1, ..., am}
• B = {b1, b1, ..., bn}
• C = {c1, c1, ..., ck}

• A relation R between the 3 sets A, B and C is:

 a subset of the cartesian product A × B × C of the sets A,   B and C

• Nomenclature

• Types of relations:

 Binary relation = a relation between two sets A and B Ternary relation = a relation between 3 sets A,   B and C

• Notes:

 Binary relations are very common !!! A ternary relation is very rare !!! Relations between 4 sets are almost impossible to find !!!

• Some notes on "Relation"

• The "relation" concept in Mathematics is static

In other words:

The content of a Mathematical relation does not change in time

This is the main reason why a relation is not used in ER modeling (content of relations must change in database, e.g., more kids are born to parents)...

Instead, ER-modeling uses the concept of relationship

• Nevertheless, but the relationship concept used in ER model is very closely related with the mathematical concept of relation