Relational Algebra in DBMS Explained in Hindi | Complete Guide with Examples
📌 Introduction
Database Management System (DBMS) modern software development का एक बहुत important part है। जब हम database से data retrieve करते हैं, तब हमें structured और efficient तरीके की जरूरत होती है। इसी purpose को fulfill करने के लिए Relational Algebra का use किया जाता है।
Relational Algebra एक formal query language है जो हमें यह बताती है कि data को database से कैसे retrieve करना है।
यह mainly दो types की query languages में से एक है:
- Procedural Query Language → क्या data चाहिए + कैसे प्राप्त करना है
- Non-Procedural Query Language → केवल क्या data चाहिए
👉 Relational Algebra procedural language का हिस्सा है।
📌 What is Relational Algebra?
Relational Algebra एक mathematical approach है जिसका उपयोग relational databases पर operations perform करने के लिए किया जाता है।
यह operations tables (relations) पर काम करते हैं और result भी हमेशा एक table ही होता है।
👉 Example:
अगर हमारे पास Student नाम की table है, तो हम उस पर operations जैसे selection, projection, join आदि कर सकते हैं।
📌 Types of Operations in Relational Algebra
Relational Algebra में दो प्रकार के operations होते हैं:
1. Fundamental (Basic) Operations
- Selection (σ)
- Projection (π)
- Union (∪)
- Set Difference (−)
- Rename (ρ)
- Cartesian Product (×)
2. Derived Operations
- Join
- Intersection
- Division
🔹 1. Selection Operation (σ)
📌 Definition
Selection operation का उपयोग table से specific rows (records) निकालने के लिए किया जाता है।
👉 यह एक unary operation है (एक ही table पर काम करता है)
📌 Syntax:
σ (condition) (Relation)
👉 Example:
σ (Sid = 3) (Student)
📌 Explanation:
यह operation Student table से केवल वो records निकालेगा जिनका Sid = 3 है।
📌 Logical Connectors
Selection में हम logical operators का use कर सकते हैं:
- AND (∧)
- OR (∨)
- NOT (¬)
👉 Example:
σ (Sid = 3 ∧ FN = "Pankaj") (Student)
🔹 2. Projection Operation (π)
📌 Definition
Projection का उपयोग table से specific columns (attributes) निकालने के लिए किया जाता है।
👉 यह भी unary operation है।
📌 Syntax:
π (A1, A2, A3, …) (Relation)
📌 Example:
π (SN, FN) (Student)
👉 यह केवल student name और father name return करेगा।
📌 Important Note:
Projection automatically duplicate values को remove करता है।
👉 इसलिए यह SQL के DISTINCT जैसा behavior करता है।
🔹 3. Rename Operation (ρ)
📌 Definition
Rename operation का उपयोग table या attributes के नाम बदलने के लिए किया जाता है।
📌 Syntax:
ρ NewName (Relation)
👉 Example:
ρ Student_Info (Student)
🔹 4. Set Operations
Set operations relational algebra का बहुत important हिस्सा हैं।
📌 Types:
- Union (∪)
- Intersection (∩)
- Difference (−)
📌 Condition:
दो tables पर set operations apply करने के लिए:
- Number of attributes same होने चाहिए
- Data types same होने चाहिए
🔹 5. Union Operation (∪)
📌 Definition:
Union दो tables के records को combine करता है और duplicate values remove करता है।
📌 Syntax:
R ∪ S
📌 Properties:
- Commutative → R ∪ S = S ∪ R
- Associative → (R ∪ S) ∪ T = R ∪ (S ∪ T)
🔹 6. Intersection Operation (∩)
📌 Definition:
Intersection common records return करता है।
📌 Syntax:
R ∩ S
👉 केवल वही values आएंगी जो दोनों tables में मौजूद हैं।
🔹 7. Set Difference (−)
📌 Definition:
Difference operation एक table से दूसरे table के records को हटाता है।
📌 Syntax:
R − S
👉 R में जो records हैं लेकिन S में नहीं हैं।
📌 Important:
- यह commutative नहीं है
- R − S ≠ S − R
🔹 8. Cartesian Product (×)
📌 Definition:
Cartesian product दो tables को combine करता है।
👉 हर row को दूसरी table की हर row से combine करता है।
📌 Formula:
Total Rows = m × n
जहाँ:
- m = first table rows
- n = second table rows
📌 Example:
Student × Course
👉 अगर Student में 4 rows और Course में 4 rows हैं → output = 16 rows
📌 Disadvantage:
- Time complexity बहुत high होती है
- Result बहुत बड़ा हो जाता है
👉 इसलिए Join का use किया जाता है
🔹 9. Join Operation (⨝)
Join relational algebra का सबसे powerful operation है।
👉 यह Cartesian Product + condition होता है।
🔸 Types of Join
1. Theta Join (⨝θ)
👉 Condition based join
Syntax:
R ⨝θ S
Example:
Student ⨝ Sid > Stid Course
2. Equi Join
👉 Equality condition पर join
Example:
Student ⨝ Sid = Stid Course
3. Natural Join
👉 Automatically common columns पर join करता है
👉 Duplicate columns remove कर देता है
🔸 Outer Joins
1. Left Outer Join
👉 Left table के सभी records + matched records
2. Right Outer Join
👉 Right table के सभी records + matched records
3. Full Outer Join
👉 दोनों tables के सभी records
🔹 10. Aggregate Functions
Aggregate functions एक set of values पर काम करते हैं और एक single value return करते हैं।
📌 Types:
- MIN
- MAX
- AVG
- SUM
- COUNT
👉 यह NULL values को ignore करते हैं।
🔹 NULL Value
NULL का मतलब होता है:
- Unknown value
- Missing value
👉 Example:
5 + NULL = NULL
5 × NULL = NULL
📌 Why Relational Algebra is Important?
Relational Algebra DBMS का backbone है।
इसके फायदे:
✔ Query optimization में मदद करता है
✔ SQL को समझने में आसान बनाता है
✔ Mathematical foundation देता है
✔ Efficient data retrieval
📌 Relational Algebra vs SQL
| Feature | Relational Algebra | SQL |
|---|---|---|
| Type | Procedural | Non-Procedural |
| Use | Internal DBMS | User Query |
| Complexity | High | Easy |
📌 Real-Life Example
मान लो आपके पास एक Student database है:
आपको चाहिए:
👉 उन students की list जिनका name "Ayushi" है
तो Relational Algebra में:
σ (SN = "Ayushi") (Student)
📌 Exam Tips (Very Important 🔥)
✔ Selection = rows filter
✔ Projection = columns filter
✔ Join = combine tables
✔ Union = combine records
✔ Intersection = common data
✔ Difference = subtract
📌 Common Mistakes
❌ Union बिना compatibility के use करना
❌ Projection में duplicates expect करना
❌ Join condition भूल जाना
❌ Cartesian product unnecessarily use करना
📌 Conclusion
Relational Algebra DBMS का core concept है जो हमें data को efficiently retrieve और manipulate करने में मदद करता है।
अगर आप इसे अच्छे से समझ लेते हैं, तो:
✅ SQL आसान हो जाता है
✅ Interview clear करना आसान हो जाता है
✅ Database design strong हो जाता है
💬 Leave a Comment & Rating