العلاقات والمفاتيح الخارجية
اربط الجداول باستخدام المفاتيح الخارجية لنمذجة العلاقات واحد إلى متعدد ومتعدد إلى متعدد في قاعدة بيانات Supabase Postgres.
العلاقات والمفاتيح الخارجية درس مجاني في Supabase Backend as a Service على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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.
الأسئلة الشائعة
هل درس «العلاقات والمفاتيح الخارجية» مجاني؟
نعم — نص درس «العلاقات والمفاتيح الخارجية» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Supabase Backend as a Service، انتقل إلى CoddyKit PRO. تتضمن دورة Supabase Backend as a Service 4 دروس في المجموع.
ماذا ستتعلم في «العلاقات والمفاتيح الخارجية»؟
اربط الجداول باستخدام المفاتيح الخارجية لنمذجة العلاقات واحد إلى متعدد ومتعدد إلى متعدد في قاعدة بيانات Supabase Postgres. تتمرن على Supabase Backend as a Service مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 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 يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تصميم مخططات قواعد البيانات
- إنشاء الجداول والأعمدة
- إدخال البيانات والاستعلام عنها أساسيًا
- العلاقات والمفاتيح الخارجية