ความสัมพันธ์และคีย์ภายนอก
เชื่อมตารางเข้าด้วยกันด้วยคีย์ภายนอก เพื่อจำลองความสัมพันธ์แบบหนึ่งต่อหลายและหลายต่อหลายรายการในฐานข้อมูล Supabase Postgres
ความสัมพันธ์และคีย์ภายนอก เป็นบทเรียน Supabase Backend as a Service ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Supabase Backend as a Service และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Supabase Backend as a Service มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Relationships Matter
Real data is connected: a user has many posts, an order has many items. Relationships link rows across tables so you avoid duplicating data.
What Is a Foreign Key?
A foreign key is a column that points to the primary key of another table. It enforces that the referenced row actually exists.
One-to-Many Example
One author writes many books. The books table stores the author's id.
CREATE TABLE authors (
id bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
name text NOT NULL
);
CREATE TABLE books (
id bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
title text NOT NULL,
author_id bigint REFERENCES authors(id)
);Referential Integrity
Postgres will reject inserting a book with an author_id that does not exist. This keeps your data consistent.
ON DELETE Behavior
Decide what happens when a parent row is deleted:
CASCADEdeletes the children tooSET NULLclears the referenceRESTRICTblocks the delete
ALTER TABLE books
ADD CONSTRAINT fk_author
FOREIGN KEY (author_id)
REFERENCES authors(id)
ON DELETE CASCADE;Querying Across Tables
With the Supabase client you can fetch related rows in one call using nested selects.
const { data } = await supabase
.from('authors')
.select('name, books(title)');Many-to-Many Relationships
When both sides can have many of the other (students and courses), use a join table holding both foreign keys.
CREATE TABLE enrollments (
student_id bigint REFERENCES students(id),
course_id bigint REFERENCES courses(id),
PRIMARY KEY (student_id, course_id)
);Composite Primary Keys
In a join table the pair of foreign keys often forms the primary key, preventing duplicate links between the same two rows.
Naming Conventions
A common convention names foreign keys as singular_id (e.g. author_id). Consistency makes nested queries predictable.
function fkName(table) {
return table.replace(/s$/, '') + '_id';
}
console.log(fkName('authors'));Visualizing in the Dashboard
The Supabase Table Editor shows relationship links, and the Schema Visualizer draws how your tables connect, which helps verify your design.
Putting It Together
Model connections with foreign keys, choose sensible ON DELETE rules, and use join tables for many-to-many. This is the backbone of a clean schema.
Quick Check
Test your understanding of relationships.
Recap
You learned foreign keys, one-to-many vs many-to-many relationships, ON DELETE behaviors, join tables with composite keys, and how to query related data with nested selects.
คำถามที่พบบ่อย
บทเรียน “ความสัมพันธ์และคีย์ภายนอก” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ความสัมพันธ์และคีย์ภายนอก” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Supabase Backend as a Service ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Supabase Backend as a Service มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ความสัมพันธ์และคีย์ภายนอก”
เชื่อมตารางเข้าด้วยกันด้วยคีย์ภายนอก เพื่อจำลองความสัมพันธ์แบบหนึ่งต่อหลายและหลายต่อหลายรายการในฐานข้อมูล Supabase Postgres คุณปฏิบัติ Supabase Backend as a Service ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Supabase Backend as a Service หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Supabase Backend as a Service บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “ความสัมพันธ์และคีย์ภายนอก” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Supabase Backend as a Service นี้ได้ไหม
ได้ บทเรียน Supabase Backend as a Service ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การออกแบบโครงสร้างฐานข้อมูล
- การสร้างตารางและคอลัมน์
- การเพิ่มและสืบค้นข้อมูลเบื้องต้น
- ความสัมพันธ์และคีย์ภายนอก