محاضرة 4
One-to-Many Relationship & Lookup Wizard
تطبيق عملي لعلاقة One-to-Many باستخدام Lookup Wizard ومثال الـ Customers/Orders.
ملخص المحاضرة
Lecture 4: One-to-Many Relationship & Lookup Wizard
1. Deep Dive: One-to-Many (1:M)
This is the most common relationship type.
- Example Scenario: A
Customerstable and anOrderstable.Customers(PK:ID)Orders(PK:ID, FK:Customer_ID)
- Logic: We want to assign a specific customer to every order placed.
2. Using the Lookup Wizard
Instead of manually typing the Customer ID into the Orders table (which causes errors), Access provides the Lookup Wizard. It creates a dropdown menu in the Foreign Key field that pulls data from the Primary Key table.
3. Practical Implementation Steps
- Prepare Tables: Ensure
Customerstable is populated. Create theOrderstable in Design View. - Select Foreign Key: In the
Orderstable, go to theCustomer_IDfield. - Data Type Selection: Instead of selecting 'Number', select Lookup Wizard from the dropdown list.
- Wizard Steps:
- Step 1: Choose "I want the lookup field to get the values from another table or query." -> Next.
- Step 2: Select the source table (
Customers). -> Next. - Step 3: Select the fields you want to see in the dropdown (e.g.,
ID,Customer_Name). -> Next. - Step 4: Sort order (optional). -> Next.
- Step 5: Hide Key Column (recommended to keep checked usually, but ensure you know which is the bound column). -> Next.
- Step 6: Enable Data Integrity: Check "Enable Data Integrity". This prevents "orphan records" (orders without valid customers). You can choose "Restrict Delete" (prevents deleting a customer who has orders).
- Finish: Save the table. Access will ask to create relationships; click Yes.
4. Verification
- Go to Database Tools -> Relationships.
- You will see a relationship line connecting
Customers(1 side) toOrders(infinity/many side). - Open the
Orderstable in Datasheet view; theCustomer_IDfield will now be a dropdown list showing customer names.