AS Level · Topic 8.3
A-Level Computer Science 9618 / Topic 8 / AS

Build the structure.

Before a single record can be stored, the database and its tables have to exist, with every field given a name and a data type, and the keys that link the tables declared. That is the job of the data definition language, and SQL is how you write it.

The key idea

The DBMS carries out all creation and modification of the database structure using its data definition language (DDL), and all queries and maintenance of the data using its data manipulation language (DML). The industry standard for both is SQL. The DDL statements you need are CREATE DATABASE, CREATE TABLE (each field with a data type: CHARACTER, VARCHAR(n), BOOLEAN, INTEGER, REAL, DATE, TIME), ALTER TABLE, PRIMARY KEY (field) and FOREIGN KEY (field) REFERENCES Table(Field).

anatomy of a CREATE TABLE statement CREATE TABLE Club( ClubID VARCHAR(3), ClubName VARCHAR(25), StartTime TIME, Indoor BOOLEAN, PRIMARY KEY (ClubID) ); the new table’s name one line per field: name, then data type the key, after the fields commas separate the lines; the statement ends with a semicolon
Fig. 1: CREATE TABLE names the table, defines each field and declares the primary key
Section 01

DDL, DML and data types.

SQL has two halves. DDL statements change the structure (the tables themselves); DML statements work with the data inside them. Every DDL statement in this lesson builds the library database used throughout this topic.

Data definition language (DDL)Data manipulation language (DML)
Works onthe structure: databases, tables, fields, keysthe data: the records in the tables
StatementsCREATE DATABASE, CREATE TABLE, ALTER TABLESELECT, INSERT INTO, UPDATE, DELETE FROM
SQL data typeHoldsExample field
CHARACTERa single character (fixed length)Grade: 'A'
VARCHAR(n)a string of up to n charactersSurname VARCHAR(20)
BOOLEANTRUE or FALSEReturned
INTEGERa whole numberPages
REALa number with a fractional partPrice
DATEa dateLoanDate
TIMEa time of dayStartTime
CREATE DATABASE Library;
Worked example Choosing data types
Choose a suitable SQL data type for each field of a CLUB table: ClubID (codes such as C01), ClubName, StartTime, Indoor (whether the club meets indoors), MaxMembers.
  1. ClubID: a short code of 3 characters, VARCHAR(3).
  2. ClubName: text of varying length, VARCHAR(25).
  3. StartTime: a time of day, TIME. Indoor: yes or no, BOOLEAN.
  4. MaxMembers: a whole number, INTEGER.
Answer: VARCHAR(3), VARCHAR(25), TIME, BOOLEAN, INTEGER
Examiner trap

An ID such as M01 or a phone number such as 07700 900123 is text, not INTEGER: it can have letters or a leading zero, and you never do arithmetic with it.

Section 02

CREATE TABLE.

In the lab, choose the example "Create a table" and run it: a new TEACHER table appears with its fields and data types. Then edit the statement and create a table of your own.

CREATE TABLE Member(
    MemberID VARCHAR(4),
    FirstName VARCHAR(20),
    Surname VARCHAR(20),
    FormGroup VARCHAR(3),
    PRIMARY KEY (MemberID)
);

CREATE TABLE Loan(
    LoanID VARCHAR(4),
    BookID VARCHAR(4),
    MemberID VARCHAR(4),
    LoanDate DATE,
    Returned BOOLEAN,
    PRIMARY KEY (LoanID),
    FOREIGN KEY (BookID) REFERENCES Book(BookID),
    FOREIGN KEY (MemberID) REFERENCES Member(MemberID)
);
Worked example Write the DDL
Write an SQL statement to create the table BOOK(BookID, Title, Author, Genre, Price), with BookID as the primary key.
  1. Start with CREATE TABLE Book( and give one line per field.
  2. BookID VARCHAR(4), Title VARCHAR(40), Author VARCHAR(30), Genre VARCHAR(10), Price REAL.
  3. End with PRIMARY KEY (BookID) and close the bracket with );
Answer:
CREATE TABLE Book(
    BookID VARCHAR(4),
    Title VARCHAR(40),
    Author VARCHAR(30),
    Genre VARCHAR(10),
    Price REAL,
    PRIMARY KEY (BookID)
);
Examiner trap

A foreign key field must have the same data type as the primary key it references. MemberID VARCHAR(4) in MEMBER needs MemberID VARCHAR(4) in LOAN, not INTEGER.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

Which statement belongs to the data definition language?

Quick check+10 XP

Which data type should store the price of a book, such as 8.50?

Quick check+10 XP

In the lab's "Create a table" example, which field is the primary key of TEACHER?

Quick check+10 XP

What is the industry standard language for both DDL and DML?

Section 03

ALTER TABLE.

Designs change after the tables have been created. ALTER TABLE changes a table definition without losing the data already stored. Try the lab examples "Add a field" and "Add a foreign key".

ALTER TABLE Book ADD Pages INTEGER;

ALTER TABLE Club ADD PRIMARY KEY (ClubID);

ALTER TABLE Loan ADD FOREIGN KEY (MemberID) REFERENCES Member(MemberID);
Worked example Link a new table
The library adds a table FORM(FormGroup, TutorName) with primary key FormGroup. Write the SQL to make FormGroup in MEMBER a foreign key.
  1. The table being changed is MEMBER, so start ALTER TABLE Member.
  2. ADD FOREIGN KEY (FormGroup) names the field in MEMBER.
  3. REFERENCES Form(FormGroup) names the table and primary key it links to.
Answer:
ALTER TABLE Member ADD FOREIGN KEY (FormGroup) REFERENCES Form(FormGroup);
Your turn
Write the SQL to add a field Email, up to 40 characters, to the MEMBER table.
Examiner trap

ALTER TABLE changes the structure. To change the values stored in a field you need the DML statement UPDATE, covered in the next lesson.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to AS Paper 1 and Paper 2 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

Which statement adds a field DueDate to the LOAN table?

Exam style+20 XP

Which line correctly declares a foreign key in LOAN?

Exam style+20 XP

A field stores a student's grade as one letter, such as B. Which data type is most suitable?

Exam style+20 XP

Why must the MEMBER table be created before the LOAN table?

Skill unlocked

SQL data definition language, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
DDL versus DML, choosing SQL data types, and writing CREATE DATABASE, CREATE TABLE and ALTER TABLE statements with primary and foreign keys.