ERD: entities और relationships को tables में बदलें
जानें कि ERD में entities, attributes, cardinality और optionality कैसे दिखाते हैं और उन्हें keys व constraints में कैसे बदलते हैं।
ERD: entities और relationships को tables में बदलें
Entity-relationship diagram (ERD) बताता है कि सिस्टम कौन-सी जानकारी रखता है और अवधारणाएँ कैसे जुड़ी हैं। टीम इसे tables, keys और constraints बनाने से पहले नियमों पर चर्चा के लिए उपयोग करती है। चित्र अपने आप data integrity लागू नहीं करता; schema में नियम लागू करने पड़ते हैं।
Entities, attributes और relationships
Entity उस वस्तु का प्रकार है जिसे सिस्टम दर्ज करता है, जैसे customer, order या product। Attributes उसकी विशेषताएँ हैं, जैसे customer email या order date। Relationship entities के बीच व्यावसायिक संबंध बताता है। हर संज्ञा को entity न बनाएँ: shipping address यदि order के साथ प्रबंधित है तो उसका attribute हो सकता है; साझा address book को अलग entity चाहिए हो सकती है।
Cardinality और optionality पढ़ें
Cardinality बताती है कि एक तरफ की एक instance दूसरी तरफ की कितनी instances से जुड़ सकती है। Notation बदलती है; legend दें और संभव हो तो minimum तथा maximum लिखें। 0..1 वैकल्पिक, अधिकतम एक; 1 ठीक एक; 0..* शून्य या अधिक; 1..* एक या अधिक। Minimum सहभागिता वैकल्पिक/अनिवार्य बताता है, maximum multiplicity बताता है।
इस उदाहरण में customer के शून्य या कई orders हो सकते हैं, पर हर order ठीक एक customer का है: Customer 1 — 0..* Order। Orders और products का संबंध many-to-many है। OrderItem entity इसे तोड़ती है और quantity या purchase-time price जैसे relationship attributes रख सकती है।
चित्र में भाषा-स्वतंत्र कोड हैं: A=customer, B=order, C=order item, D=product। सिरों के अंक सीमाएँ दिखाते हैं।
चित्र 1. Orders और products का many-to-many संबंध order items से दर्शाया गया है; प्रत्येक item एक order और एक product को संदर्भित करता है।
मॉडल को tables में बदलें
One-to-many संबंध में foreign key आम तौर पर many वाली तरफ होती है। हर order के लिए customer जरूरी हो तो customer_id NOT NULL REFERENCES customers(id) रखें। Foreign key customer के अस्तित्व की जाँच करती है; NOT NULL order पक्ष से संबंध अनिवार्य बनाता है। Many-to-many के लिए दोनों entities को संदर्भित करने वाली junction table बनती है। Primary या unique key duplicate pair रोक सकती है, लेकिन पहले तय करें कि एक product order में कई पंक्तियों पर आ सकता है या नहीं।
नीचे PostgreSQL syntax केवल उदाहरण है; चलाया नहीं गया:
Sources
[Oracle Data Modeler: entities and relationships](https://docs.oracle.com/cd/E29463_01/doc.33/e35120/data_modeling.htm)
[Microsoft SQL Server: database diagram relationships](https://learn.microsoft.com/en-us/ssms/visual-db-tools/design-database-diagrams-visual-database-tools)
[PostgreSQL 18: constraints](https://www.postgresql.org/docs/18/ddl-constraints.html)