Computer Science और IT (कंप्यूटर साइंस)‹›⌂ 0:00 पढ़ाई

Module 10 · 29 मिनट पढ़ाई · मुफ़्त chapter

ER model और relational model

संक्षेप में

ER model entities, attributes और relationships को cardinality (1:1, 1:N, M:N) और participation के साथ दिखाता है। Relational model इन्हें tuples वाले tables में रखता है। Candidate key minimal superkey है, और n में k attributes वाली एक key से 2ⁿ⁻ᵏ superkeys बनती हैं। M:N relationship को अपना table चाहिए।

आप किस exam की तैयारी कर रहे हैं? हम आपके exam के practice और पिछले साल के सवाल दिखाएँगे।

शून्य से शुरू: register से database तक

Computer आने से पहले के railway reservation counter के बारे में सोचिए। Clerk के पास एक मोटा register होता है, और हर booking की line में यात्री का नाम, उम्र, पता, train number, train का नाम, तारीख़ और berth लिखा होता है। अगर रमेश पाँच यात्राएँ book करता है, तो उसका पता पाँच बार लिखा जाता है, और उस train के हर यात्री के साथ train का नाम फिर से लिखा जाता है।

अब मान लीजिए रमेश का घर बदल गया। Clerk को पाँचों lines ढूँढकर ठीक करनी होंगी, और अगर एक भी छूट गई, तो register एक ही आदमी के बारे में दो अलग बातें बताएगा। अगर दो clerks एक ही समय पर उसी register में लिखें, तो एक berth दो लोगों को मिल सकती है। Office में आग लग जाए, तो सब कुछ ख़त्म हो जाता है।

Database इन समस्याओं को हल करता है। यह आपस में जुड़े data का व्यवस्थित संग्रह है, और database management system (DBMS) वह software है जो इसे रखता है, इस पर पूछे गए सवालों का जवाब देता है और इसकी सुरक्षा करता है। DBMS हर बात को एक ही जगह रखता है, कई users को बिना टकराव के एक साथ काम करने देता है, और crash के बाद data वापस ला सकता है।

Tables बनाने से पहले designer असली दुनिया की एक तस्वीर बनाते हैं कि कौन-सी चीज़ें हैं और वे आपस में कैसे जुड़ी हैं। इसी तस्वीर को ER model कहते हैं, और इससे बनने वाले tables relational model बनाते हैं। यह chapter दोनों को सिखाता है।

यह क्यों ज़रूरी है? भारत का हर railway ticket, bank account और exam result किसी database में रखा है। ख़राब design में data दोहराया जाता है और आपस में मेल नहीं खाता, जबकि अच्छा design उसे सालों तक सही रखता है। इसीलिए हर exam में database का हिस्सा design के सवालों से शुरू होता है।

Entities और attributes के प्रकार

Entity असली दुनिया की ऐसी चीज़ है जिसे बाकी से अलग पहचाना जा सके, जैसे एक छात्र, एक train या एक bank account। एक जैसी entities का समूह, जैसे किसी college के सभी छात्र, entity set कहलाता है। हर entity को attributes से बताया जाता है, जैसे roll number, नाम और जन्म की तारीख़।

Attributes जोड़ों में आते हैं, और exam हर जोड़े को परखता है:

  • Simple attribute को और तोड़ा नहीं जा सकता, जैसे roll number, जबकि composite attribute के हिस्से होते हैं, जैसे Name पहले नाम और उपनाम से मिलकर बनता है।
  • Single-valued attribute की हर entity के लिए एक ही value होती है, जैसे जन्म की तारीख़, जबकि multivalued attribute की कई values हो सकती हैं, जैसे phone numbers।
  • Stored attribute database में रखा जाता है, जबकि derived attribute दूसरों से निकाला जाता है, जैसे जन्म की तारीख़ से Age।
  • Key attribute की value हर entity के लिए अलग होती है, जैसे roll number, इसलिए यह entity को पहचान लेता है।

ER diagram में entity set को rectangle से और attribute को उससे जुड़े ellipse से दिखाते हैं। Multivalued attribute के लिए double ellipse, derived attribute के लिए dashed ellipse बनता है, और key attribute के नीचे line खींची जाती है। Composite attribute एक ellipse होता है, जिससे उसके हिस्सों के छोटे ellipses जुड़े होते हैं।

