1. Learning Outcomes#
या chapter नंतर तुम्हाला:
- database मधील
relationshipम्हणजे नेमके काय ते समजेल - दोन tables मध्ये relationship का तयार करावी लागते ते समजेल
One-to-Onerelationship ओळखता आणि design करता येईलOne-to-Manyrelationship ओळखता आणि design करता येईलparentआणिchildtable यांचा अर्थ समजेलPrimary KeyआणिForeign Keyrelationship तयार करताना कसे काम करतात ते समजेलUNIQUEconstraint वापरून One-to-One relationship कशी enforce करता येते ते समजेल- real-world requirement पाहून योग्य relationship निवडता येईल
- चुकीची relationship design केल्यामुळे कोणत्या data problems येऊ शकतात ते ओळखता येईल
2. Relationship म्हणजे काय?#
समजा आपण college साठी database तयार करत आहोत.
आपल्याकडे वेगवेगळी माहिती असू शकते:
- students
- departments
- courses
- teachers
ही सगळी माहिती एकाच table मध्ये भरली तर table इतका गोंधळेल की परिस्थिती अशी होईल — "एक ना धड भाराभर चिंध्या."
म्हणून आपण वेगवेगळ्या गोष्टींसाठी वेगवेगळे tables तयार करतो.
उदाहरणार्थ:
departments
department_id
department_nameआणि:
employees
employee_id
employee_name
department_idआता प्रश्न असा आहे:
एखादा employee कोणत्या department मध्ये काम करतो हे database ला कसे समजणार?
इथे relationship उपयोगी पडते.
3. Definition#
A relationship defines how rows in one database table are associated with rows in another table.
relationship म्हणजे एका table मधील data चा दुसऱ्या table मधील data सोबत काय संबंध आहे हे database design मध्ये स्पष्ट करणे.
उदाहरणार्थ:
Rahul हा employee आहे.
त्याचा department_id = 10 आहे.
departments table मध्ये:
10 → ITअसे असेल तर आपल्याला समजते:
Rahul → IT Departmentइथे employee आणि department यांच्यामध्ये relationship तयार झाली.
4. Relationship ची गरज का पडते?#
समजा प्रत्येक employee च्या row मध्ये आपण department ची संपूर्ण माहिती ठेवली:
| employee_id | employee_name | department_name | department_location |
|---|---|---|---|
| 1 | Rahul | IT | Pune |
| 2 | Sneha | IT | Pune |
| 3 | Amit | IT | Pune |
इथे IT आणि Pune पुन्हा पुन्हा store होत आहे.
उद्या IT department चे location Mumbai झाले तर अनेक rows update कराव्या लागतील.
एक row update झाली आणि दुसरी राहिली तर data inconsistent होऊ शकतो.
त्याऐवजी department स्वतंत्र table मध्ये ठेवू शकतो:
departments#
| department_id | department_name | location |
|---|---|---|
| 10 | IT | Pune |
employees#
| employee_id | employee_name | department_id |
|---|---|---|
| 1 | Rahul | 10 |
| 2 | Sneha | 10 |
| 3 | Amit | 10 |
आता department ची माहिती एकाच ठिकाणी आहे.
Employees फक्त त्या department चा reference ठेवतात.
Relationship आपल्याला related data वेगवेगळ्या tables मध्ये व्यवस्थित ठेवून त्यांना logically connect करण्यास मदत करते.
5. दोन tables connect कसे होतात?#
इथे दोन महत्त्वाचे terms समजून घ्या.
Primary Key#
Primary Key म्हणजे table मधील प्रत्येक row uniquely identify करणारी value.
उदाहरण:
departments.department_idजर department_id = 10 असेल तर त्या ID ने एक specific department ओळखता येतो.
Foreign Key#
दुसऱ्या table मधील Primary Key ला refer करणाऱ्या column ला सामान्यतः Foreign Key म्हणून वापरले जाते.
उदाहरण:
employees.department_idहा column:
departments.department_idला refer करू शकतो.
म्हणजे employee row जणू database ला सांगते:
"माझा department कोणता ते जाणून घ्यायचे आहे? माझ्याकडे असलेला department_id departments table मध्ये बघ."
थोडक्यात:
departments.department_id
↓
employees.department_idहीच tables मधील connection तयार करण्याची मूलभूत कल्पना आहे.
6. One-to-One Relationship#
आता पहिला प्रकार पाहूया.
One-to-One म्हणजे एका table मधील एका row शी दुसऱ्या table मधील जास्तीत जास्त एक संबंधित row जोडलेली असते.
एक सोपे उदाहरण घेऊ.
आपल्याकडे:
usersआणि:
user_profilesअसे दोन tables आहेत.
आपला business rule असा आहे:
प्रत्येक user साठी एकच profile असू शकते.
उदाहरण:
User 1 Rahul → Profile 101
User 2 Sneha → Profile 102
User 3 Amit → Profile 103पण असे नसावे:
User 1 Rahul → Profile 101
User 1 Rahul → Profile 102
User 1 Rahul → Profile 103कारण मग एका user च्या अनेक profiles झाल्या.
ते One-to-One राहणार नाही.
One-to-One visually समजून घेऊया#
Rendering diagram…
Diagram could not be rendered.
या diagram मध्ये:
USERहा एक table आहे.USER_PROFILEहा दुसरा table आहे.user_idवापरून profile specific user सोबत जोडली जाते.- एका user शी एक profile जोडलेली आहे.
- एका profile चा संबंध एका user शी आहे.
सोप्या भाषेत:
एक user → एक profile
हा One-to-One relationship चा basic mental model आहे.
7. One-to-One SQL मध्ये कशी enforce करायची?#
आता आपण फक्त diagram पाहून थांबणार नाही.
Database ला हेही सांगावे लागेल की:
"एका user साठी दुसरी profile insert करू देऊ नकोस."
त्यासाठी UNIQUE constraint उपयोगी पडते.
पहिला table:
CREATE TABLE users (
user_id INT PRIMARY KEY,
name VARCHAR(100)
);दुसरा table:
CREATE TABLE user_profiles (
profile_id INT PRIMARY KEY,
user_id INT UNIQUE,
bio VARCHAR(255),
FOREIGN KEY (user_id) REFERENCES users(user_id)
);इथे सर्वात महत्त्वाची line आहे:
user_id INT UNIQUEuser_id हा Foreign Key आहे.
पण त्याच्यावर UNIQUE सुद्धा आहे.
याचा अर्थ एकाच user_id ची duplicate value user_profiles table मध्ये पुन्हा येऊ शकत नाही.
उदाहरणार्थ:
INSERT INTO users VALUES (1, 'Rahul');
INSERT INTO user_profiles
VALUES (101, 1, 'Java Developer');हे valid आहे.
पुन्हा:
INSERT INTO user_profiles
VALUES (102, 1, 'SQL Developer');करण्याचा प्रयत्न केला तर user_id = 1 आधीच वापरलेला असल्यामुळे database duplicate value स्वीकारणार नाही.
आपण नेमके काय केले?#
FOREIGN KEY ने profile कोणत्या user ची आहे हे सांगितले.
UNIQUE ने एका user साठी अनेक profile rows तयार होऊ दिल्या नाहीत.
म्हणून:
Foreign Key
+
UNIQUE
=
One-to-One rule enforce करण्याचा एक सामान्य मार्गफक्त Foreign Key लावल्यामुळे relationship आपोआप One-to-One होत नाही. एका parent value साठी एकच child row असावी हा rule enforce करणेही आवश्यक आहे.
8. UNIQUE नसेल तर काय होईल?#
हे खूप महत्त्वाचे आहे.
समजा आपण table असा तयार केला:
CREATE TABLE user_profiles (
profile_id INT PRIMARY KEY,
user_id INT,
bio VARCHAR(255),
FOREIGN KEY (user_id) REFERENCES users(user_id)
);इथे user_id वर UNIQUE नाही.
मग database अशा rows allow करू शकतो:
| profile_id | user_id | bio |
|---|---|---|
| 101 | 1 | Java Developer |
| 102 | 1 | SQL Developer |
| 103 | 1 | Backend Developer |
आता:
User 1
├─ Profile 101
├─ Profile 102
└─ Profile 103म्हणजे एका user सोबत अनेक profile rows जोडल्या गेल्या.
आपला original One-to-One business rule तुटला.
"Foreign Key आहे म्हणजे relationship One-to-One आहे" असे समजू नका. Relationship चा प्रकार business rule आणि database constraints दोन्हीवर अवलंबून असतो.
9. One-to-One कुठे वापरली जाऊ शकते?#
One-to-One प्रत्येक ठिकाणी वापरायची relationship नाही.
ती तेव्हा उपयुक्त ठरू शकते जेव्हा एका entity शी संबंधित अतिरिक्त माहिती स्वतंत्र table मध्ये ठेवायची असते.
उदाहरणे:
User आणि User Profile#
One User
→
One User ProfileEmployee आणि Employee Detail#
One Employee
→
One Employee DetailPerson आणि Passport#
एखाद्या simplified system मध्ये business rule असा असू शकतो:
One Person
→
One Passport Recordइथे एक गोष्ट कायम लक्षात ठेवा:
relationship हा real-world business rule वर आधारित असतो.
"हे example इंटरनेटवर One-to-One म्हटले आहे म्हणून माझ्या application मध्येही ते One-to-Oneच असेल" असे नसते.
तुमच्या system चा rule महत्त्वाचा.
10. One-to-Many Relationship#
आता सर्वात जास्त वापरल्या जाणाऱ्या relationships पैकी एक पाहूया.
समजा company मध्ये departments आहेत.
एका department मध्ये अनेक employees काम करू शकतात.
उदाहरण:
IT Department
→ Rahul
→ Sneha
→ Amitपण प्रत्येक employee एका specific department मध्ये आहे असे आपण या example मध्ये मानत आहोत.
मग relationship अशी होते:
एक department → अनेक employees
याला One-to-Many relationship म्हणतात.
11. One-to-Many visually समजून घेऊया#
Rendering diagram…
Diagram could not be rendered.
हा diagram डावीकडून समजून घेऊया.
DEPARTMENT हा parent side आहे.
EMPLOYEE हा अनेक records असू शकणारा side आहे.
एक department:
ITसोबत:
Rahul
Sneha
Amitअसे अनेक employee records जोडले जाऊ शकतात.
म्हणजे relationship चा mental model:
One Parent
→
Many Childrenआता याला SQL मध्ये पाहू.
12. One-to-Many SQL Design#
Parent table:
CREATE TABLE departments (
department_id INT PRIMARY KEY,
department_name VARCHAR(100)
);Child table:
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
employee_name VARCHAR(100),
department_id INT,
FOREIGN KEY (department_id)
REFERENCES departments(department_id)
);इथे employees.department_id वर UNIQUE नाही.
हे मुद्दाम आहे.
का?
कारण अनेक employees कडे same department ID असणे valid आहे.
उदाहरण:
INSERT INTO departments
VALUES (10, 'IT');आता:
INSERT INTO employees
VALUES (1, 'Rahul', 10);
INSERT INTO employees
VALUES (2, 'Sneha', 10);
INSERT INTO employees
VALUES (3, 'Amit', 10);या तिन्ही employees चा:
department_id = 10आहे.
हे duplicate दिसत असले तरी इथे ते चुकीचे नाही.
कारण प्रत्येक row वेगळ्या employee ची आहे.
तीन employees एकाच IT department मध्ये काम करत आहेत.
हीच आपली requirement होती.
Result#
| employee_id | employee_name | department_id |
|---|---|---|
| 1 | Rahul | 10 |
| 2 | Sneha | 10 |
| 3 | Amit | 10 |
इथे:
department_id 10तीनदा दिसतो.
म्हणून One-to-Many relationship तयार होते.
One-to-Many relationship मध्ये parent table मधील एक row child table मधील अनेक rows कडून refer केली जाऊ शकते.
13. Parent आणि Child म्हणजे काय?#
parent आणि child हे शब्द ऐकून family serial सुरू झाली असे समजू नका.
Database मध्ये त्यांचा साधा अर्थ आहे.
समजा:
departmentstable चा department_id:
employeestable refer करत आहे.
मग सामान्यतः:
departments → Parent
employees → Childअसे म्हणतो.
का?
कारण child row मध्ये parent चा reference असतो.
या example मध्ये:
employees.department_idहा:
departments.department_idला refer करतो.
Parent-child flow#
Rendering diagram…
Diagram could not be rendered.
या flow मध्ये आधी department record अस्तित्वात आहे.
उदाहरणार्थ:
department_id = 10
department_name = ITनंतर Rahul ची employee row तयार करताना:
department_id = 10ठेवतो.
त्यामुळे Rahul चा संबंध IT department शी तयार होतो.
सोप्या भाषेत:
Child row parent चा ID वापरून सांगते की ती कोणाशी संबंधित आहे.
14. Invalid parent reference म्हणजे काय?#
समजा departments table मध्ये फक्त:
10 → IT
20 → HRहे departments आहेत.
आता आपण employee insert केला:
INSERT INTO employees
VALUES (4, 'Neha', 999);पण:
department_id = 999असा department अस्तित्वातच नाही.
जर योग्य FOREIGN KEY constraint असेल तर database ही invalid relationship स्वीकारणार नाही.
कारण child record अशा parent कडे बोट दाखवत आहे जो अस्तित्वातच नाही.
जणू कोणी म्हणाले:
"मी Room Number 999 मध्ये राहतो."
आणि hotel मध्ये Room 999 आहेच नाही.
Receptionist म्हणेल:
"साहेब, आधी room तरी असू द्या!"
Database सुद्धा त्याच logic ने referential consistency जपतो.
Foreign Key चा महत्त्वाचा उपयोग म्हणजे child row ला valid parent row शी जोडणे.
15. One-to-One आणि One-to-Many मधला मुख्य फरक#
| प्रश्न | One-to-One | One-to-Many |
|---|---|---|
| एका parent शी किती child rows? | जास्तीत जास्त एक | अनेक |
| Child-side Foreign Key duplicate होऊ शकतो? | सामान्यतः नाही | होऊ शकतो |
UNIQUE constraint उपयोगी? |
होय, अनेक designs मध्ये | साधारणपणे child foreign key वर नाही |
| Example | User → User Profile | Department → Employees |
हे लक्षात ठेवण्याचा सर्वात सोपा मार्ग:
One-to-One:
एका व्यक्तीसाठी एक locker
One-to-Many:
एका classroom मध्ये अनेक studentsपण analogy फक्त कल्पना समजण्यासाठी आहे.
Actual database relationship नेहमी तुमच्या application's business rule नुसार ठरवा.
16. Relationship ठरवताना योग्य प्रश्न कोणता?#
हा प्रश्न विचारू नका:
"या दोन tables मध्ये कोणती relationship popular आहे?"
त्याऐवजी business requirement ला प्रश्न विचारा.
समजा entities आहेत:
Department
Employeeस्वतःला विचारा:
एका Department शी किती Employees जोडले जाऊ शकतात?
उत्तर:Show Answer
Manyमग दुसऱ्या बाजूने विचारा:
एका Employee चा किती Departments शी संबंध असू शकतो?
आपल्या current business rule नुसार:
Oneम्हणून:
Department → Employee
One → Manyहे One-to-Many आहे.
दुसरे example#
Entities:
User
User Profileप्रश्न:
एका User ला किती Profiles allow आहेत?
Oneएका Profile चा किती Users शी संबंध आहे?
Oneम्हणून:
User → User Profile
One → Oneहे One-to-One आहे.
Relationship चे नाव table च्या नावावरून ठरत नाही. Cardinality — म्हणजे एका बाजूच्या record शी दुसऱ्या बाजूचे किती records जोडले जाऊ शकतात — यावर relationship ठरते.
17. Real-World Examples#
Customer आणि Orders#
एक customer अनेक orders करू शकतो.
Customer
→ Order 1
→ Order 2
→ Order 3म्हणून:
Customer → Orders
One → ManyCategory आणि Products#
एका category मध्ये अनेक products असू शकतात.
Electronics
→ Laptop
→ Mobile
→ Headphonesम्हणून:
Category → Products
One → ManyEmployee आणि Employee Profile#
जर application मध्ये प्रत्येक employee साठी फक्त एक profile record ठेवायचा असेल:
Employee → Employee Profile
One → OneCountry आणि Cities#
एका country मध्ये अनेक cities असतात.
Country → Cities
One → Manyइथे एक pattern दिसतोय:
Relationship समजण्यासाठी आधी tables पाहण्यापेक्षा business sentence तयार करा.
उदाहरण:
"One department can have many employees."
या एका sentence मधूनच relationship जवळपास स्पष्ट होते.
18. Common Beginner Mistakes#
Mistake 1: Foreign Key दिसला म्हणजे One-to-Many#
नेहमीच नाही.
Foreign Key tables जोडतो.
Relationship One-to-One आहे की One-to-Many हे constraints आणि business rule ठरवतात.
Mistake 2: One-to-One मध्ये UNIQUE विसरणे#
जर child-side reference duplicate होऊ शकत असेल तर एका parent शी अनेक child rows जोडल्या जाऊ शकतात.
मग intended One-to-One rule database enforce करणार नाही.
Mistake 3: Parent आणि Child उलटे समजणे#
ज्या table मध्ये Foreign Key आहे तो सामान्यतः child असतो.
ज्या table च्या key ला तो refer करतो तो parent असतो.
Mistake 4: Real-world requirement न विचारता relationship बनवणे#
पहिले requirement समजा.
नंतर database design करा.
नाहीतर अवस्था होते — "आधी लगीन कोंढाण्याचं, मग रायबाचं."
म्हणजे design आधी आणि requirement नंतर असे करू नका.
Mistake 5: Duplicate Foreign Key म्हणजे नेहमी error#
One-to-Many मध्ये duplicate Foreign Key values पूर्णपणे योग्य असू शकतात.
उदाहरण:
department_id = 10अनेक employee rows मध्ये असणे अपेक्षितच आहे.
19. Practical Modelling Strategy#
जेव्हा दोन entities दिसतील तेव्हा ही विचार करण्याची सवय लावा.
Step 1: दोन entities ओळखा#
उदाहरण:
Department
EmployeeStep 2: पहिल्या entity च्या एका record कडे पाहा#
One DepartmentStep 3: त्याच्याशी दुसऱ्या entity चे किती records जोडले जाऊ शकतात ते विचारा#
Many EmployeesStep 4: उलट दिशेने विचार करा#
One Employee
→
One DepartmentStep 5: Business rule database मध्ये enforce करा#
Primary Key, Foreign Key आणि आवश्यक constraints वापरून design तयार करा.
हा विचार करण्याचा approach पाठ केल्यास relationship पाठांतराने नाही तर logic ने समजू लागते.
20. Interview Preparation#
What is a one-to-one relationship in a relational database?Show Answer
A one-to-one relationship is a relationship in which one row in one table is associated with at most one corresponding row in another table, according to the application's business rules and constraints.
Key Points:
- One record maps to one corresponding record.
- A foreign key is commonly used to connect the tables.
- A UNIQUE constraint can be used to prevent multiple child rows from referencing the same parent.
Why interviewer asks this:
The interviewer wants to verify whether you understand database relationship cardinality rather than only Foreign Key syntax.
What is a one-to-many relationship?Show Answer
A one-to-many relationship allows one row in a parent table to be associated with multiple rows in a child table. The child table usually stores a foreign key referencing the parent table.
Key Points:
- One parent can have multiple children.
- Each child stores the parent's reference.
- Repeating the same foreign-key value across multiple child rows can be valid.
Why interviewer asks this:
This is one of the most common relationship patterns in relational database design.
What is the difference between one-to-one and one-to-many relationships?Show Answer
In a one-to-one relationship, one parent row can be associated with at most one corresponding child row. In a one-to-many relationship, one parent row can be associated with multiple child rows.
Key Points:
- The difference is primarily cardinality.
- One-to-one commonly requires uniqueness on the referencing column.
- One-to-many permits multiple child rows to reference the same parent.
Why interviewer asks this:
The interviewer wants to see whether you can convert business requirements into an appropriate relational model.
21. Hands-On Practice#
Practice 1#
Requirement
एका company मध्ये:
- एक department मध्ये अनेक employees असू शकतात.
- प्रत्येक employee एकाच department मध्ये काम करतो.
Your Task
Relationship चा प्रकार ओळखा आणि parent व child table सांगा.
HintShow Hint
एका department सोबत किती employee rows जोडल्या जाऊ शकतात याचा विचार करा.
SolutionShow Solution
ही One-to-Many relationship आहे.
Parent: departments
Child: employeesemployees table मध्ये department_id Foreign Key ठेवता येईल.
Explanation
एका department चा ID अनेक employee rows मध्ये वापरला जाऊ शकतो.
म्हणून:
One Department
→
Many EmployeesPractice 2#
Requirement
एका application मध्ये:
- प्रत्येक account साठी फक्त एक settings record असू शकतो.
- एक settings record फक्त एका account शी संबंधित असतो.
Your Task
Relationship ओळखा आणि SQL design मध्ये One-to-One rule कसा enforce कराल ते सांगा.
HintShow Hint
Foreign Key पुरेसा आहे का, की duplicate account reference रोखण्याचीही गरज आहे?
SolutionShow Solution
ही One-to-One relationship आहे.
उदाहरण:
CREATE TABLE accounts (
account_id INT PRIMARY KEY,
username VARCHAR(100)
);
CREATE TABLE account_settings (
settings_id INT PRIMARY KEY,
account_id INT UNIQUE,
theme VARCHAR(50),
FOREIGN KEY (account_id)
REFERENCES accounts(account_id)
);Explanation
account_id Foreign Key असल्यामुळे settings record account शी connect होतो.
UNIQUE असल्यामुळे एकाच account साठी अनेक settings rows तयार करता येत नाहीत.
Practice 3#
Requirement
खालील data पहा:
categories
1 → Books
2 → Electronicsproducts
101 → SQL Book → category 1
102 → Java Book → category 1
103 → Laptop → category 2
104 → Smartphone → category 2Your Task
categories आणि products यांच्यातील relationship ओळखा.
HintShow Hint
एका category ID ची किती product rows मध्ये पुनरावृत्ती झाली आहे ते पहा.
SolutionShow Solution
ही One-to-Many relationship आहे.
One Category
→
Many ProductsExplanation
category_id = 1 दोन products कडून refer केला आहे.
category_id = 2 सुद्धा दोन products कडून refer केला आहे.
म्हणून एका category शी अनेक product rows जोडलेल्या आहेत.
22. या Chapter मधून काय लक्षात ठेवायचे?#
relationshipदोन tables मधील related records कसे जोडलेले आहेत ते दर्शवते.Primary Keyrow uniquely identify करते.Foreign Keyदुसऱ्या table मधील key ला refer करून connection तयार करते.- One-to-One मध्ये एका record शी जास्तीत जास्त एक corresponding record जोडलेला असतो.
- One-to-One enforce करण्यासाठी Foreign Key सोबत
UNIQUEconstraint उपयुक्त ठरू शकते. - One-to-Many मध्ये एका parent row शी अनेक child rows जोडलेल्या असू शकतात.
- One-to-Many मध्ये same Foreign Key value अनेक child rows मध्ये येणे normal असू शकते.
- Foreign Key असणे आणि relationship type ठरणे या दोन वेगळ्या गोष्टी आहेत.
parenttable च्या record लाchildtable refer करते.- Relationship design नेहमी actual business rule वर आधारित असावी.
- योग्य relationship शोधण्यासाठी सर्वात चांगला प्रश्न आहे: "एका record शी दुसऱ्या बाजूचे किती records जोडले जाऊ शकतात?"
Relationship पाठ करू नका. Business rule ला "एकाशी किती?" हा प्रश्न विचारा. उत्तरातून database relationship naturally समजू लागेल.