محاضرة 3
Database Relationships Implementation
أنواع العلاقات one-to-one و one-to-many و many-to-many وتطبيقها عملياً داخل Access.
ملخص المحاضرة
Lecture 3: Database Relationships Implementation
1. The Concept of Relationships
In a relational database, data is split into multiple tables to avoid Redundancy (duplicate data) and ensure Consistency. These tables are then linked using Relationships.
- Foreign Key (FK): A field in one table that refers to the Primary Key in another table. The relationship is built by connecting the PK to the FK.
2. Types of Relationships
- One-to-One (1:1):
- Definition: One record in Table A relates to exactly one record in Table B.
- Example: An
Employeetable and aPayrolltable (assuming one payroll record per employee). OrPersonandPassport. - Usage: Often used to split a table with too many fields for security or organization.
- One-to-Many (1:M):
- Definition: One record in Table A relates to multiple records in Table B.
- Example:
CustomerandOrders. One customer makes many orders, but an order belongs to only one customer. - Implementation: The PK of the "One" side is added as a FK in the "Many" side.
- Many-to-Many (M:N):
- Definition: Multiple records in Table A relate to multiple records in Table B.
- Example:
StudentandCourses. - Implementation: Cannot be done directly. Requires a third Junction Table (e.g.,
Enrollments) containing FKs from both sides.
3. Implementing One-to-One in Access
- Scenario: Linking
Employee(PK:Employee_ID) withCars(if each employee gets one car). - Steps:
- Go to Database Tools tab -> Click Relationships.
- Show Table: Select both tables (
Employee,Cars) and click Add. - Create Link: Drag the
Employee_ID(PK) from the Employee table and drop it onto theEmployee_ID(FK) in the Cars table. - Edit Relationships Dialog:
- Verify the relationship type says "One-to-One".
- Check "Enforce Referential Integrity": This ensures you cannot add a car for an employee who doesn't exist.
- Click Create. A line with
1on both ends will appear connecting the tables.
محتويات المحاضرة
اختبر فهمك للمحاضرة
تصحيح فوري بالذكاء الاصطناعي