कभी-कभी कोई value मौजूद नहीं होती, जैसे किसी छात्र का email address नहीं है या हमें अभी पता नहीं है। ऐसी value को NULL लिखते हैं, जिसका मतलब है "पता नहीं या लागू नहीं"। NULL, शून्य या ख़ाली शब्द के बराबर नहीं है।

यह क्यों ज़रूरी है? हर तरह का attribute tables में अपने अलग तरीके से जाता है। Derived attribute कभी store नहीं होता, composite attribute हिस्सों में खुल जाता है, और multivalued attribute के लिए पूरा नया table चाहिए। इसलिए attribute का प्रकार सही पहचानने से ही table का design तय होता है।

Relationships, cardinality और participation

Ratio मतलब उदाहरण
1:1 हर A अधिकतम एक B से, और हर B अधिकतम एक A से Department का एक Head
1:N एक A कई B से, लेकिन हर B अधिकतम एक A से Department में कई Employees
M:N हर A कई B से, और हर B कई A से Students और Courses

Entities अकेली नहीं रहतीं। Student किसी course में enrol करता है और employee किसी department में काम करता है। ऐसे संबंध को relationship कहते हैं, और एक जैसे relationships का समूह relationship set है। इसे diamond से दिखाते हैं, जो जुड़ने वाले rectangles से line से जुड़ा होता है।

Relationship set की degree उसमें शामिल entity sets की संख्या है। Binary relationship दो entity sets को जोड़ता है, और यही सबसे आम है। Ternary relationship तीन को जोड़ता है, जैसे Supplier, Project को Part देता है। Unary या recursive relationship किसी entity set को उसी से जोड़ता है, जैसे Employee, Employee को manage करता है, और यहाँ दोनों सिरे अलग-अलग roles निभाते हैं।

Relationship का अपना attribute भी हो सकता है। "Student, Course में enrol करता है" में grade न अकेले student का है और न अकेले course का; यह दोनों के जोड़े का है। इसलिए grade, Enrols का attribute है।

Binary relationship का cardinality ratio बताता है कि एक तरफ़ की एक entity दूसरी तरफ़ की अधिकतम कितनी entities से जुड़ सकती है। ऊपर की table में तीनों प्रकार उदाहरण के साथ दिए गए हैं।

Participation एक अलग सवाल का जवाब देता है: क्या हर entity को भाग लेना ही है? अगर हर employee को किसी न किसी department में काम करना ही है, तो Employee की participation total है, और इसे double line से दिखाते हैं। अगर कोई department बिना किसी project के भी रह सकता है, तो "project चलाता है" में उसकी participation partial है, और इसे single line से दिखाते हैं।

कुछ जगह दोनों बातें हर line पर एक (min, max) जोड़े के रूप में लिखी जाती हैं। जैसे, Works_in में Employee की तरफ़ (1, 1) का मतलब है कि हर employee कम से कम एक और ज़्यादा से ज़्यादा एक department में काम करता है। Minimum 0 हो तो participation partial है, और minimum 1 या उससे ज़्यादा हो तो total है।

यह क्यों ज़रूरी है? Cardinality और participation से ही तय होता है कि foreign key कहाँ जाएगी और वह NULL हो सकती है या नहीं। GATE के ज़्यादातर "कम से कम कितने tables" वाले सवाल असल में इन्हीं दो बातों के सवाल हैं।

Weak entity sets

कुछ entities अपने आप पहचानी नहीं जा सकतीं। एक company insurance के लिए हर employee के dependents का record रखती है। किसी dependent का नाम "आशा" है, लेकिन दो अलग employees की बेटी का नाम आशा हो सकता है। इसलिए अकेला नाम key नहीं है; सिर्फ़ "employee E101 की dependent आशा" ही अनोखी पहचान है।

ऐसे entity set को weak entity set कहते हैं। यह एक owner (identifying या strong) entity set पर identifying relationship के ज़रिए निर्भर रहता है। एक ही owner की weak entities को अलग करने वाला attribute, यहाँ dependent का नाम, partial key या discriminator कहलाता है।

