Full answer
Think of a college register.
- A table holds one kind of thing:
students,courses,enrolments. - A row is one instance: the student Asha.
- A column is one attribute every row has, with a data type:
name TEXT,year INTEGER. - A primary key identifies each row uniquely and never repeats:
students.id. It cannot be NULL. A table has at most one primary key, but the key can span several columns, like(student_id, course_id)inenrolments. - A foreign key is a column whose value must exist as a key in another table.
enrolments.student_idmust be the id of a real student. This is called referential integrity.
sql
CREATE TABLE students (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
city TEXT,
year INTEGER
);
CREATE TABLE courses (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
credits INTEGER
);
CREATE TABLE enrolments (
student_id INTEGER REFERENCES students(id),
course_id INTEGER REFERENCES courses(id),
marks INTEGER,
PRIMARY KEY (student_id, course_id)
);
INSERT INTO students VALUES (1,'Asha','Pune',1),(2,'Ben','Leeds',2),(3,'Chen','Pune',1),(4,'Dara',NULL,3),(5,'Eli','Leeds',2);
INSERT INTO courses VALUES (10,'Databases',4),(20,'Algorithms',4),(30,'Statistics',3),(40,'Ethics',2);
INSERT INTO enrolments VALUES
(1,10,88),(1,20,92),(2,10,75),(2,30,NULL),
(3,10,64),(3,20,71),(3,30,80),(5,20,55);
PRAGMA foreign_keys = ON;The foreign key is what makes a join meaningful. To list each student with their marks, you match enrolments.student_id to students.id:
sql
SELECT s.name, c.title, e.marks
FROM enrolments e
JOIN students s ON s.id = e.student_id
JOIN courses c ON c.id = e.course_id
WHERE s.id = 1
ORDER BY c.title;| name | title | marks |
|---|---|---|
| Asha | Algorithms | 92 |
| Asha | Databases | 88 |
With foreign keys enforced, the database refuses an enrolment for a student who does not exist:
sql
INSERT INTO enrolments VALUES (99, 10, 70); -- error on purposetext
Error: FOREIGN KEY constraint failedSQLite only enforces foreign keys after PRAGMA foreign_keys = ON; PostgreSQL always enforces them.
sql
CREATE TABLE students (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
city TEXT,
year INTEGER
);
CREATE TABLE courses (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
credits INTEGER
);
CREATE TABLE enrolments (
student_id INTEGER REFERENCES students(id),
course_id INTEGER REFERENCES courses(id),
marks INTEGER,
PRIMARY KEY (student_id, course_id)
);
INSERT INTO students VALUES (1,'Asha','Pune',1),(2,'Ben','Leeds',2),(3,'Chen','Pune',1),(4,'Dara',NULL,3),(5,'Eli','Leeds',2);
INSERT INTO courses VALUES (10,'Databases',4),(20,'Algorithms',4),(30,'Statistics',3),(40,'Ethics',2);
INSERT INTO enrolments VALUES
(1,10,88),(1,20,92),(2,10,75),(2,30,NULL),
(3,10,64),(3,20,71),(3,30,80),(5,20,55);
PRAGMA foreign_keys = ON;
SELECT s.name, c.title, e.marks
FROM enrolments e
JOIN students s ON s.id = e.student_id
JOIN courses c ON c.id = e.course_id
WHERE s.id = 1
ORDER BY c.title;
-- error on purpose
INSERT INTO enrolments VALUES (99, 10, 70);Checked on SQLite 3.46.1, run twice with identical output.
Key points
- Table: one kind of entity; row: one instance; column: one typed attribute
- Primary key: unique, not NULL, one per table, can be composite
- Foreign key: value must exist in the referenced table
- SQLite enforces foreign keys only when the pragma is on
Common mistakes
- Saying a table can have several primary keys (it can have several unique keys, but one primary key)
- Assuming SQLite checks foreign keys by default
Follow-ups
- How does a primary key differ from a unique constraint?
- What is a composite key and when do you need one?
Tips
- Use one small example schema and point to each concept in it