Databases and SQL
Tables, records, fields and keys in a relational database, and SQL to search, sort, add, change and delete data.
Watch the lesson
Every point below, worked through in one video, and each point links straight to its own part.
Learn
Databases and SQL, explained point by point
Everything the GCSE specification expects you to be able to do, and how to actually do it - the same lesson a signed-in student studies from.
Describe how a database stores data in tables, records and fieldsOCR and AQA only
A database is an organised store of data. The data is held in tables. Each row of a table is a record, which is everything stored about one thing, such as one pupil. Each column is a field, one fact about that thing, such as a surname, and every field has a data type. OCR and AQA are the two boards that examine databases. Edexcel and Eduqas do not examine SQL.
Explain primary keys, foreign keys and why a relational database is usedAQA only
A primary key is a field whose value is different for every record, such as a pupil number, so that each record can be picked out exactly. A relational database has more than one table. A foreign key is a field in one table that holds the primary key of a record in another, which is what links them. Splitting data this way means each fact is stored once, which cuts wasted space, called redundancy, and stops the same fact appearing with two different values, called inconsistency. AQA examines this.
Retrieve data with SELECT, FROM and WHEREOCR and AQA only
A query asks a database for data. SELECT lists the fields you want to see, FROM names the table they are in, and WHERE gives the condition a record must meet to be included. So SELECT Surname FROM Pupils WHERE Year = 11 shows the surname of everyone in Year 11. Leave out WHERE and every record is returned. OCR also uses a star in place of the field list to mean every field. Text values go in quotation marks and numbers do not.
Sort the results of a query with ORDER BYAQA only
ORDER BY puts the results in order of a field. It goes after WHERE. Add ASC for ascending order, which is smallest first or A to Z, or DESC for descending. Without ORDER BY the records come back in no promised order, so when a question shows a sorted result, the query needs this line. AQA examines ORDER BY.
Retrieve data from two tablesAQA only
When the data you need is spread over two tables, name both after FROM, and use WHERE to match the foreign key in one to the primary key in the other. Without that match the query would pair every record with every record. Where both tables have a field of the same name, write the table name, a dot and then the field name to say which one you mean. AQA’s queries use at most two tables.
Add, change and delete data with INSERT, UPDATE and DELETEAQA only
INSERT INTO adds a new record: name the table, then give the values in the order of its fields. UPDATE changes existing records: name the table, use SET to give the field its new value, and use WHERE to say which records to change. DELETE FROM removes the records that match its WHERE. The WHERE is what keeps the change to the records you meant. Leave it off an UPDATE or a DELETE and every record in the table is affected. AQA examines these three.
Databases and SQL key terms
The words the specification and the mark schemes use, each defined the way an examiner wants it.
- Database
- An organised collection of data stored so that it can be searched and updated easily.
- Table
- A set of records in a database that all have the same fields.
- Record
- One row of a table, holding all the data stored about a single item.
- Field
- One column of a table, holding a single item of data about each record.
- Primary key
- A field that has a different value in every record, used to identify each record uniquely.
- Foreign key
- A field in one table that holds the primary key of a record in another table, linking the two.
- Relational database
- A database made of two or more tables that are linked by keys.
- Data redundancy
- The same item of data being stored more than once when it does not need to be.
- SQL
- Structured Query Language: the language used to search and change the data in a database.
- Query
- A request to a database for the records that meet a stated condition.
Common mistakes in Databases and SQL
The slips that cost marks year after year, and what to do instead.
Writing the three parts of a query in the wrong order
The order is always SELECT the fields, FROM the table, WHERE the condition. ORDER BY goes last.
Leaving quotation marks off text values in a condition
Text goes in quotation marks and numbers do not, so a surname is quoted and an age is not.
Leaving WHERE off an UPDATE or a DELETE
Without WHERE the change is made to every record in the table. Add the condition that picks out the records you mean.
Mixing up a record and a field
A record is a whole row about one item. A field is a single column, such as the surname.
Choosing a primary key that could repeat
A name or a date of birth can be shared. A primary key must be different for every record, such as an ID number.
Practice
Try a Databases and SQL question
A GCSE-style original question from this topic. Have a go before you open the working - deciding on an answer first is what makes the working stick.
Answer: SELECT Title FROM Films WHERE Year > 2020
After 2020 means a year greater than 2020, and the field to show is Title.
- The field to show is Title.
- After 2020 means Year > 2020.
- So the query is SELECT Title FROM Films WHERE Year > 2020.
