Relacje i klucze obce
Połącz tabele za pomocą kluczy obcych, aby modelować relacje jeden-do-wielu i wiele-do-wielu w bazie danych Supabase Postgres.
Relacje i klucze obce to bezpłatna lekcja Supabase Backend as a Service na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Supabase Backend as a Service, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Supabase Backend as a Service zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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.
Często zadawane pytania
Czy lekcja „Relacje i klucze obce” jest bezpłatna?
Tak — pełny tekst „Relacje i klucze obce” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Supabase Backend as a Service, przejdź na CoddyKit PRO. Kurs Supabase Backend as a Service zawiera 4 lekcji w sumie.
Co nauczysz się w „Relacje i klucze obce”?
Połącz tabele za pomocą kluczy obcych, aby modelować relacje jeden-do-wielu i wiele-do-wielu w bazie danych Supabase Postgres. Ćwiczysz Supabase Backend as a Service z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Supabase Backend as a Service?
Nie wymagamy żadnego doświadczenia. Supabase Backend as a Service w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Relacje i klucze obce”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Supabase Backend as a Service?
Tak. Każda lekcja Supabase Backend as a Service zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Projektowanie schematów baz danych
- Tworzenie tabel i kolumn
- Podstawowe wstawianie danych i wykonywanie zapytań
- Relacje i klucze obce