One-to-One (1:1) Relationships
One-to-One (1:1) Relationships & Foreign Key Placement
In relational database engineering, relationships define how records in one table correlate with records in another. A One-to-One (1:1) relationship means that a single record in Table A is associated with at most one record in Table B, and vice versa.
While beginners often ask: "Why not just put all columns in a single table?", experienced database architects know that 1:1 relationships are critical for security isolation, performance optimization, and vertical partitioning.
1. Architectural Justifications for 1:1 Tables
Why split data into two tables linked in a 1:1 relationship?
- 1Security & Sensitive Data Isolation: You can store public user profiles in
users(readable by all services) and isolate sensitive fields (tax ID, KYC documents, banking details) inuser_tax_detailswith strict table-level access permissions. - 2Performance & Vertical Partitioning: In MySQL
InnoDB, reading narrow tables is significantly faster. Storing frequently read columns (username, email) in one table and rarely read wide columns (PDF resumes, JSON metadata) in a child table keeps your main table's pages compact in the Buffer Pool. - 3Sparse Columns: If only 2% of users have enterprise billing profiles, keeping those 15 billing columns in the main table would leave 98% of rows filled with empty
NULLvalues.
2. Implementing a 1:1 Relationship in MySQL
A 1:1 relationship is enforced by placing a Foreign Key in the dependent table and applying a UNIQUE constraint on that foreign key column!
DDL Implementation:
3. Alternative: Shared Primary Key Architecture
Instead of generating a separate surrogate key (passport_id), the child table can use the exact same Primary Key as the parent table, doubling as both Primary Key and Foreign Key:
4. Querying 1:1 Relationships with Joins
5. Best Practices & Common Pitfalls
- Do Not Forget the UNIQUE Constraint: If you add a foreign key without a
UNIQUEconstraint, the database engine will allow multiple child rows per parent, inadvertently creating a One-to-Many (1:N) relationship! - Cascading Deletions: Always declare
ON DELETE CASCADEon 1:1 auxiliary detail tables so that deleting a user automatically cleans up their associated preferences and passport records.
Multiple Choice Questions
1. What database constraint must be added to a Foreign Key column to enforce a strict One-to-One (1:1) relationship?
A. NOT NULL only B. UNIQUE C. CHECK D. DEFAULT Answer: B Explanation: Applying a UNIQUE constraint on a foreign key column guarantees that no parent row can be referenced by more than one child record, ensuring a 1:1 cardinality.
2. What is a primary architectural justification for splitting customer data into a 1:1 relationship rather than storing everything in one table?
A. MySQL prohibits tables with more than 5 columns B. Isolating sensitive or rarely accessed attributes (vertical partitioning) to improve security and buffer pool memory efficiency C. 1:1 relationships eliminate the need for primary keys D. Foreign keys are faster than reading single tables Answer: B Explanation: Vertical partitioning isolates sensitive columns for tighter security access control and keeps frequently accessed core rows compact in RAM cache.
3. In a Shared Primary Key 1:1 architecture, what role does the child table's primary key play?
A. It only generates random numbers B. It functions simultaneously as both the table's Primary Key and as the Foreign Key pointing to the parent C. It acts as an auto-incrementing surrogate key D. It disables indexes Answer: B Explanation: In shared primary key designs, the child's primary key is identical to the parent's primary key and is declared as a foreign key referencing the parent.
4. Which referential action should generally be attached to a 1:1 dependent profile table when the parent user is deleted?
A. ON DELETE RESTRICT B. ON DELETE CASCADE C. ON DELETE NO ACTION D. ON DELETE RESTART Answer: B Explanation: Because 1:1 auxiliary records have no reason to exist without their parent entity, ON DELETE CASCADE ensures clean garbage collection of dependent rows.
5. What type of SQL join should be used to retrieve all users along with their 1:1 passport details, even if some users have not yet submitted a passport?
A. INNER JOIN B. LEFT JOIN C. CROSS JOIN D. FULL OUTER JOIN Answer: B Explanation: A LEFT JOIN retains all records from the left table (users) regardless of whether a matching record exists in the right table (user_passports).
One-to-Many (1:N) Relationships
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| None | One-to-Many (1:N) Relationships |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.