Skip to content

Commit

Permalink
Merge pull request #4239 from szarnyasg/fk-constraints
Browse files Browse the repository at this point in the history
Update FK example
  • Loading branch information
szarnyasg authored Dec 3, 2024
2 parents 4ec6688 + 8673228 commit ad8ac6c
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions docs/sql/constraints.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ad8ac6c

Please sign in to comment.