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

Preview

Unit 6 B2: Creating tables, rules and imports

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

From paper to a working table

Oakfield Youth Club is made up. The design is on paper. Now the technician has to build it.

Building a table is quick when the plan is good. Each line of the data dictionary (a table that describes each field) turns into one line of the table. This lesson shows how, using SQL (a language for working with tables of data).

By the end of this lesson

Note

I can build a table from a data dictionary, with rules, and add records to it.

Key words

WordWhat it means
Constrainta rule the table enforces on its data
CREATE TABLEthe SQL words that build a new table
INSERTthe SQL word that adds a record to a table
Importto bring data in from another file
CSV filea text file with one record per line and commas between fields
Integera whole number
Range checka check that a number is between two limits
Primary keya field that is different for every record

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

Starter: what you already know

Have a go

Starter: what you already know

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

Each dictionary row becomes a line of SQL

In CREATE TABLE, you name the table and then list its fields. After each field name you write its type and its rules.

A constraint is a rule the table enforces. If a new record breaks it, the table refuses the record. The refusal is validation (checking entered data) at work.

Worked example: dictionary to SQL

In the data dictionaryIn the SQL
Primary key, whole numberINTEGER PRIMARY KEY
Presence check (must not be empty)NOT NULL
Range check, 11 to 19CHECK (age BETWEEN 11 AND 19)
List check (a set of days)CHECK (weekday IN ('Monday', 'Tuesday'))
Default value of 0DEFAULT 0
No two the sameUNIQUE

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

Try it: build the activities table

Code: sql
-- The members table is already built. Here we build activities.
CREATE TABLE activities (
  activity_id INTEGER PRIMARY KEY,
  activity_name TEXT NOT NULL UNIQUE,
  weekday TEXT NOT NULL CHECK (weekday IN ('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday')),
  fee REAL NOT NULL DEFAULT 0 CHECK (fee >= 0),
  places_left INTEGER NOT NULL CHECK (places_left BETWEEN 0 AND 30)
);
INSERT INTO activities (activity_name, weekday, fee, places_left) VALUES
  ('Football', 'Tuesday', 2.5, 12),
  ('Chess', 'Friday', 0, 8);

-- Remove the two dashes on the next line to see the table refuse a record.
-- INSERT INTO activities (activity_name, weekday, fee, places_left) VALUES ('Art', 'Funday', 1, 5);

SELECT * FROM activities;

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

5 more parts of this lesson are in the full lesson, which colleges teach from the Library. A college account gives your students all of it.