From 867322808a69660e0ebf309abffed18007e90e70 Mon Sep 17 00:00:00 2001 From: Gabor Szarnyas Date: Tue, 3 Dec 2024 06:52:28 +0100 Subject: [PATCH] Update FK example --- docs/sql/constraints.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/docs/sql/constraints.md b/docs/sql/constraints.md index 33e602c045..1f967ac614 100644 --- a/docs/sql/constraints.md +++ b/docs/sql/constraints.md @@ -97,10 +97,17 @@ Foreign keys define a column, or set of columns, that refer to a primary key or ```sql CREATE TABLE students (id INTEGER PRIMARY KEY, name VARCHAR); -CREATE TABLE exams (student_id INTEGER REFERENCES students(id), grade INTEGER); +CREATE TABLE subjects (id INTEGER PRIMARY KEY, name VARCHAR); +CREATE TABLE exams ( + exam_id INTEGER PRIMARY KEY, + subject_id INTEGER REFERENCES subjects(id), + student_id INTEGER REFERENCES students(id), + grade INTEGER +); INSERT INTO students VALUES (1, 'Student 1'); -INSERT INTO exams VALUES (1, 10); -INSERT INTO exams VALUES (2, 10); +INSERT INTO subjects VALUES (1, 'CS 101'); +INSERT INTO exams VALUES (1, 1, 1, 10); +INSERT INTO exams VALUES (2, 1, 2, 10); ``` ```console