← VCE Spreadsheets, SQL & Visualisation

SQL constraints for validation

A constraint is a rule on a column. This CHECK rejects any record with an age outside 0 to 120. NOT NULL (must have a value) and UNIQUE (no repeats) are constraints too.

Understanding SQL constraints for validation in VCE Spreadsheets, SQL & Visualisation

What it does: UNIQUE means no value can repeat in that column, so a second record with the same email is rejected. CHECK with BETWEEN is a range check. Any mark below 0 or above 100 is rejected. CHECK is the constraint used for a range check. NOT NULL does not take a condition. The data type comes first, then the constraint: name TEXT NOT NULL. BETWEEN includes the start and end values, so 0 and 120 are both allowed. Constraints validate data on entry. They reject bad records rather than correcting them later. A CHECK constraint needs its rule inside brackets. Without them the database cannot read the rule.

Common mistakes: The condition inside a CHECK constraint must be wrapped in brackets. It should be CHECK (age >= 0). The presence check constraint is written NOT NULL, not NO NULL. It stops the column being left empty.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 45–54) and word search (Modules 35–54).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which constraint makes sure a column can never be left empty (a presence check)?
UNIQUENOT NULL
Matches
name TEXT NULL NOT
↓
The words are in the wrong order. NOT NULL is the keyword pair and it should be written as name TEXT NOT NULL.
Applying
Meanings
Term
CHECK (age BETWEEN 0 AND 120)
Meaning
A constraint is a rule on a column. This CHECK rejects any record with an age outside 0 to 120. NOT NULL (must have a value) and UNIQUE (no repeats) are constraints too.
Practice
CREATE TABLE student (
Testing
Gaps
name TEXT ,
Check Answer
Spots
2 id INT UNIQUE,
3 age INT CHECK age >= 0,

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Chambered Nautilus' badge — File Explorer, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which constraint makes sure a column can never be left empty (a presence check)?
A. NOT NULL ✓
B. UNIQUE
NOT NULL means the column must have a value. UNIQUE only stops repeated values.
2. Which constraint stops two records from having the same value in a column, such as two students with the same email?
A. UNIQUE ✓
B. NOT NULL
UNIQUE rejects repeats. NOT NULL only checks that a value is present.
3. Which is the correct way to make sure age is between 0 and 120?
A. CHECK (age BETWEEN 0 AND 120) ✓
B. NOT NULL (age BETWEEN 0 AND 120)
CHECK is the constraint used for a range check. NOT NULL does not take a condition.
4. A table has the rule CHECK (age BETWEEN 0 AND 120). What happens when you try to INSERT a record with age 150?
A. The database saves it and changes the age to 120
B. The database rejects the record ✓
A constraint stops a record that breaks the rule from being saved. The database does not fix the value for you.
5. Which type of validation check does CHECK (age BETWEEN 0 AND 120) perform?
A. A presence check
B. A range check ✓
It tests that the number sits within a set range. A presence check is done by NOT NULL.

Match the Pairs

name TEXT NOT NULL → A presence check: the database will not accept a record if the name field is left empty.
CHECK (age BETWEEN 0 AND 120) → A range check: the database rejects any record where age is below 0 or above 120.
A constraint that stops two students being saved with the same email address → email TEXT UNIQUE
A constraint that only allows a mark between 0 and 100 → CHECK (mark BETWEEN 0 AND 100)
name TEXT NULL NOT → The words are in the wrong order. NOT NULL is the keyword pair and it should be written as name TEXT NOT NULL.
CHECK age BETWEEN 0 AND 120 → The condition is missing its brackets. It should be CHECK (age BETWEEN 0 AND 120).

Key Term

CHECK (age BETWEEN 0 AND 120)
A constraint is a rule on a column. This CHECK rejects any record with an age outside 0 to 120. NOT NULL (must have a value) and UNIQUE (no repeats) are constraints too.

Fill in the Blanks

1. name TEXT NOT NULL,
Hint: Type the constraint that works as a presence check, so the column cannot be left empty.
2. email TEXT UNIQUE,
Hint: Type the constraint that stops two records from having the same value in this column.
3. age INTEGER CHECK (age BETWEEN 0 AND 120)
Hint: Type the operator that tests whether a value falls inside a range.
4. age INTEGER CHECK (age BETWEEN 0 AND 120)
Hint: Type the constraint keyword that applies a range rule to the column.
5. CREATE TABLE pupil ( age INTEGER CHECK (age BETWEEN 0 AND 120) );
Hint: Type the name of the column that the range rule is checking.

Spot the Error

Question 1
A CREATE TABLE student (
B id INT UNIQUE,
C age INT CHECK age >= 0,
D name TEXT NOT NULL
E );
Line C — The condition inside a CHECK constraint must be wrapped in brackets. It should be CHECK (age >= 0).
Question 2
A CREATE TABLE pet (
B name TEXT NO NULL,
C id INT UNIQUE
D );
Line B — The presence check constraint is written NOT NULL, not NO NULL. It stops the column being left empty.
Question 3
A CREATE TABLE player (
B id INT PRIMARY KEY,
C age INT NOT NULL,
D CHECK (age BETWEEN 0, 120)
E );
Line D — BETWEEN needs the keyword AND between the two limits, not a comma. It should be CHECK (age BETWEEN 0 AND 120).
Question 4
A CREATE TABLE member (
B email TEXT UNIQUE
C age INT NOT NULL
D );
Line B — There is a comma missing at the end of the line. Column definitions in CREATE TABLE are separated by commas, so it should be email TEXT UNIQUE,
Question 5
A CREATE TABLE ticket (
B id INT UNIQUE,
C qty INT CHECK (qty < 0),
D seat TEXT NOT NULL
E );
Line C — The rule is the wrong way round. It would only accept negative quantities. To accept only positive quantities it should be CHECK (qty > 0).

Student with a class code? Join your class →