E/R Diagram: one-to-one, one-to-many, many-to-one, many-to-many

Cardinality બતાવે છે કે એક entity ના કેટલા બીજી સાથે જોડાય છે: one-to-one (એક વ્યક્તિ અને એનું Aadhaar), one-to-many (એક મા અને એના બાળકો), અને many-to-many (students અને courses), જેને implement કરવા એક linking table જોઈએ.

10 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

એક order, કે સો?

Meera ના design માં એક Customer અને એક Order છે, 'આપે છે' થી જોડાયેલા. પણ એક મહત્વનો સવાલ નક્કી કરે છે કે tables ખરેખર કેવી રીતે કામ કરે છે: કેટલા? શું એક customer એક order આપે છે, કે ઘણા?

એ 'કેટલા' ને cardinality કહે છે, અને એને સાચું કરવું એ એવા database, જે કામ કરે છે, અને એવા, જે data ખોવે છે, ના વચ્ચેનો ફરક છે. એક પ્રકારની cardinality, many-to-many, એટલી માંગ વાળી છે કે એ તમને એક વધારાનું table ઉમેરવા મજબૂર કરે છે, બરાબર એ સૂઝ જે Meera ના આખા design ને સમજાવશે. આ lesson relationships નું 'કેટલા' છે.

Theory

એક પરિવારમાં connections ગણવા

પરિવારના સંબંધો વિચારો. એક વ્યક્તિ અને એનું Aadhaar number: બંને બાજુ બરાબર એક (one-to-one). એક મા અને એના બાળકો: એક મા, ઘણા બાળકો (one-to-many). Students અને એ subjects જે એ લે છે: દરેક student ઘણા subjects લે છે, દરેક subject ના ઘણા students છે (many-to-many). તમે cardinality ને પહેલેથી સહજ-બોધથી સમજો છો, એ બસ ગણવું છે કે દરેક બાજુના કેટલા જોડાય છે.

At a glance

cardinalities

Typeઅર્થઉદાહરણ
One-to-one (1:1)દરેક બાજુ, વધુમાં વધુ એકPerson to Aadhaar
One-to-many (1:N)એક ઘણા સાથે જોડાય છેCustomer to Orders
Many-to-one (N:1)એ જ, ઘણા વાળી બાજુથી જોયુંOrders to Customer
Many-to-many (M:N)દરેક બાજુ ઘણા સાથે જોડાય છેStudents to Courses

Theory

Many-to-many સમસ્યા

One-to-one અને one-to-many tables માં સાફ-સુથરા બેસે છે. પણ many-to-many ને સીધું બે tables માં store કરી શકાતું નથી, તમે links ક્યાં રાખશો? એક customer ઘણા items ખરીદે છે અને એક item ઘણા customers દ્વારા ખરીદાય છે; કોઈ table માં એક list માટે જગ્યા નથી.

એનો ઇલાજ વચ્ચે એક junction (linking) table છે. એક દુકાન માટે, એ Orders table છે: એ ગંદા M:N (customers to items) ને બે સાફ one-to-many relationships માં તોડે છે (એક customer થી ઘણા orders, એક item ઘણા orders માં આવતું). દરેક many-to-many એક વચ્ચેનું table બની જાય છે.

Quiz

એક college માં, દરેક student ઘણા courses માં enrol કરે છે, અને દરેક course ના ઘણા students છે. આ કઈ cardinality છે, અને એને implement કરવા શું જોઈએ?

  1. Many-to-many, એક junction/linking table ની જરૂર
  2. One-to-many, સીધું બે tables માં store થવા લાયક
  3. One-to-one, કોઈ વધારાના table ની જરૂર નહીં
  4. Many-to-one, બસ એક foreign key
Show the answer

Many-to-many, એક junction/linking table ની જરૂર

દરેક બાજુ બીજીના ઘણા સાથે જોડાય છે, તો એ many-to-many (M:N) છે. એ ફક્ત Student અને Course tables માં રહી શકતું નથી; એને student-અને-course ની જોડી રાખતું એક junction table (એક Enrollment table) જોઈએ, M:N ને બે one-to-many relationships માં વહેંચતું. 'Students અને courses' એ THE textbook M:N ઉદાહરણ છે, અને junction-table ની જરૂર એ મુખ્ય exam મુદ્દો છે.

Think first

Cardinality ઓળખો

Meera કહે છે: 'મારો દરેક order બરાબર એક customer નો છે, પણ એક customer ના ઘણા orders હોઈ શકે.' એને CUSTOMER ની બાજુથી વાંચતાં, આ કઈ cardinality છે? અને શું એને એક junction table જોઈએ?

Show the answer

Customer ની બાજુથી એ one-to-many (1:N) છે: એક customer, ઘણા orders. (Order ની બાજુથી એ જ relationship many-to-one છે, એ જ link, ઉલટો દૃષ્ટિકોણ.) એને એક junction table નહીં જોઈએ, one-to-many સીધું store થાય છે customer ની key ને દરેક order row માં મૂકીને. ફક્ત many-to-many ને વધારાનું વચ્ચેનું table જોઈએ. 1:N એ સાચા databases માં સૌથી સામાન્ય relationship છે.

Watch out

Marks ક્યાં કપાય છે

એ ચૂકવું કે many-to-many ને એક junction table જોઈએ, અહીંનું સૌથી મહત્વનું implementation તથ્ય. one-to-many અને many-to-one ને ભેળવવા: એ ઉલટા છેડાથી જોયેલો એક જ relationship છે, અલગ designs નહીં. અને નબળા ઉદાહરણ આપવા, ચોખ્ખા classics વાપરો: person-Aadhaar (1:1), mother-children કે customer-orders (1:N), students-courses (M:N). Exams તમારી પાસે દરેકને નામ આપવા અને એક ઉદાહરણ આપવા બંને કરાવે છે.

Theory

આ Meera ના આખા design ને સમજાવે છે

હવે સમજાય છે કે એક દુકાનનું database ક્યારેય ફક્ત 'customers અને items' કેમ નથી હોતું: એને વચ્ચે એક Orders table જોઈએ, બરાબર એટલા માટે કારણ કે customers-to-items many-to-many છે. એ વચ્ચેનું table એ જગ્યા છે જ્યાં keys પોતાનું કામ કરે છે, જે આગળના lessons નો સેટ છે. Cardinality એ કારણ છે કે database designs માં એ tables હોય છે જે હોય છે. આગળ: એક entity ને પોતાના દમ પર ટકવા શું જોઈએ, strong સામે weak entities.

Summary

Key takeaways

  • Cardinality = એક entity ના કેટલા બીજી સાથે જોડાય છે.
  • One-to-one (1:1): દરેક બાજુ વધુમાં વધુ એક (person to Aadhaar).
  • One-to-many (1:N): એક ઘણા સાથે જોડાય છે (customer to orders); સૌથી સામાન્ય; સીધું store થાય છે.
  • Many-to-one એ જ 1:N relationship છે બીજી બાજુથી જોયું.
  • Many-to-many (M:N): દરેક બાજુ ઘણા સાથે જોડાય છે (students to courses); એક junction/linking table જોઈએ.
  • યાદ રાખવાની યુક્તિ: person-Aadhaar (1:1), mother-children (1:N), students-courses (M:N).

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Concepts of Database

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

E/R Diagram: one-to-one, one-to-many, many-to-one, many-to-many · Data Processing and Analysis (DPA) · Gri-Learn