GCSE · Computer Science · AQA · Spec 8525

SQL INSERT INTO

A table is waiting for a new record. Two bracketed lists decide exactly where every value lands, and one small slip puts the right data in the wrong place.

Computer Science · Databases

INSERT INTO: watch each value find its field

Press Next to run the statement one value at a time. The table on the right is the table's contents.

Before you press Next: the statement has three values and three fields. Which field do you think the 12 will land in?

INSERT INTO tblLessons (LessonID, CourseID, MemberID)
VALUES (4, 12, 203);

Result

LessonIDCourseIDMemberID
110201
210202
311201

Output

 

Step 1: This is tblLessons before the statement runs: three records, three fields. The statement has not run yet, so nothing has changed.

1 / 5

Step through the trace — every value is the lesson’s, not run by the page.

Step 1 of 5: This is tblLessons before the statement runs: three records, three fields. The statement has not run yet, so nothing has changed..

Watch out: Position is the only thing deciding. A statement with its values swapped looks just as tidy, but the data lands in the wrong fields.

Computer Science · Databases

Now you write the VALUES line

Add a record to tblLessons for LessonID 5, CourseID 11 and MemberID 202.

  1. INSERT INTO tblLessons (LessonID, CourseID, MemberID)Line 1 is done for you: INSERT INTO, the table name, then the field names in brackets, separated by commas.
  2. missing step
Which line is step 2?

Two statements that look alike

CREATE TABLEvsINSERT INTO

Both name the table and use brackets. What goes inside the brackets is where they split.

Focus

What it makes

CREATE TABLE

A new table, with its fields

INSERT INTO

A new record in a table that already exists

The insight

One builds the table. The other puts something into it.

What goes in the brackets

CREATE TABLE

Each field name followed by its data type, e.g. LessonID INT

INSERT INTO

Line 1: the field names. VALUES line: the values.

When it happens

CREATE TABLE

First

INSERT INTO

Afterwards, once the table exists

Example

CREATE TABLE

CREATE TABLE tblLessons (LessonID INT, CourseID INT, MemberID INT);

INSERT INTO

INSERT INTO tblLessons (LessonID, CourseID, MemberID) VALUES (6, 10, 205);

Computer Science · Databases

Find the faulty line

This script should create a table and then add lesson 8, on course 10, taken by member 205. One line is faulty. Which one?

A student's script — which line goes wrong?

Computer Science · Databases

Accepted or rejected?

tblLessons already holds LessonID 1, 2 and 3. LessonID is a PRIMARY KEY and CourseID is NOT NULL. Will each INSERT be accepted or rejected, and why?

Still to sort

Accepted (0)

Breaks no rule.

Where the line is: If every rule is kept, the record goes in.

Rejected (0)

Breaks at least one rule.

Where the line is: One broken rule is enough: the table refuses the record.

5 of 5 still to sort.

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

Two bracketed lists, lined up side by side. Work out where every value lands, then see what happens when the table says no.

What you need to know

  • SQL (structured query language) manages data in relational databases, and its statements must follow syntax rules to execute correctly.
  • An INSERT statement adds data into a table that already exists.
  • Start with INSERT INTO, then the table name, then the field names in brackets, separated by commas.
  • On the next line, write VALUES, then the values, also in brackets and separated by commas.
  • Order is everything: values go in the same order as the field names, so each lands in its matching field.
  • Have a goSam says: "SQL is clever, it will spot that 203 is obviously a member number." The fields are (LessonID, CourseID, MemberID) and Sam writes VALUES (203, 4, 12). Where does 203 really go?

    Into LessonID, because it is the first value.

    SQL matches by position only. It never judges a value by how it looks, so Sam's 203 lands in the first field.

  • CREATE TABLE makes a new table first: the table name, then the fields with their data types in brackets.
  • Have a goYour friend has a brand new idea for a table that does not exist yet. Which do you run first: INSERT INTO or CREATE TABLE?

    CREATE TABLE.

    A table must exist before records can be inserted, so the table comes first and the records second.

  • Constraints are rules the data must follow. NOT NULL means a column will not accept a null value.
  • PRIMARY KEY means every row has a unique, non-null value in that column.
  • Have a goA column is a PRIMARY KEY. A record arrives with no value at all in that column. Can the table accept it?

    No. A primary key needs a value in every row.

    Primary key means unique and non-null, so a row with nothing in that column breaks the rule.

  • Break a rule and the INSERT is rejected.
  • A foreign key is a value that exists as a primary key in another table.

The big picture

SQL manages data in relational databases. An INSERT statement adds a record to a table that already exists: INSERT INTO, the table and its field names in brackets, then VALUES and the values in brackets, in the same order as the fields. The table has to be made first with CREATE TABLE, and constraints such as NOT NULL and PRIMARY KEY make the table reject any INSERT that breaks its rules.

Key points

1INSERT INTO table (fields) VALUES (values); the first value goes in the first field, the second in the second, and so on.
2The table has to exist first: CREATE TABLE comes before INSERT INTO.
3NOT NULL and PRIMARY KEY are rules the data must follow. An INSERT that breaks one is rejected.
4Swapped values still look tidy but land in the wrong fields, so always read the two lists side by side.

Worked example

Problem

Work out the record that this statement adds to tblLessons: INSERT INTO tblLessons (MemberID, LessonID, CourseID) VALUES (206, 9, 13);

⚠ Watch out

Thinking SQL works out which value belongs in which field, or that a table accepts any data. It does not: values are matched to fields by position only, and constraints such as PRIMARY KEY and NOT NULL make the table reject rows that break its rules.

🧠

Memory hook

Field names on top, values underneath, like pegs lined up with labelled holes: the first peg goes in the first hole, whatever the peg looks like. And the constraints are the doorman: break a rule and the record does not get in.

✓

Check yourself

Cover the page. Write the two lines of an INSERT for a record of your own in tblLessons, then say which value lands in the second field and one rule an INSERT could break.

Flashcards

(11)
What is SQL used for?
Managing data within relational databases. Standardised commands let users interact with and interrogate the data.
What does an SQL INSERT statement do?
It adds data into a table that already exists.
What is the basic shape of an INSERT statement?
INSERT INTO table (field1, field2) VALUES (value1, value2);
How does SQL decide which value goes into which field?
By position only. The values are in the same order as the field names, so the first value goes in the first field, the second in the second, and so on.
What goes wrong if two values in the VALUES line are swapped?
Each value is matched by position, so they land in the wrong fields, even though the statement still looks tidy.
Which statement makes a new table, and what goes in its brackets?
CREATE TABLE, then the table name, then the fields with their data types in brackets. The table has to exist before records can be inserted.
What are INT, FLOAT, CHAR, VARCHAR and DATE examples of?
Data types, which are given to each field when a table is created. They are examples, not a complete list.
What does the NOT NULL constraint do?
A column with NOT NULL will not accept a null value.
What does PRIMARY KEY mean for a column?
Every row has a unique, non-null value in that column.
What happens to an INSERT that breaks a constraint?
It is rejected: the table does not accept the record.
What is a foreign key?
A value that exists as a primary key in another table.

Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.

Learning with Lightbulb is opening soon

You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.

Keep me posted

More AQA GCSE Computer Science topics

See the full AQA Computer Science curriculum →

How this lesson was checked. This AQA GCSE Computer Science (specification 8525)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 4 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.