Table relationships in Microsoft Access connect two or more tables using related fields. Relationships help organize database information, reduce unnecessary duplication, and maintain consistency between related records.
A table relationship establishes a connection between records in different tables. Usually, a primary key in one table is connected to a matching foreign key in another table.
Students Table
| StudentID | StudentName | CourseID |
|---|---|---|
| 101 | Rahul Kumar | 1 |
| 102 | Priya Singh | 2 |
| 103 | Amit Kumar | 1 |
Courses Table
| CourseID | CourseName |
|---|---|
| 1 | ADCA |
| 2 | Tally Prime |
In this example, CourseID connects the Students table to the Courses table. Rahul and Amit are enrolled in the course identified by CourseID 1.
If the course name is stored repeatedly for every student, changing a course name may require updating many records.
Course information can be stored in one Courses table. Student records can refer to the appropriate course using CourseID.
A primary key uniquely identifies each record in a table. Its values cannot be duplicated or left null.
Example: StudentID in the Students table.
Students
--------------------------
StudentID (Primary Key)
StudentName
CourseID
A foreign key is a field that refers to a key in another table. It is used to establish a relationship between the tables.
Courses
--------------------------
CourseID (Primary Key)
CourseName
Students
--------------------------
StudentID (Primary Key)
StudentName
CourseID (Foreign Key)
In a one-to-one relationship, each record in one table corresponds to at most one record in the other table, and vice versa.
Example: An employee table connected to a separate employee-details table where each employee has one matching details record.
In a one-to-many relationship, one record in the first table can be related to multiple records in the second table. Each record in the second table refers to at most one record in the first table through that key.
Example: One course can have many students.
In a many-to-many relationship, multiple records in one table can relate to multiple records in another table. This is normally implemented using a junction table.
Example: Students can enroll in multiple courses, and each course can have multiple students.
| Relationship | Example |
|---|---|
| One-to-One | Employee and employee details |
| One-to-Many | Course and students |
| Many-to-Many | Students and courses through an enrollment table |
One-to-many relationships are common in database applications. A parent record can be associated with multiple child records.
DepartmentID — Primary Key
DepartmentID — Foreign Key
The DepartmentID in Employees refers to the DepartmentID in Departments. Several employees may share the same DepartmentID.
Microsoft Access provides a Relationships window where you can connect tables visually.
Referential integrity helps ensure that foreign-key values refer to valid records in the related table.
If CourseID 1 and CourseID 2 exist in the Courses table, a student can refer to either course. A student should not refer to CourseID 99 when that course does not exist, if referential integrity is enforced and the relationship requires a matching course.
When supported and enabled, cascade update propagates a change to a referenced key to matching foreign-key values in related records.
When enabled, deleting a parent record can also delete related child records. This should be used carefully because it can remove many records.
| Option | Effect |
|---|---|
| Cascade Update | Updates related foreign-key values when the referenced key changes. |
| Cascade Delete | Deletes related child records when the parent record is deleted. |
A many-to-many relationship is commonly implemented by creating a third table called a junction table or linking table.
Students Table
StudentID (Primary Key)
StudentName
Courses Table
CourseID (Primary Key)
CourseName
Enrollments Table
StudentID (Foreign Key)
CourseID (Foreign Key)
EnrollmentDate
Each enrollment record connects one student to one course. Multiple enrollment records allow a student to take multiple courses and a course to have multiple students.
Together, these two relationships implement the many-to-many association between Students and Courses.
The Relationships window allows you to review the connections between tables.
Removing a relationship line removes the defined relationship, not the table records themselves. Always verify the effect before making changes.
Solution: Check the field data types, field sizes, and key definitions.
Solution: Check for unmatched foreign-key values, null values where not allowed, and incompatible field definitions.
Solution: Ensure the primary key uniquely identifies every record. A field with duplicate values cannot serve as a standard primary key.
Solution: Verify that the foreign-key values match the intended primary-key values and that your query or form uses the correct relationship.
Solution: Review Cascade Delete settings and restore data from a backup if needed.
Create related Students and Courses tables in Microsoft Access.
1. Which key uniquely identifies each record in a table?
2. Which relationship allows one course to be associated with many students?
3. What does referential integrity help maintain?