MySQL + SQL · Lesson 23
School Database के लिए ER Diagram Examples
उदाहरणों से सीखना
ER diagrams समझने का सबसे तेज़ तरीका असली देखना है। हम तीन परिचित systems — एक school, एक library और एक online shop — को entities, attributes और relationships के रूप में model करेंगे, फिर हर एक को tables पर map करेंगे।
💡 हर उदाहरण को: entities ढूँढिए, attributes जोड़िए, relationships से जोड़िए, फिर tables में बदलिए।
उदाहरण 1: School
- Entities: Student, Teacher, Course।
- Attributes: Student(roll [key], name), Course(code [key], title)।
- Relationships: Student Course में enroll करता है (many-to-many); Teacher Course पढ़ाता है (one-to-many)।
students(roll PK, name) courses(code PK, title, teacher_id FK) enrollments(roll FK, code FK) -- M:N को link table
उदाहरण 2: Library
- Entities: Member, Book।
- Attributes: Member(id [key], name), Book(isbn [key], title)।
- Relationship: Member Book borrow करता है (many-to-many, borrow date के साथ)।
members(id PK, name) books(isbn PK, title) borrowings(id FK, isbn FK, borrow_date) -- link table
Many-to-many "borrows" relationship borrowings table बनती है जो दोनों keys और date जैसी अतिरिक्त जानकारी रखती है।
उदाहरण 3: Online shop
- Entities: Customer, Product, Order।
- Attributes: Customer(id [key], name), Product(id [key], price), Order(id [key], date)।
- Relationships: Customer Order place करता है (one-to-many); Order Product रखता है (many-to-many)।
customers(id PK, name) products(id PK, price) orders(id PK, date, customer_id FK) order_items(order_id FK, product_id FK, qty) -- link table
इन्हें tables में बदलना
Pattern हर बार वही है:
- Entity → table; key attribute → primary key।
- One-to-many → "many" तरफ़ foreign key (जैसे courses में
teacher_id)। - Many-to-many → दोनों foreign keys वाली link table (जैसे
enrollments,order_items)।
एक बार cardinality पहचान लें, tables लगभग खुद लिख जाती हैं।
अपना बनाने के steps
- Entities list कीजिए (system के मुख्य nouns)।
- हर एक को attributes जोड़कर key underline कीजिए।
- Relationships ढूँढिए (entities जोड़ते verbs)।
- हर relationship की cardinality तय कीजिए।
- ऊपर के नियमों से tables में बदलिए।
आम गलतियाँ
- M:N को link table चूकना। Many-to-many को हमेशा एक चाहिए।
- Foreign key गलत तरफ़ रखना। यह "many" तरफ़ जाती है।
- Link पर अतिरिक्त जानकारी भूलना। date या quantity जैसा data link table में जाता है।
- Cardinality छोड़ना। Tables बनाने से पहले तय कीजिए।
सारांश
- Systems को entities, attributes और relationships के रूप में model कीजिए।
- School, library और shop सब वही pattern follow करते हैं।
- Entity → table; key → primary key।
- One-to-many → foreign key; many-to-many → link table।
- Cardinality तय कीजिए, फिर tables स्वाभाविक रूप से बनती हैं।
आगे, हिस्से दोहराइए ER model में, या पूरा बनाइए school database case study में।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
School को ER diagram कैसे बनाऊँ?
Entities पहचानिए (Student, Teacher, Course), हर एक को attributes दीजिए (जैसे roll, name), और relationships बनाइए (Student Course में enroll करता है, Teacher Course पढ़ाता है)। Cardinality चिह्नित कीजिए, फिर entities को tables और relationships को keys पर map कीजिए।
Library ER diagram में कौन-सी entities होती हैं?
Library में आमतौर पर Member, Book और Librarian entities होती हैं। मुख्य relationship Member Book "borrow करता है" है, जो many-to-many है, तो यह members और books को जोड़ती अलग borrowing table बनती है।
ER diagram tables कैसे बनता है?
हर entity एक table बनती है, हर attribute एक column, key attribute primary key। One-to-many relationships many तरफ़ foreign key जोड़ते हैं, और many-to-many relationships दो foreign keys वाली link table बनते हैं।
Many-to-many relationship का उदाहरण क्या है?
एक Student कई Courses ले सकता है और एक Course के कई Students हो सकते हैं — वह many-to-many है। Tables में यह link table (जैसे enrollments) बनती है जो student_id और course_id साथ रखती है।
ER diagram बनाने को कौन-से steps follow करूँ?
Entities list कीजिए, उनके attributes जोड़कर key चिह्नित कीजिए, entities के बीच relationships पहचानिए, हर की cardinality तय कीजिए, फिर tables में बदलिए। उस क्रम में काम करना design साफ़ और पूरा रखता है।
