Relationships within the
 relational database
Relationships within the relational database
 You already know that relationships are classified as one-to-
  one, one-to-many, and many-to-many.

    The 1:M relationship is the relational modeling ideal.
    Therefore, this relationship type should be the norm in any
    relational database design.

 The 1:1 relationship should be rare in any relational database
  design.

 M:N relationships cannot be implemented as such in the
  relational model.
Relationships within the relational database




          The 1:M
         Relationship
Relationships within the relational database
 The 1:M Relationship

The 1:M relationship is the relational database norm.
Relationships within the relational database
 The 1:M Relationship

• Each painting is painted by one and only one painter, but each
  painter could have painted many paintings.
Relationships within the relational database
 The 1:M Relationship

• There is only one row in the PAINTER table for any given row in the
  PAINTING table, but there are may be many rows in the PAINTING
  table for any given row in the PAINTER table.



• The 1:M relationship is found in any database environment. Students in
  a typical college or university will discover that each COURSE can
  generate many CLASSes but that each CLASS refers to only one
  COURSE.
Relationships within the relational database
 The 1:M Relationship

The ERM( entity relationship model) for the 1:M relationship between
  COURSE and CLASS.
Relationships within the relational database
 The 1:M Relationship
Relationships within the relational database
 The 1:M Relationship

• Each COURSE can have many CLASSes, but each CLASS references
  only one COURSE

• There will be only one row in the COURSE table form any given
  row in the COURSE table.
Relationships within the relational database




          The 1:1
         Relationship
Relationships within the relational database
 The 1:1 Relationship

• As the 1:1 label implies, in this relationship, one entity can be
  related to only one other entity, and vise versa.

• For example, one department chair-a professor-can chair only one
  department, and one department can have only one department
  chair.

• The entities PROFESSOR and DEPARTMENT thus exhibit a 1:1
  relationship.
Relationships within the relational database
 The 1:1 Relationship




      The basic relationship modeled in Figure 3.22
Relationships within the relational database
 The 1:1 Relationship




      As you examine the tables in Figure 3.23, note that there
Relationships within the relational database
1.   Each professor is a Tiny College employee. Therefore, the professor
     identification is through the EMP_Num.

2.   The 1:1 PROFESSOR chairs DEPARTMENT relationship is
     implemented by having the EMP_NUM fereign key in the
     DEPARTMENT table.

3.   Also note that the PROFESSOR table contains the DEPT_CODE
     foreign key to implement the 1:M DEPARTMENT employs
     PROFESSOR relationship.
Relationships within the relational database
•    The preceding “PROFESSOR chairs DEPARTMENT
    example illustrates a proper 1:1 relationship. In fact, the
    use of 1:1 relationship ensures that two entity sets are
    not placed in the same table when they should not be.

However, the existence of 1:1 relationship sometimes
  means that the entity components were not defined
  properly. It could indicate that the two entities actually
  belong in the same table!
Relationships within the relational database




     The M:N
   RELATIONSHIP
Relationships within the relational database


•   A many-to-many (M:N) relationship is not supported directly in the relational
    environment. However, M:N relationships can be implemented by creating a
    new entity in 1:M relationships with the original entities.
Relationships within the relational database
Relationships within the relational database
•   To explore the many-to-many (M:N) relationship, consider a rather typical
    college environment in which each STUDENT can take CLASSes and each
    CLASS can contain many STUDENTs. The ER model in Figure 3.24 show
    the M:N relationship.
Relationships within the relational database
•    Each CLASS can have many STUDENTs, and each STUDENT can
    take many CLASSes.

• There can be many rows in the CLASS table for any given row in the
  STUDENT table, and there can be many rows in the STUDENT table
  for any given row in the class



• To examine the M:N relationship more closely, imagine a small
  college with two students, each of whom takes three classes.
Relationships within the relational database
  The M:N relationship should not be implemented for two god reasons:


 The tables create many redundancies. For example, note that the STU_NUM
 values occur many times in the STUDENT table.


 • Given the structure and contents of the two tables, the relational operations
 become very complex and are likely to lead to system efficiency errors and
 output errors.
Relationships within the relational database
Relationships within the relational database
  Fortunately, the problems inherent in the many-to-many (M:N)
 relationship can easily be avoided by creating a composite entity (also
 referred to as a bridge entity or an associative entity). Because such a table
 is used to link the tables that were originally related in an M:N relationship,
 the composite entity structure includes-as foreign keys – at least the
 primary keys of the tables that are to be linked.
Relationships within the relational database
Relationships within the relational database
  The ENROLL table yields the required M:N to 1:M conversion. Observe that
 the composite entity represented by the ENROLL table must contain at least
 the primary keys of the CLASS and STUDENTS tables (CLASS_CODE and
 STU_NUM, respectively) for which it serves as a connector. Also note that the
 STUDENT and CLASS tables now contain only one row per entity.


 The ENROLL table contains multiple occurrences of the foreign key
 values, but those controlled redundancies are incapable of producing
 anomalies as long as referential integrity is enforced.
Relationships within the relational database




 •As you can examine the figure, note that composite entity named ENROLL
 represents the linking table between STUDENT and CLASS.
Relationships within the relational database




 •This figure shows the expanded ERM, including the expanded 1:M relationship
 between COURSE and CLASS
Relationships within the relational database
Prepared BY:

•HARRY OCHINANG
  •JANE CATALLA
   •RONN RIVERA

