5. RELATIONSHIPS AND THE RELATIONSHIPS TOOL

The Relationships Tool is used to define relationships between tables based on common fields. Relationships defined using the Relationships Tool are important as they help ensure integrity of data, and they provide us with default join criteria for queries involving more than one table. In this section we will use the University and the Library databases in our examples.

Consider the University database we have been using that contains a Department table and a Course table. These two tables have the deptCode field in common:

      • In the Department table, deptCode is the primary key and is used to identify a specific department.
      • In the Course table, the deptCode field is a part of the primary key and indicates the department to which the course belongs.

To ensure that a row in Course is related to an existing row in Department we can use the Relationships Tool to define a relationship between these two tables based on this common field. Using a diagram, we can illustrate this connection between these two tables:

     Figure 5.1: Showing a relationship between two tables

In this situation we say that deptCode in Course is a foreign key referencing the deptCode field in Department.

Now, consider the Library database:

      • The Loan table has a callNo field and so does the Book table; the callNo field identifies a specific book.
      • The Loan table has an id field and so does the Member table; the id field identifies an individual member.

In the Library database we can establish a relationship between Loan and Book based on the callNo field, and a second relationship between Loan and Member based on the id field. Using a diagram, we can illustrate these two relationships:

                   Figure 5.2: Showing relationships involving three tables

The Loan table has two foreign keys, callNo and id:

      • The callNo field in Loan is a foreign key referencing the primary key (callNo) in Book.
      • The id field in Loan is a foreign key referencing the primary key (id) in Member.

License

Share This Book