ER notation यह सब साफ़ दिखाता है। Weak entity set double rectangle होता है, identifying relationship double diamond होता है, और partial key के नीचे dashed line होती है। Identifying relationship में weak entity की participation हमेशा total होती है, क्योंकि employee के बिना dependent का कोई अस्तित्व नहीं है।

Weak entity की key, owner की primary key और partial key को जोड़कर बनती है। इसलिए Dependent की key (EmpID, DepName) होती है। Identifying relationship आमतौर पर owner से weak entity की तरफ़ 1:N होता है, क्योंकि एक employee के कई dependents हो सकते हैं, लेकिन हर dependent एक ही employee का होता है।

यह क्यों ज़रूरी है? जहाँ भी data किसी का "अपना" होता है, वहाँ weak entities मिलती हैं: किसी hotel building के कमरे, किसी loan की किश्तें, किसी bill की lines। अगर आप owner की key भूल जाएँ, तो दो अलग records टकरा जाते हैं और database उन्हें अलग नहीं कर पाता।

Relational model: relations, tuples और domains

शब्द मतलब 4 columns और 60 rows वाले Student में
Degree (arity) Attributes की संख्या 4
Cardinality Tuples की संख्या 60
Schema नाम और attributes की list, यानी design Student(RollNo, Name, City, DOB)
Instance किसी समय पर मौजूद tuples आज की 60 rows

ER diagram एक चित्र है; computer relations रखता है। Relation बस एक नाम वाला table है, जैसे Student(RollNo, Name, City, DOB)। हर column एक attribute है, हर row एक tuple है, और किसी attribute की संभव values का set उसका domain है, जैसे phone वाले column के लिए "10 अंकों वाले numbers"।

किसी relation के आकार और रूप को चार शब्द बताते हैं। ऊपर की table में degree, cardinality, schema और instance, चारों का मतलब 4 columns और 60 rows वाले Student के उदाहरण से दिया गया है।

Relation tuples का एक set है, और इससे उसमें तीन गुण आते हैं। कोई दो tuples बिल्कुल एक जैसे नहीं हो सकते, rows के क्रम का कोई मतलब नहीं होता, और जब values attribute के नाम से पढ़ी जाएँ तो columns का क्रम भी मायने नहीं रखता। हर value atomic भी होती है, यानी domain की एक ही value, कोई list नहीं।

Schema बहुत कम बदलता है, शायद सालों में एक बार, जबकि instance हर बार बदलता है जब कोई row जुड़ती या हटती है। Exam में "relation" का मतलब instance और "relation schema" का मतलब design होता है, इसलिए सवाल ध्यान से पढ़िए।

यह क्यों ज़रूरी है? Relation एक set है, इसलिए database speed के लिए rows को आराम से आगे-पीछे कर सकता है, और queries sets की भाषा में लिखी जा सकती हैं। Atomic values अच्छे design की पहली शर्त भी हैं, और normal forms इसी पर खड़े होते हैं।

Keys: super, candidate, primary और foreign

Database ठीक एक tuple कैसे ढूँढता है? वह key का इस्तेमाल करता है। Superkey attributes का कोई भी ऐसा set है जिसकी values किसी दो tuples में कभी नहीं दोहरातीं। Student(RollNo, Name, City, DOB) में {RollNo} superkey है, और {RollNo, Name} तथा {RollNo, Name, City} भी superkeys हैं, क्योंकि unique set में attributes जोड़ने से वह unique ही रहता है।

Candidate key एक minimal superkey है: इसमें से कोई भी attribute हटाइए तो यह unique नहीं रहती। {RollNo, Name} superkey है लेकिन candidate key नहीं, क्योंकि अकेला RollNo ही काफ़ी है। अगर Aadhaar number भी रखा गया है, तो {Aadhaar} दूसरी candidate key है।

Designer एक candidate key को primary key चुनता है, और उसकी value कभी NULL नहीं हो सकती। बाकी candidate keys alternate keys कहलाती हैं। जो attribute कम से कम एक candidate key में आता है, उसे prime attribute कहते हैं, और बाकी non-prime हैं।

