Before you start, you need
Turn each rule into a constraint
A table can stop data that breaks its rules from being saved. A title must exist, an email perhaps should not repeat, and a task flag should only ever be 0 or 1. A constraint is a rule that the table checks.
NOT NULL requires a value to be present. UNIQUE stops a value from repeating; it can also protect a pair of columns. These rules apply even when data comes in from different applications.
DEFAULT supplies a value when you leave the column out on insert. DEFAULT 0 does not stop anyone writing 7, and it does not replace a NULL written explicitly. To limit values, use CHECK, such as CHECK (completed IN (0, 1)).
If the flag must exist and be 0 or 1, combine NOT NULL and CHECK. The condition in a CHECK can give an unknown result with NULL; on its own, it does not require a value. Try one valid value and one that should be rejected to check each rule.
title TEXT NOT NULL,
code TEXT UNIQUE,
completed INTEGER
NOT NULL
DEFAULT 0
CHECK (completed IN (0, 1))See it in action
We want tasks with a required title and a completed flag that accepts only 0 or 1.
CREATE TABLE valid_tasks (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
completed INTEGER NOT NULL DEFAULT 0
CHECK (completed IN (0, 1))
);Result in the lab: valid_tasks created.
- NOT NULL forces a title and a flag to be stored. The identity is declared with the key you have already learnt.
- DEFAULT 0 is used when an insert leaves out completed. CHECK stops a value such as 7 from being written.
- If you insert NULL explicitly, DEFAULT does not replace it. NOT NULL rejects that attempt.
- UNIQUE stops a value from repeating; UNIQUE(customer_id, code) protects a pair. The challenge combines a unique address with a valid reference.
Practice with help
Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.
Guided practice
Set titles and starting values
Create tasks with id INTEGER as the primary key, a required title VARCHAR(200) and completed INTEGER with a default of 0.
We check the requested structure, the valid rows, the default values and that invalid rows are rejected.
Every attempt resets the data, so include all the steps you need to check in a single attempt.
View tables and data
Open this panel to see the full data.
The title must be present, and completed must get 0 when it is left out.
Add NOT NULL to title.
Use DEFAULT 0 on completed.
Solution explained
CREATE TABLE tasks (id INTEGER PRIMARY KEY, title VARCHAR(200) NOT NULL, completed INTEGER DEFAULT 0);Each constraint expresses a different rule of the domain.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Protect presence, uniqueness and references
Create contacts with the four columns given, in this order. It must accept only data that meets its rules:
- id: INTEGER PRIMARY KEY.
- customer_id: INTEGER NOT NULL REFERENCES customers(id).
- email: TEXT NOT NULL UNIQUE.
- active: INTEGER NOT NULL DEFAULT 1, with a CHECK that allows only 0 or 1.
We check the requested structure, the valid rows, the default values and that invalid rows are rejected.
Every attempt resets the data, so include all the steps you need to check in a single attempt.
View tables and data
Open this panel to see the full data.
Match each requirement to a different rule.
DEFAULT does not replace CHECK or NOT NULL.
Reference customers(id) and limit active to 0 and 1.
Solution explained
CREATE TABLE contacts (id INTEGER PRIMARY KEY, customer_id INTEGER NOT NULL REFERENCES customers(id), email TEXT NOT NULL UNIQUE, active INTEGER NOT NULL DEFAULT 1 CHECK (active IN (0,1)));The reference requires a real customer; the address must exist and be unique; active has presence, a default and a domain.
Key takeaways
- NOT NULL requires presence.
- UNIQUE stops the declared value or combination from repeating.
- DEFAULT supplies a value when the column is left out.
- CHECK limits the accepted values.