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 કરવા શું જોઈએ?
- Many-to-many, એક junction/linking table ની જરૂર
- One-to-many, સીધું બે tables માં store થવા લાયક
- One-to-one, કોઈ વધારાના table ની જરૂર નહીં
- 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).