हल किया उदाहरण 1: superkeys गिनना। R(A, B, C, D) लीजिए, जिसकी इकलौती candidate key A है। हर superkey में A होना चाहिए, और B, C, D में से हर एक रह भी सकता है और हट भी सकता है, इसलिए 23=82^3 = 8 superkeys हैं। आम तौर पर nn attributes और kk attributes वाली एक candidate key से 2n−k2^{n-k} superkeys बनती हैं।

हल किया उदाहरण 2: दो candidate keys। मान लीजिए R(A, B, C, D) की candidate keys A और B हैं। A वाले sets 23=82^3 = 8 हैं, B वाले sets भी 8 हैं, और दोनों वाले sets 22=42^2 = 4 हैं। Inclusion-exclusion से जवाब 8+8−4=128 + 8 - 4 = 12 superkeys है।

Foreign key दो relations को जोड़ती है। Enrolment(RollNo, CourseID, Grade) में RollNo, Student को refer करता है। Referential integrity कहती है कि foreign key की हर value, referenced relation के किसी मौजूदा tuple की primary key से मिलनी चाहिए, या फिर NULL होनी चाहिए। Foreign key अपने ही relation को भी refer कर सकती है, जैसे Employee(EmpID, Name, ManagerID) में ManagerID, EmpID को refer करता है।

यह क्यों ज़रूरी है? Keys duplicate records को रोकती हैं, और foreign keys "अनाथ" records को रोकती हैं, जैसे किसी ऐसे छात्र के marks जो है ही नहीं। Superkeys गिनना और candidate keys पहचानना GATE के सबसे ज़्यादा पूछे जाने वाले एक-अंक के सवालों में आते हैं।

ER diagram को tables में बदलना

Relationship कैसे रखा जाता है दो entity sets के लिए tables
M:N दोनों keys (और relationship के attributes) वाला नया table, जिसकी key दोनों का जोड़ा है 3
1:N N वाली तरफ़ foreign key जोड़ी जाती है 2
1:1 किसी भी तरफ़ foreign key, बेहतर है total वाली तरफ़ 2
1:1, दोनों तरफ़ total दोनों entity sets एक table में मिला दिए जाते हैं 1

ER diagram तैयार होने के बाद कुछ तय नियम उसे tables में बदल देते हैं। शुरुआत entity sets से कीजिए:

  1. हर strong entity set अपने simple attributes के साथ एक table बनता है, और उसकी key उस table की primary key बनती है।
  2. Composite attribute को खोल दिया जाता है, इसलिए Name दो columns, FirstName और LastName, बन जाता है।
  3. Age जैसा derived attribute store ही नहीं किया जाता।
  4. हर multivalued attribute को अपना table मिलता है, जैसे StudentPhone(RollNo, Phone), जिसकी key दोनों columns मिलकर बनाते हैं।
  5. Weak entity set ऐसा table बनता है जिसमें owner की key भी होती है, जैसे Dependent(EmpID, DepName, Age), जिसकी primary key (EmpID, DepName) है।

इसके बाद relationships को map कीजिए, और यहाँ सब कुछ cardinality तय करती है। ऊपर की table बताती है कि M:N, 1:N, 1:1 और दोनों तरफ़ total वाले 1:1 relationship को कैसे रखा जाता है और दो entity sets के लिए कितने tables बनते हैं।

हल किया उदाहरण। Student(RollNo, Name) और Course(CourseID, Title) एक M:N relationship Enrols से जुड़े हैं, जिसका attribute Grade है, और Student का एक multivalued attribute Phone है। Tables ये बनते हैं: Student(RollNo, Name), Course(CourseID, Title), Enrols(RollNo, CourseID, Grade) जिसकी key (RollNo, CourseID) है, और StudentPhone(RollNo, Phone)। इसलिए कम से कम 4 tables चाहिए।

1:N में foreign key N वाली तरफ़ ही क्यों? हर employee एक ही department में काम करता है, इसलिए Employee की row में ठीक एक DeptID की जगह होती है। अगर EmpID को Department के अंदर रखें, तो एक cell में employees की list रखनी पड़ेगी, और इससे atomic values का नियम टूट जाएगा।

