Module 7 · Designing tables

Protect columns with constraints

By the end you will be able to declare what values a column accepts.

About 16 min

0 of 52 lessons

Before you start, you need

Declare primary and foreign keys

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.

Minimal syntax
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.

SQL
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.

  1. NOT NULL forces a title and a flag to be stored. The identity is declared with the key you have already learnt.
  2. DEFAULT 0 is used when an insert leaves out completed. CHECK stops a value such as 7 from being written.
  3. If you insert NULL explicitly, DEFAULT does not replace it. NOT NULL rejects that attempt.
  4. 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

Not started

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.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Protect presence, uniqueness and references

Not started

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.

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.

Next: Modify tables with ALTER TABLE