1. Learning Outcomes#
या chapter नंतर तुम्हाला:
Parent TableआणिChild Tableम्हणजे काय ते समजेल- दोन tables मधील data logically connect करण्यासाठी
Foreign Keyका लागते ते समजेल Foreign Keyकोणत्या column ला reference करते ते ओळखता येईलReferential Relationshipम्हणजे काय ते समजेल- valid आणि invalid foreign key values मधील फरक समजेल
Foreign Keyमुळे चुकीचा किंवा orphan data कसा टाळला जातो ते समजेल- basic
FOREIGN KEY ... REFERENCES ...syntax समजेल - practical database मध्ये foreign key कुठे वापरली जाते ते समजेल
- foreign key बद्दलच्या common beginner mistakes ओळखता येतील
- interview मध्ये foreign key बद्दल basic professional answer देता येईल
2. दोन Tables वेगळे असले तरी त्यांचा संबंध असू शकतो#
समजा आपण एका company साठी database बनवत आहोत.
आपल्याकडे employees आहेत.
तसेच company मध्ये वेगवेगळे departments आहेत.
उदाहरणार्थ:
| department_id | department_name |
|---|---|
| 10 | Development |
| 20 | Testing |
| 30 | HR |
ही departments table आहे.
आता employees:
| employee_id | employee_name | department_id |
|---|---|---|
| 101 | Amit | 10 |
| 102 | Sneha | 20 |
| 103 | Rahul | 10 |
ही employees table आहे.
पहिल्यांदा पाहताना या दोन स्वतंत्र tables वाटतात.
पण employees.department_id आणि departments.department_id यांच्यामधून त्या जोडलेल्या आहेत.
उदाहरणार्थ:
Amit चा department_id = 10.
departments table मध्ये 10 म्हणजे Development.
म्हणजे Amit हा Development department मध्ये आहे.
ही connection database ला व्यवस्थित समजावून सांगण्यासाठी Foreign Key वापरली जाते.
Foreign Key चा मुख्य उपयोग एका table मधील record दुसऱ्या table मधील valid record शी जोडण्यासाठी होतो.
3. Foreign Key ची गरज का पडते?#
आता एक महत्त्वाचा प्रश्न.
आपण employees table मध्ये department_id column ठेवला आहेच.
मग foreign key वेगळी कशाला?
चला foreign key नसल्यावर काय होऊ शकते ते पाहू.
आपल्या departments table मध्ये फक्त हे departments आहेत:
| department_id | department_name |
|---|---|
| 10 | Development |
| 20 | Testing |
| 30 | HR |
पण कोणीतरी employee insert करताना असे लिहिले:
INSERT INTO employees (employee_id, employee_name, department_id)
VALUES (104, 'Priya', 999);इथे Priya चा department_id आहे 999.
पण departments table मध्ये 999 नावाचा department अस्तित्वातच नाही.
Database ने ही value स्वीकारली तर आपल्या data ची अवस्था अशी होईल:
"Priya department 999 मध्ये आहे."
आणि department 999 कुठे आहे?
कुठेच नाही.
म्हणजे employee record एका अस्तित्वात नसलेल्या department कडे point करत आहे.
अशा data वर report काढताना, application बनवताना किंवा analytics करताना गोंधळ होऊ शकतो.
मराठीत म्हण आहे, "पायाचाच पत्ता नाही आणि वर मजला बांधायला निघालो."
इथेही तसेच झाले.
Department अस्तित्वात नाही, पण employee त्याच्याशी जोडला गेला.
Foreign Key ही problem थांबवण्यास मदत करते.
4. Definition#
A foreign key is a column or set of columns in one table that references a key in another table and helps maintain a valid relationship between the related records.
Foreign Key म्हणजे एका table मधील असा column, जो दुसऱ्या संबंधित table मधील valid record ला reference करतो.
सोप्या भाषेत सांगायचे तर foreign key database ला सांगते:
"या column मध्ये येणारी value त्या दुसऱ्या table मध्ये खरोखर अस्तित्वात असली पाहिजे."
5. Parent Table म्हणजे काय?#
Foreign key समजण्यासाठी आधी Parent Table समजून घेऊ.
आपल्या example मध्ये:
departmentsही parent table आहे.
कारण employees मधील department_id values या table मधील departments वर अवलंबून आहेत.
उदाहरण:
| department_id | department_name |
|---|---|
| 10 | Development |
| 20 | Testing |
| 30 | HR |
इथे department_id प्रत्येक department ची ओळख आहे.
सामान्यतः foreign key दुसऱ्या table मधील PRIMARY KEY किंवा इतर eligible unique key ला reference करते.
आपल्या example मध्ये:
department_idहा departments table चा primary key आहे.
ज्या table मधील key ला दुसरी table reference करते, त्या context मध्ये तिला Parent Table म्हणतो.
6. Child Table म्हणजे काय?#
आता employees table पाहू.
| employee_id | employee_name | department_id |
|---|---|---|
| 101 | Amit | 10 |
| 102 | Sneha | 20 |
| 103 | Rahul | 10 |
या table मध्ये department_id आहे.
हा column departments table मधील department_id ला reference करतो.
म्हणून आपल्या example मध्ये employees ही Child Table आहे.
ज्या table मध्ये foreign key ठेवलेली असते, त्या relationship च्या context मध्ये तिला Child Table म्हणतो.
एक छोटी गंमत लक्षात ठेवा:
Parent म्हणतो:
"आधी माझ्याकडे valid department असू दे."
Child म्हणतो:
"ठीक आहे, मग मी त्या department चा ID वापरतो."
Database म्हणतो:
"दोघांनी नीट संबंध ठेवला तर माझं आयुष्य सुखाचं."
7. Parent आणि Child यांचा basic flow#
या relationship कडे visually पाहू.
Rendering diagram…
Diagram could not be rendered.
Flow departments table पासून सुरू होतो.
त्या table मध्ये department_id आहे.
employees table मधील department_id foreign key म्हणून त्याला reference करते.
त्यामुळे employee कोणत्या valid department शी जोडलेला आहे हे database ला समजते.
इथे मुख्य गोष्ट अशी:
employees.department_id मध्ये कोणताही random department ID टाकणे योग्य नाही.
तो parent table मधील valid department शी संबंधित असला पाहिजे.
8. Foreign Key तयार करण्यापूर्वी Tables समजून घेऊ#
आपण दोन tables तयार करू.
पहिली parent table:
CREATE TABLE departments (
department_id INT PRIMARY KEY,
department_name VARCHAR(100) NOT NULL
);इथे:
department_id INT PRIMARY KEYयाचा अर्थ प्रत्येक department कडे unique department_id असेल.
उदाहरणार्थ:
10
20
30आता child table तयार करू:
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
employee_name VARCHAR(100) NOT NULL,
department_id INT,
FOREIGN KEY (department_id)
REFERENCES departments(department_id)
);इथे foreign key चा महत्त्वाचा भाग आहे:
FOREIGN KEY (department_id)
REFERENCES departments(department_id)हे step-by-step समजून घेऊ.
FOREIGN KEY (department_id)#
हा भाग database ला सांगतो:
"employees table मधील department_id हा foreign key आहे."
REFERENCES departments(department_id)#
हा भाग database ला सांगतो:
"या foreign key ची value departments table मधील department_id ला reference करेल."
म्हणजे पूर्ण अर्थ:
employees.department_idहा column:
departments.department_idयाच्याशी जोडलेला आहे.
9. Foreign Key Relationship कसा काम करतो?#
समजा parent table मध्ये हा data आहे:
INSERT INTO departments (department_id, department_name)
VALUES
(10, 'Development'),
(20, 'Testing'),
(30, 'HR');आता:
SELECT * FROM departments;Expected Result#
| department_id | department_name |
|---|---|
| 10 | Development |
| 20 | Testing |
| 30 | HR |
आता employee insert करू:
INSERT INTO employees (employee_id, employee_name, department_id)
VALUES (101, 'Amit', 10);हे valid आहे.
का?
कारण Amit साठी आपण:
department_id = 10दिला.
आणि parent departments table मध्ये department 10 आधीपासून अस्तित्वात आहे.
Database ला relationship valid दिसतो.
Rendering diagram…
Diagram could not be rendered.
इथे database logically check करते की referenced department उपलब्ध आहे का.
तो आहे.
म्हणून child record valid relationship तयार करू शकतो.
10. Invalid Foreign Key Value#
आता आपण वेगळे काहीतरी करून पाहू.
INSERT INTO employees (employee_id, employee_name, department_id)
VALUES (102, 'Sneha', 999);आपल्या departments table मध्ये department 999 नाही.
Relationship असा दिसेल:
Rendering diagram…
Diagram could not be rendered.
आपल्या current data नुसार answer आहे:
Noम्हणून foreign key constraint असताना database सामान्यतः हा insert reject करेल.
Exact error message कोणता दिसेल हे वापरलेल्या database system वर अवलंबून बदलू शकते.
पण problem एकच आहे:
Child record एका unavailable parent record ला reference करण्याचा प्रयत्न करत आहे.
Foreign key column मध्ये योग्य data type आहे म्हणून कोणतीही value चालेल असे समजू नका.
Data type योग्य असणे आणि referenced value valid असणे या दोन वेगळ्या गोष्टी आहेत.
11. Referential Relationship म्हणजे काय?#
आता Referential Relationship समजून घेऊ.
Referential हा शब्द सुरुवातीला मोठा वाटतो.
पण concept सोपा आहे.
एका table मधील value दुसऱ्या table मधील record कडे reference करते.
आपल्या example मध्ये:
employees.department_idहा:
departments.department_idकडे reference करतो.
म्हणून दोन tables मध्ये referential relationship तयार होते.
उदाहरण:
Amit
department_id = 10आणि:
10 = Developmentम्हणून database मधील दोन वेगळ्या records मध्ये meaningful connection तयार झाली.
12. Referential Integrity#
Foreign key सोबत एक महत्त्वाचा term तुम्हाला अनेकदा ऐकायला मिळेल:
Referential Integrity.
सोप्या भाषेत:
Related tables मधील references valid ठेवण्याचा नियम म्हणजे referential integrity.
उदाहरणार्थ:
जर employee म्हणत असेल:
माझा department_id = 20तर संबंधित department खरोखर उपलब्ध असायला हवा.
नाहीतर data logically inconsistent होईल.
Rendering diagram…
Diagram could not be rendered.
यामुळे database मध्ये "नाव आहे पण गाव नाही" असा data तयार होण्याची शक्यता कमी होते.
Foreign key फक्त tables जोडण्यासाठी नसते.
ती related data चा logical correctness टिकवून ठेवण्यासही मदत करते.
13. Foreign Key म्हणजे Primary Key नाही#
हा beginner confusion खूप common आहे.
Primary Key आणि Foreign Key एकच गोष्ट नाहीत.
| Primary Key | Foreign Key |
|---|---|
| Table मधील record identify करण्यासाठी वापरली जाते | दुसऱ्या related record ला reference करण्यासाठी वापरली जाते |
| साधारणपणे unique असते | Foreign key values repeat होऊ शकतात |
| एका table ची identity तयार करण्यात मदत करते | Tables मधील connection तयार करण्यात मदत करते |
employee_id हे example असू शकते |
department_id हे employees table मधील example असू शकते |
आपल्या employees table मध्ये:
employee_idहा employee identify करतो.
तर:
department_idहा employee कोणत्या department शी related आहे ते सांगतो.
उदाहरण:
| employee_id | employee_name | department_id |
|---|---|---|
| 101 | Amit | 10 |
| 102 | Rahul | 10 |
| 103 | Sneha | 20 |
इथे employee_id values unique आहेत.
पण department_id = 10 दोन employees साठी आले आहे.
ते चुकीचे नाही.
कारण foreign key चे काम employee uniquely identify करणे नाही.
तिचे काम valid department ला reference करणे आहे.
Foreign Key unique असायलाच पाहिजे असा नियम नाही.
14. Foreign Key Column कोणत्या Table मध्ये असतो?#
आपल्या example मध्ये:
departmentsहा parent आहे.
employeesहा child आहे.
Foreign key कुठे आहे?
employees table मध्ये.
department_id INTआणि constraint:
FOREIGN KEY (department_id)
REFERENCES departments(department_id)म्हणून beginner shortcut म्हणून असे लक्षात ठेवू शकता:
Child table मध्ये foreign key असते आणि ती parent table मधील संबंधित key ला reference करते.
हा shortcut basic examples साठी खूप उपयोगी आहे.
15. Parent Record आधी का लागतो?#
समजा नवीन Finance department तयार करायचा आहे.
त्याचा ID आहे:
40Finance मध्ये Neha नावाची employee आहे.
आपण आधी employee insert करण्याचा प्रयत्न केला:
INSERT INTO employees (employee_id, employee_name, department_id)
VALUES (104, 'Neha', 40);पण department 40 parent table मध्ये अजून नाही.
Foreign key enabled असल्यास operation fail होऊ शकतो.
योग्य sequence:
पहिले department तयार करा:
INSERT INTO departments (department_id, department_name)
VALUES (40, 'Finance');नंतर employee:
INSERT INTO employees (employee_id, employee_name, department_id)
VALUES (104, 'Neha', 40);आता reference valid आहे.
Rendering diagram…
Diagram could not be rendered.
यात लक्षात ठेवण्यासारखी गोष्ट:
Child record ला parent information वर अवलंबून राहावे लागत असेल, तर referenced parent value उपलब्ध असणे महत्त्वाचे आहे.
16. एक Practical Example: Orders आणि Customers#
Company example समजला.
आता e-commerce example घेऊ.
आपल्याकडे customers आहेत:
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
customer_name VARCHAR(100) NOT NULL
);आणि orders:
CREATE TABLE orders (
order_id INT PRIMARY KEY,
order_amount DECIMAL(10, 2) NOT NULL,
customer_id INT,
FOREIGN KEY (customer_id)
REFERENCES customers(customer_id)
);इथे:
customers ही parent table.
orders ही child table.
orders.customer_id ही foreign key.
ती reference करते:
customers.customer_idसमजा:
INSERT INTO customers (customer_id, customer_name)
VALUES (1, 'Riya');नंतर:
INSERT INTO orders (order_id, order_amount, customer_id)
VALUES (5001, 2500.00, 1);Order 5001 customer 1 शी जोडला गेला.
Customer 1 म्हणजे Riya.
म्हणून application ला समजते:
Order 5001 belongs to Riyaजर कुणी असा order insert केला:
INSERT INTO orders (order_id, order_amount, customer_id)
VALUES (5002, 1800.00, 999);आणि customer 999 अस्तित्वात नसेल, तर foreign key हा invalid reference रोखण्यास मदत करते.
17. Foreign Keys Real Projects मध्ये कुठे दिसतात?#
Foreign keys फक्त interview question नाहीत.
Production databases मध्ये त्यांचा उपयोग related business data connect करण्यासाठी मोठ्या प्रमाणावर होतो.
उदाहरणार्थ:
orders.customer_id
Customer आणि त्याचे orders जोडण्यासाठी.
employees.department_id
Employee आणि department जोडण्यासाठी.
payments.order_id
Payment कोणत्या order शी संबंधित आहे हे सांगण्यासाठी.
comments.post_id
Comment कोणत्या post शी संबंधित आहे हे सांगण्यासाठी.
students.course_id
Student च्या course-related record ला संबंधित course शी जोडण्यासाठी.
Concept सर्व ठिकाणी जवळजवळ तोच आहे:
एका record मधील reference दुसऱ्या valid record कडे point करतो.
18. Foreign Key नसल्यास काय problem होऊ शकते?#
समजा application bug मुळे खालील data आला:
| order_id | customer_id |
|---|---|
| 5001 | 1 |
| 5002 | 2 |
| 5003 | 85792 |
पण customers table मध्ये आहेत फक्त:
| customer_id | customer_name |
|---|---|
| 1 | Riya |
| 2 | Aman |
Order 5003 कोणाचा?
Customer 85792 अस्तित्वात नाही.
हा record logically problem निर्माण करतो.
अशा प्रकारच्या record ला सामान्य भाषेत orphan reference सारखे समजू शकता—record आहे, पण त्याचा expected related parent उपलब्ध नाही.
Foreign key constraints अशा invalid references टाळण्यात मदत करतात.
Foreign key वापरण्याचा मुख्य फायदा म्हणजे application code वरच पूर्णपणे अवलंबून न राहता database level वरही relationship validity enforce करता येते.
19. Foreign Key म्हणजे JOIN नाही#
हा आणखी एक common confusion आहे.
Foreign Key आणि JOIN वेगळे concepts आहेत.
Foreign key:
Tables मधील relationship चा rule define करते.
JOIN:
Query लिहिताना related tables मधून data एकत्र retrieve करण्यासाठी वापरला जातो.
उदाहरणार्थ foreign key सांगते:
employees.department_id references departments.department_idपण employee आणि department name एकत्र दाखवण्यासाठी query मध्ये JOIN वापरला जाऊ शकतो.
दोन्ही related आहेत.
पण दोन्ही एकच नाहीत.
"Foreign key आहे म्हणजे SQL automatically joined result दाखवेल" असे समजू नका.
Foreign key relationship enforce करते. Data retrieve करताना कोणती query वापरायची ते वेगळे ठरते.
20. Foreign Key बद्दल आणखी एक महत्त्वाची गोष्ट#
Foreign key declaration चा basic pattern असा दिसतो:
FOREIGN KEY (child_column)
REFERENCES parent_table(parent_column)आपल्या example मध्ये:
FOREIGN KEY (department_id)
REFERENCES departments(department_id)इथे स्वतःला तीन प्रश्न विचारा:
- Foreign key कोणत्या child column वर आहे?
- ती कोणत्या parent table ला reference करते?
- त्या parent table मधील कोणत्या column ला reference करते?
या तीन प्रश्नांची उत्तरे आली की foreign key syntax समजणे खूप सोपे होते.
21. Example वाचण्याची योग्य पद्धत#
ही syntax पाहा:
FOREIGN KEY (customer_id)
REFERENCES customers(customer_id)ती पाठ करू नका.
वाचा:
"customer_id हा माझ्या current child table मधील foreign key आहे."
"तो customers table ला reference करतो."
"त्या table मधील customer_id शी त्याची relationship आहे."
Syntax मग naturally लक्षात राहते.
घोकंपट्टी केली तर दोन दिवसांनी REFERENCES कुठे लावायचे यावर पंचायत बसते.
Concept समजला तर syntax स्वतः जागेवर बसते.
22. Common Beginner Mistakes#
Mistake 1: Parent record नसताना child record insert करणे#
उदाहरण:
INSERT INTO employees (employee_id, employee_name, department_id)
VALUES (105, 'Kiran', 500);जर department 500 उपलब्ध नसेल, तर reference invalid आहे.
पहिले referenced parent value अस्तित्वात आहे का ते पहा.
Mistake 2: Foreign Key आणि Primary Key एकच समजणे#
Primary key record identify करते.
Foreign key relationship तयार करते.
दोघांची जबाबदारी वेगळी आहे.
Mistake 3: Foreign Key unique असते असे समजणे#
अनेक child records एकाच valid parent value ला reference करू शकतात.
उदाहरण:
Amit -> Department 10
Rahul -> Department 10त्यामुळे foreign key value repeat होणे स्वतःमध्ये error नाही.
Mistake 4: फक्त समान column name असल्याने relationship तयार झाली असे समजणे#
दोन tables मध्ये department_id नावाचा column आहे म्हणून database foreign key relationship आपोआप तयार करत नाही.
Relationship explicitly define केलेली असावी.
उदाहरण:
FOREIGN KEY (department_id)
REFERENCES departments(department_id)Mistake 5: Foreign Key फक्त diagram साठी आहे असे समजणे#
Foreign key database constraint म्हणून actual data consistency maintain करण्यात मदत करते.
ती फक्त design document मधील arrow नाही.
23. छोटा Knowledge Check#
employees.department_id हा foreign key असेल आणि तो departments.department_id ला reference करत असेल, तर parent table कोणती?Show Answer
departments ही parent table आहे.
कारण तिच्या department_id ला child table reference करते.
Foreign key value repeat होऊ शकते का?Show Answer
हो.
Foreign key चा उद्देश unique record identify करणे नसून valid parent record ला reference करणे आहे.
Foreign key चा मुख्य फायदा काय?Show Answer
Related tables मधील references valid ठेवण्यास आणि invalid relationship data टाळण्यास foreign key मदत करते.
24. Hands-On Practice#
Practice 1#
Requirement
आपल्याकडे खालील parent table आहे:
CREATE TABLE categories (
category_id INT PRIMARY KEY,
category_name VARCHAR(100) NOT NULL
);products table तयार करा.
त्यात:
product_idproduct_namecategory_id
हे columns असावेत.
category_id ने categories.category_id ला reference केले पाहिजे.
Your Task
योग्य foreign key सहित products table तयार करणारी SQL query लिहा.
HintShow Hint
Child table मधील:
category_idहा column:
categories(category_id)ला reference करेल.
SolutionShow Solution
CREATE TABLE products (
product_id INT PRIMARY KEY,
product_name VARCHAR(100) NOT NULL,
category_id INT,
FOREIGN KEY (category_id)
REFERENCES categories(category_id)
);Explanation
products.category_id हा child column आहे.
तो categories.category_id या parent key ला reference करतो.
त्यामुळे product ला दिलेला category reference valid category शी जोडता येतो.
Practice 2#
Requirement
categories table मध्ये फक्त हे IDs आहेत:
1
2
3खालील insert पाहा:
INSERT INTO products (product_id, product_name, category_id)
VALUES (101, 'Keyboard', 8);Your Task
ही operation foreign key constraint मुळे का fail होऊ शकते ते स्पष्ट करा.
HintShow Hint
category_id = 8 parent table मध्ये आहे का ते पहा.
SolutionShow Solution
Category 8 parent categories table मध्ये उपलब्ध नाही.
म्हणून products.category_id = 8 हा invalid reference आहे.
Foreign key constraint database ला असा inconsistent child record reject करण्यास मदत करते.
Explanation
Column चा data type योग्य असला तरी referenced value parent table मध्ये valid असणे आवश्यक आहे.
25. Interview Preparation#
What is a foreign key in SQL?Show Answer
A foreign key is a column or set of columns in a child table that references a key in a related parent table. It is used to establish relationships between tables and help maintain referential integrity.
Key Points:
- Connects related tables
- Exists on the referencing side of the relationship
- Usually references a primary key or another eligible unique key
- Helps prevent invalid references
Why interviewer asks this:
The interviewer wants to verify that you understand relational database design rather than only SQL query syntax.
Can a foreign key contain duplicate values?Show Answer
Yes. A foreign key can contain duplicate values because multiple child records may reference the same parent record.
Key Points:
- Foreign keys are not automatically unique
- Repeated values can represent multiple records associated with the same parent
Why interviewer asks this:
This checks whether you understand the difference between primary keys and foreign keys.
What is the difference between a primary key and a foreign key?Show Answer
A primary key uniquely identifies a row within its own table. A foreign key references a key in a related table and is primarily used to establish and enforce a relationship between records.
Key Points:
- Primary key identifies
- Foreign key references
- Primary key values are unique
- Foreign key values may repeat
Why interviewer asks this:
This is a fundamental relational database concept and is frequently used to test database basics.
26. या Chapter मधून काय लक्षात ठेवायचे?#
Foreign Keyदोन related tables मधील valid reference तयार करण्यासाठी वापरली जाते.- ज्या table मधील key reference केली जाते ती त्या relationship मध्ये
Parent Tableअसते. - ज्या table मध्ये foreign key असते ती
Child Tableअसते. - Child table मधील foreign key value ने संबंधित valid parent record कडे point केले पाहिजे.
- Foreign key invalid references टाळून referential integrity maintain करण्यात मदत करते.
- Foreign key आणि primary key यांची कामे वेगळी आहेत.
- Foreign key values repeat होऊ शकतात.
- समान column names असणे म्हणजे foreign key relationship आपोआप तयार होणे नाही.
FOREIGN KEY (...) REFERENCES ... (...)हा basic syntax pattern आहे.- Concept समजून syntax वाचा; फक्त syntax पाठ करू नका.
Foreign key बद्दल उत्तर देताना तीन गोष्टी स्पष्ट सांगता आल्या पाहिजेत: it references another related key, it establishes a relationship, and it helps maintain referential integrity.