Ternary relationship, या दो से ज़्यादा degree वाला कोई भी relationship, हमेशा अपना अलग table बनाता है जिसमें उसकी सभी entity sets की keys होती हैं। "Manages" जैसा recursive 1:N relationship उसी table में foreign key बनता है, यानी Employee के अंदर ManagerID।

यह क्यों ज़रूरी है? GATE अक्सर एक छोटा ER diagram देकर कम से कम tables की संख्या पूछता है। Entity sets गिनिए, हर M:N relationship और हर multivalued attribute के लिए एक जोड़िए, और सिर्फ़ तब मिलाइए जब किसी 1:1 relationship में दोनों तरफ़ total participation हो।

Descriptive lens: लिखित paper यह कैसे पूछते हैं

IT officer पदों, राज्य Assistant Programmer परीक्षाओं और university tests के लिखित paper data modelling को चार तरह से पूछते हैं:

  1. किसी दी गई स्थिति, जैसे hospital, library या railway reservation system, का ER diagram बनाना, जिसमें keys, cardinality और participation दिखाए गए हों।
  2. दिए गए ER diagram को relations में बदलना और हर primary और foreign key बताना।
  3. शब्दों के जोड़ों को परिभाषित करना और उनमें अंतर बताना, जैसे weak और strong entity, superkey और candidate key, या total और partial participation।
  4. किसी design के फ़ैसले को सही ठहराना, जैसे multivalued attribute के लिए अलग table क्यों चाहिए।

अभ्यास सवाल (हमारे अपने, पिछले वर्षों के सवाल नहीं)।

  1. Members, books, books की copies और loans वाली college library का ER diagram बनाइए। अगर कोई weak entity है तो उसे और सभी cardinalities को दिखाइए। (15 अंक)
  2. अपने बनाए एक relation की मदद से superkey, candidate key, primary key और foreign key समझाइए। अगर R(A, B, C, D, E) की इकलौती candidate key A है, तो उसकी कितनी superkeys हैं? (10 अंक)
  3. Doctor, Patient और Ward entity sets वाले ER diagram को tables में बदलिए, जहाँ एक doctor कई patients का इलाज करता है और हर patient एक ward में रहता है। (10 अंक)

सवाल 1 का answer plan।

  • Member, Book और Copy को entity sets के रूप में पहचानिए, और समझाइए कि Copy weak है क्योंकि copy number 3 अलग-अलग किताबों में दोहराया जाता है।
  • Book की key ISBN रखिए और Copy की partial key CopyNo रखिए, ताकि उसकी पूरी key (ISBN, CopyNo) बने।
  • Loan को Member और Copy के बीच समय के साथ बदलने वाले M:N relationship के रूप में दिखाइए, जिसके attributes IssueDate और ReturnDate हैं।
  • Identifying relationship में Copy की participation को total और Loan में Member की participation को partial दिखाइए।
  • इससे बनने वाले tables लिखिए और foreign keys बताइए, ताकि examiner को दिखे कि design पूरा है।
  • अंत में एक line लिखिए कि यह design हर copy के लिए किताब की जानकारी दोहराने से कैसे बचाता है।

Quick revision

  • DBMS जुड़े हुए data को एक बार रखता है, उसे कई users के बीच सुरक्षित तरीके से बाँटता है और failure के बाद वापस लाता है।
  • Entity set rectangle है, attribute ellipse है, multivalued attribute double ellipse है, derived attribute dashed ellipse है, और key के नीचे line होती है।
  • Cardinality (1:1, 1:N, M:N) अधिकतम साथी बताती है, और participation (total या partial) न्यूनतम बताता है।
  • Weak entity के लिए double rectangle, double diamond और total participation होते हैं, और उसकी key, owner की key और partial key से बनती है।
  • Degree columns की संख्या है, और cardinality rows की संख्या है।
  • Candidate key minimal superkey है, और primary key वह चुनी हुई candidate key है जो NULL नहीं हो सकती।
  • nn attributes में kk attributes वाली एक candidate key से 2n−k2^{n-k} superkeys बनती हैं।
  • Foreign key की value किसी मौजूदा primary key value से मिलनी चाहिए या NULL होनी चाहिए।
  • M:N के लिए अलग table चाहिए, 1:N में foreign key N वाली तरफ़ जाती है, और दोनों तरफ़ total participation वाले 1:1 को मिलाया जा सकता है।
  • हर multivalued attribute एक table जोड़ता है, और derived attribute कोई table नहीं जोड़ता।

