One-to-One Relationship Design in SQL
📖 Scenario: You are designing a simple database for a company. Each employee has exactly one company car assigned to them. You need to create tables to store employee details and their assigned car details, ensuring a one-to-one relationship between employees and cars.
🎯 Goal: Create two tables, Employees and CompanyCars, with a one-to-one relationship between them using a foreign key constraint.
📋 What You'll Learn
Create a table called
Employees with columns EmployeeID (primary key) and Name (text).Create a table called
CompanyCars with columns CarID (primary key), Model (text), and EmployeeID (foreign key).Ensure the
EmployeeID column in CompanyCars enforces a one-to-one relationship with Employees.Add a unique constraint on
EmployeeID in CompanyCars to prevent multiple cars assigned to the same employee.💡 Why This Matters
🌍 Real World
One-to-one relationships are common in databases when each entity in one table corresponds to exactly one entity in another, such as employees and their assigned company cars.
💼 Career
Understanding how to design and enforce one-to-one relationships is important for database design roles, backend development, and data modeling tasks.
Progress0 / 4 steps