Relationships within the relational database

  • 1.
    Relationships within the relational database
  • 2.
    Relationships within therelational database  You already know that relationships are classified as one-to- one, one-to-many, and many-to-many.  The 1:M relationship is the relational modeling ideal. Therefore, this relationship type should be the norm in any relational database design.  The 1:1 relationship should be rare in any relational database design.  M:N relationships cannot be implemented as such in the relational model.
  • 3.
    Relationships within therelational database The 1:M Relationship
  • 4.
    Relationships within therelational database  The 1:M Relationship The 1:M relationship is the relational database norm.
  • 5.
    Relationships within therelational database  The 1:M Relationship • Each painting is painted by one and only one painter, but each painter could have painted many paintings.
  • 6.
    Relationships within therelational database  The 1:M Relationship • There is only one row in the PAINTER table for any given row in the PAINTING table, but there are may be many rows in the PAINTING table for any given row in the PAINTER table. • The 1:M relationship is found in any database environment. Students in a typical college or university will discover that each COURSE can generate many CLASSes but that each CLASS refers to only one COURSE.
  • 7.
    Relationships within therelational database  The 1:M Relationship The ERM( entity relationship model) for the 1:M relationship between COURSE and CLASS.
  • 8.
    Relationships within therelational database  The 1:M Relationship
  • 9.
    Relationships within therelational database  The 1:M Relationship • Each COURSE can have many CLASSes, but each CLASS references only one COURSE • There will be only one row in the COURSE table form any given row in the COURSE table.
  • 10.
    Relationships within therelational database The 1:1 Relationship
  • 11.
    Relationships within therelational database  The 1:1 Relationship • As the 1:1 label implies, in this relationship, one entity can be related to only one other entity, and vise versa. • For example, one department chair-a professor-can chair only one department, and one department can have only one department chair. • The entities PROFESSOR and DEPARTMENT thus exhibit a 1:1 relationship.
  • 12.
    Relationships within therelational database  The 1:1 Relationship The basic relationship modeled in Figure 3.22
  • 13.
    Relationships within therelational database  The 1:1 Relationship As you examine the tables in Figure 3.23, note that there
  • 14.
    Relationships within therelational database 1. Each professor is a Tiny College employee. Therefore, the professor identification is through the EMP_Num. 2. The 1:1 PROFESSOR chairs DEPARTMENT relationship is implemented by having the EMP_NUM fereign key in the DEPARTMENT table. 3. Also note that the PROFESSOR table contains the DEPT_CODE foreign key to implement the 1:M DEPARTMENT employs PROFESSOR relationship.
  • 15.
    Relationships within therelational database • The preceding “PROFESSOR chairs DEPARTMENT example illustrates a proper 1:1 relationship. In fact, the use of 1:1 relationship ensures that two entity sets are not placed in the same table when they should not be. However, the existence of 1:1 relationship sometimes means that the entity components were not defined properly. It could indicate that the two entities actually belong in the same table!
  • 16.
    Relationships within therelational database The M:N RELATIONSHIP
  • 17.
    Relationships within therelational database • A many-to-many (M:N) relationship is not supported directly in the relational environment. However, M:N relationships can be implemented by creating a new entity in 1:M relationships with the original entities.
  • 18.
    Relationships within therelational database
  • 19.
    Relationships within therelational database • To explore the many-to-many (M:N) relationship, consider a rather typical college environment in which each STUDENT can take CLASSes and each CLASS can contain many STUDENTs. The ER model in Figure 3.24 show the M:N relationship.
  • 20.
    Relationships within therelational database • Each CLASS can have many STUDENTs, and each STUDENT can take many CLASSes. • There can be many rows in the CLASS table for any given row in the STUDENT table, and there can be many rows in the STUDENT table for any given row in the class • To examine the M:N relationship more closely, imagine a small college with two students, each of whom takes three classes.
  • 21.
    Relationships within therelational database  The M:N relationship should not be implemented for two god reasons: The tables create many redundancies. For example, note that the STU_NUM values occur many times in the STUDENT table. • Given the structure and contents of the two tables, the relational operations become very complex and are likely to lead to system efficiency errors and output errors.
  • 22.
    Relationships within therelational database
  • 23.
    Relationships within therelational database  Fortunately, the problems inherent in the many-to-many (M:N) relationship can easily be avoided by creating a composite entity (also referred to as a bridge entity or an associative entity). Because such a table is used to link the tables that were originally related in an M:N relationship, the composite entity structure includes-as foreign keys – at least the primary keys of the tables that are to be linked.
  • 24.
    Relationships within therelational database
  • 25.
    Relationships within therelational database  The ENROLL table yields the required M:N to 1:M conversion. Observe that the composite entity represented by the ENROLL table must contain at least the primary keys of the CLASS and STUDENTS tables (CLASS_CODE and STU_NUM, respectively) for which it serves as a connector. Also note that the STUDENT and CLASS tables now contain only one row per entity. The ENROLL table contains multiple occurrences of the foreign key values, but those controlled redundancies are incapable of producing anomalies as long as referential integrity is enforced.
  • 26.
    Relationships within therelational database •As you can examine the figure, note that composite entity named ENROLL represents the linking table between STUDENT and CLASS.
  • 27.
    Relationships within therelational database •This figure shows the expanded ERM, including the expanded 1:M relationship between COURSE and CLASS
  • 28.
    Relationships within therelational database
  • 29.
    Prepared BY: •HARRY OCHINANG •JANE CATALLA •RONN RIVERA