Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

DA 2.9: Setting up a dataset with CREATE TABLE

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

50 minutes

A table that refuses bad data

Somebody types "tomorrow" in a column meant for dates. Somebody leaves a name empty. Somebody uses the same id twice.

A well set up table can refuse all three, before they spoil a report.

Key words

WordWhat it meansExample
Constrainta rule the table enforces on its dataNOT NULL
NULLa missing value
Primary keya column whose value is different in every row
Data typethe kind of value a column holds
Schemathe plan of a set of tables: columns and types
Dataseta collection of related data about one subject
Defaultthe value used when none is given

The cells students fill in are left blank in this preview.

By the end of this lesson

Note

I can write CREATE TABLE to set up a dataset from a design.

Starter

Have a go

Starter

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

CREATE TABLE turns a design into a table

A design says each column, its type and its rules. CREATE TABLE builds it.

List each column with its type. Then add constraints: PRIMARY KEY, NOT NULL (no missing value), UNIQUE, CHECK (a test the value must pass) and DEFAULT.

Try it: build and fill a club table

Code: duckdb
create table club_sessions (
  session_id integer primary key,
  club varchar not null,
  session_date date not null,
  pupils integer check (pupils >= 0),
  room varchar default 'Hall'
);

insert into club_sessions (session_id, club, session_date, pupils)
values (1, 'chess', '2026-04-03', 14), (2, 'choir', '2026-04-04', 22);

select * from club_sessions;

-- Try it: add a third session with 9 pupils.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

The lesson goes on

6 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.