နားလည်ထားရမယ့် အချက်
- One-to-one — User <-> Profile (row တစ်ကြောင်းစီစီတည်း တွဲနေတယ်)
- One-to-many — User -> Orders/Posts များစွာ (တစ်ဘက်ချင်းစီက User တစ်ယောက်တည်းသာ ပြန်ညွှန်း)
- Many-to-many — Students <-> Courses (join table student_courses ကနေ)
One-to-one ဆိုတာ table တစ်ခုထဲက row တစ်ကြောင်းက table တခြားတစ်ခုထဲက row တစ်ကြောင်းတည်းနဲ့ ချိတ်ဆက်တာပါ — User နဲ့ Profile, organize လုပ်ဖို့ သီးခြားထားပေမယ့် အမြဲတွဲနေတာပါ။
One-to-many ဆိုတာ row တစ်ကြောင်းက row များစွာနဲ့ ချိတ်ဆက်ပေမယ့် အပြန်အလှန်မဟုတ်ပါ — User တစ်ယောက်မှာ Order များစွာရှိနိုင်ပေမယ့် Order တစ်ခုစီက User တစ်ယောက်တည်းကိုပဲ ညွှန်းပါတယ်။ ဒါက အသုံးအများဆုံး shape ပါ။
Many-to-many ဆိုတာ ဘက်နှစ်ဘက်စလုံးက row များစွာနဲ့ ချိတ်ဆက်နိုင်တာပါ — Student များစွာက Course များစွာ တက်ရောက်ပါတယ်။ ဒီအခါ table ဘယ်ဟာမှ foreign key တစ်ခုတည်းနဲ့ မကိုင်ဆောင်နိုင်ပါ၊ အလယ်မှာ join table (student_courses) တစ်ခု လိုအပ်ပါတယ်။
Join Table Row
student_courses ထဲက row တစ်ကြောင်းစီက pair တစ်ခုသာပါ: student_id နဲ့ course_id, enrollment တစ်ခုကို ကိုယ်စားပြုပါတယ်။ Relationship က one-to-many link နှစ်ခု အလယ်မှာ တွေ့ဆုံလိုက်တာပါပဲ။
THREE RELATIONSHIP SHAPES
-------------------------
THREE RELATIONSHIP SHAPES
----------------------------
ONE-TO-ONE
User <-----------------> Profile
ONE-TO-MANY
User <---------------+--- Post
+--- Post
+--- Post
MANY-TO-MANY (via a join table)
Student <---> student_courses <---> Course
Student <---> | <---> Course
v
(student_id, course_id) pairsလက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်
Real feature အတွက် relationship shape သုံးမျိုးထဲက ဘယ်ဟာလိုသလဲ မှတ်တတ်ခြင်းက data modeling ရဲ့ အရေးအကြီးဆုံး skill ပါ။ Quick test: 'side A မှာ side B တစ်ခုထက်ပိုနိုင်သလား, ပြန်လည်ရော' လို့ မေးကြည့်ပါ။
အဖြေနှစ်ခုစလုံး 'no' ဆိုရင် one-to-one, တစ်ဘက်တည်း many ရှိရင် one-to-many, ဘက်နှစ်ဘက်စလုံး many ရှိရင် many-to-many ဖြစ်ပြီး join table လိုအပ်ပါတယ် — true many-to-many ကို foreign key column တစ်ခုတည်းနဲ့ model ချလို့ မရနိုင်ပါ။
Column တစ်ခုထဲ List ထည့်ချင်တဲ့ Signal
Column တစ်ခုတည်းထဲမှာ list တစ်ခု ထည့်ချင်တယ် ဆိုတဲ့ ခံစားချက်ရလာတိုင်း many-to-many relationship တစ်ခုနဲ့ join table အမှန်တကယ် လိုအပ်နေတဲ့ လက္ခဏာဖြစ်ပါတယ်။
အတူတူ စမ်းရေးကြည့်မယ်
const students = [
{ id: 1, name: "Aung" },
{ id: 2, name: "Moe" },
];
const courses = [
{ id: 10, title: "Databases 101" },
{ id: 20, title: "Web Basics" },
{ id: 30, title: "Networking" },
];
const studentCourses = [
{ student_id: 1, course_id: 10 },
{ student_id: 1, course_id: 20 },
{ student_id: 2, course_id: 20 },
{ student_id: 2, course_id: 30 },
];
function getEnrolledCourses(studentId, joinTable, coursesTable) {
const courseIds = joinTable
.filter((link) => link.student_id === studentId)
.map((link) => link.course_id);
return coursesTable.filter((course) => courseIds.includes(course.id)).map((c) => c.title);
}
console.log("Aung is enrolled in:", getEnrolledCourses(1, studentCourses, courses));
console.log("Moe is enrolled in:", getEnrolledCourses(2, studentCourses, courses));Aung is enrolled in: [ 'Databases 101', 'Web Basics' ]
Moe is enrolled in: [ 'Web Basics', 'Networking' ]၅ မိနစ် စမ်းကြည့်
studentCourses array ထဲကို enrollment အသစ်တစ်ခု (student 1 ကို course 30 ထပ်ထည့်) ထည့်ပြီး getEnrolledCourses(1, ...) ရလဒ် ဘယ်လိုပြောင်းလဲမလဲ ကြိုခန့်မှန်းပြီး run ကြည့်ပါ။
သတိလေးတစ်ချက်
many-to-many ကို join table သုံးမည့်အစား column တစ်ခုထဲ id list ကို comma-separated string အဖြစ် သိမ်းဆည်းခြင်း
one-to-one ကို အမြဲ table နှစ်ခု ပေါင်းပစ်သင့်တယ်လို့ ယူဆခြင်း — တစ်ခါတစ်ရံ ဟုတ်ပေမယ့် optional/sensitive data ကို သီးခြားထားတာမျိုးမှာတော့ ဟုတ်ချင်မှ ဟုတ်ပါလိမ့်မယ်
Wikipedia: Many-to-many (data model) — How Databases Work