A one-to-one relationship in SQL is a relationship where one record in one table is associated with exactly one record in another table.
In simple words:
One row from Table A can relate to only one row from Table B, and vice versa.
Why One-to-One Relationships are Important
Enterprise applications often need:
- Separate sensitive data
- Improve security
- Reduce table complexity
- Organize optional information
Without proper one-to-one design:
- Tables become too large
- Sensitive data becomes harder to manage
- Database structure becomes less maintainable
One-to-One Relationships Solve These Problems
By:
- Separating logically related information into different tables
Simple Real-Life Example
Think about:
- Person and Passport
Scenario
- One person has one passport
- One passport belongs to one person
Example
Naresh → Passport A12345 Rahul → Passport B56789
One-to-One Relationship Works Similarly
Each row:
- Maps to only one related row
One-to-One Relationship Internal Architecture
Users Table
|
|
User_Profile Table
Main Purpose of One-to-One Relationships
- Improve database organization
- Separate optional data
- Enhance security
- Improve normalization
How One-to-One Relationship Works
One-to-one relationships are implemented using:
- Primary key
- Unique foreign key
Example: Users Table
| user_id | user_name |
|---|---|
| 1 | Naresh |
| 2 | Rahul |
User_Profile Table
| profile_id | user_id | address | phone |
|---|---|---|---|
| 101 | 1 | Hyderabad | 9999999999 |
| 102 | 2 | Bangalore | 8888888888 |
Meaning
- One user has one profile
- One profile belongs to one user
SQL Table Creation Example
Users Table
CREATE TABLE users (
user_id INT PRIMARY KEY,
user_name VARCHAR(100)
);
User_Profile Table
CREATE TABLE user_profile (
profile_id INT PRIMARY KEY,
user_id INT UNIQUE,
address VARCHAR(200),
phone VARCHAR(20),
FOREIGN KEY (user_id)
REFERENCES users(user_id)
);
Why UNIQUE Constraint?
UNIQUE ensures:
- One user can appear only once in profile table
Invalid Example
user_id = 1 user_id = 1
Problem
- One user mapped to multiple profiles
UNIQUE Constraint Prevents This
- Maintains one-to-one relationship
One-to-One Relationship Query Flow
Users Table
|
v
Foreign Key Matching
|
v
User_Profile Table
|
v
Combined Result
Retrieve User with Profile
SELECT u.user_name,
p.address,
p.phone
FROM users u
JOIN user_profile p
ON u.user_id = p.user_id;
Result
| user_name | address | phone |
|---|---|---|
| Naresh | Hyderabad | 9999999999 |
| Rahul | Bangalore | 8888888888 |
One-to-One vs One-to-Many
| Feature | One-to-One | One-to-Many |
|---|---|---|
| Relationship | One ↔ One | One ↔ Many |
| Unique Constraint | Required | Not required |
| Example | User-Profile | Department-Employees |
One-to-One vs Many-to-Many
| Feature | One-to-One | Many-to-Many |
|---|---|---|
| Relationship Count | Single mapping | Multiple mappings |
| Junction Table | Not required | Required |
| Example | Person-Passport | Students-Courses |
Common Real-World Examples
- User ↔ Profile
- Person ↔ Passport
- Employee ↔ Parking Slot
- Student ↔ ID Card
- Customer ↔ Wallet
One-to-One in Banking Systems
Banking systems use one-to-one relationships for:
- Customer ↔ KYC details
- User ↔ Security settings
- Account ↔ Debit card
Why?
- Sensitive data separated securely
One-to-One in E-Commerce
E-commerce systems use one-to-one relationships for:
- User ↔ Wallet
- Order ↔ Invoice
- Product ↔ Inventory details
Example
One order has one invoice
One-to-One in Learning Platforms
Learning systems use one-to-one relationships for:
- Student ↔ Certificate
- User ↔ Preferences
- Trainer ↔ Bio profile
One-to-One in Microservices
Microservices architectures use one-to-one relationships for:
- User ↔ Authentication profile
- Account ↔ Security settings
- Service ↔ Configuration
Advantages of One-to-One Relationships
- Better normalization
- Improved security
- Cleaner database design
- Better optional data management
Disadvantages of One-to-One Relationships
- More joins required
- Additional tables needed
- Increased query complexity
Performance Considerations
One-to-one relationships may:
- Require JOIN operations
- Slightly increase query complexity
Optimization Techniques
- Index foreign keys
- Use proper normalization
- Avoid unnecessary one-to-one tables
Example Index
CREATE INDEX idx_user_profile ON user_profile(user_id);
One-to-One in JPA/Hibernate
JPA supports one-to-one relationships using:
- @OneToOne annotation
Example
@OneToOne @JoinColumn(name = "user_id") private User user;
Best Practices
- Use UNIQUE constraints properly
- Separate sensitive data carefully
- Avoid unnecessary table splitting
- Use indexing on foreign keys
- Maintain referential integrity
Common Interview Mistake
Many developers think:
- Primary key alone automatically creates one-to-one relationships
Reality
One-to-one relationships require:
- UNIQUE foreign key constraints
Related Learning Topics
- What is One-to-Many Relationship?
- What is Many-to-Many Relationship?
- What is a Foreign Key?
- What is Referential Integrity?
- Database Normalization with Example
Professional Interview Answer
A one-to-one relationship in SQL is a relationship where one record in one table is associated with exactly one record in another table. It is commonly implemented using a foreign key with a UNIQUE constraint to ensure that each record maps to only one related record. One-to-one relationships are widely used to separate optional, sensitive, or logically distinct data into separate tables while maintaining strong relational integrity. Examples include user and profile, person and passport, account and debit card, and order and invoice relationships. Enterprise systems such as banking platforms, e-commerce applications, ERP systems, learning management systems, and microservices architectures use one-to-one relationships for security, modularity, normalization, and maintainable database design.
Why Interviewers Like This Answer
- Clearly explains relationship structure
- Mentions UNIQUE constraint importance
- Includes normalization concepts
- Provides real-world enterprise examples
- Demonstrates strong relational database understanding
Frequently Asked Questions
What is a one-to-one relationship?
A relationship where one row in a table maps to exactly one row in another table.
How is one-to-one implemented in SQL?
Using a foreign key with a UNIQUE constraint.
Why use one-to-one relationships?
To separate optional or sensitive data and improve database organization.
What are examples of one-to-one relationships?
User-Profile, Person-Passport, Order-Invoice.
Why is UNIQUE constraint important?
It ensures only one related record exists for each parent record.