ख़ुद को परखें

इस chapter पर exam जैसे सवाल। जवाब चुनिए, फिर उसकी व्याख्या देखिए। 15 में से 0 हल किए

  1. अलग-अलग files की तुलना में DBMS मुख्य रूप से किस तरह मदद करता है?

  2. ER diagram में double ellipse क्या दिखाता है?

  3. Employee, Employee को manage करता है, यह किस degree का relationship है?

  4. Students और Courses, जहाँ एक student कई courses लेता है और एक course में कई students होते हैं, कैसा relationship है?

  5. हर employee को किसी department में काम करना ही है। Works_in में Employee की participation कैसी है?

  6. Weak entity set की primary key किससे बनती है?

  7. किसी relation में 5 attributes और 200 tuples हैं। उसकी degree और cardinality क्या हैं?

  8. Relational model में relation का कौन-सा गुण नहीं है?

  9. Student(RollNo, Name, Aadhaar) में RollNo और Aadhaar दोनों unique हैं। {RollNo, Name} क्या है?

  10. R(A, B, C, D, E) की इकलौती candidate key A है। Superkeys की संख्या कितनी है?

  11. R(A, B, C, D) की candidate keys सिर्फ़ A और B हैं। Superkeys की संख्या कितनी है?

  12. Referencing table में foreign key की value को क्या करना चाहिए?

  13. Student entity के multivalued attribute Phone को कैसे map करते हैं?

  14. M:N relationship वाले दो strong entity sets के लिए कम से कम कितने tables चाहिए?

  15. E1 और E2 एक 1:N relationship से जुड़े हैं, और दोनों तरफ़ participation partial है। कम से कम कितने tables चाहिए?

पिछले साल के सवाल (PYQ)

UPSC CSE के पिछले साल के सवाल यहाँ तभी दिखेंगे, जब हर सवाल को official question paper से शब्दशः मिला लिया जाएगा।

AI के साथ सीखें

ऐसा tutor जो बताने की जगह पूछता है, ऐसा साथी जो दूसरा पक्ष रखता है, और यह chapter आपके अपने ज़िले के उदाहरणों से।

Tutor को बताइए कहाँ अटके हैं। वह सवालों से रास्ता दिखाएगा और कुछ कोशिशों के बाद जवाब देगा।

🙋 Doubt पूछें या feedback दें

कहीं अटके, या कोई गलती दिखी? ExamHike team को लिखिए। आम तौर पर एक दिन में जवाब आता है, और जवाब आपके My progress page पर दिखेगा।

Doubt पूछने या feedback देने के लिए login करें →

अक्सर पूछे जाने वाले सवाल

Superkey और candidate key में क्या अंतर है?

Superkey attributes का कोई भी ऐसा set है जो tuple को अकेले पहचान ले। Candidate key minimal superkey है, जिसमें से कोई attribute हटाया नहीं जा सकता।

Weak entity set क्या है?

यह ऐसा entity set है जिसकी अपनी key नहीं होती, और इसे owner entity के ज़रिए पहचाना जाता है। इसकी key, owner की key और partial key से बनती है।

M:N relationship के लिए कितने tables चाहिए?

M:N relationship से जुड़े दो entity sets के लिए कम से कम तीन tables चाहिए, हर entity set का एक और relationship का एक।

क्या foreign key NULL हो सकती है?

हाँ, जब तक वह primary key का हिस्सा न हो। नहीं तो उसे referenced table की किसी मौजूदा primary key value से मिलना चाहिए।

इस module के और chapter: Databases (डेटाबेस)

Official स्रोत

इस chapter के तथ्य इन official स्रोतों से मिलाकर जाँचे गए हैं।

  1. GATE 2026 Computer Science and Information Technology (CS) syllabus, IIT Guwahati
  2. NPTEL Database Systems (IIT Kanpur, 106104135): ER model and relational model