Topic 9 · Databases
Databases and SQL
Tables, records, fields, data types, and primary keys, and how to query a single-table database with SQL.
Tables, records, and fields
A database is a structured way to store data so it can be searched, sorted, and updated easily. A single-table database stores everything in one table.
Each row is a record: all the data about one thing (here, one pet). Each column is a field: one piece of data stored for every record (here, the pet's name, type, and so on).
| PetID | Name | Type | Age | Owner |
|---|---|---|---|---|
| P01 | Bella | Dog | 3 | Khan |
| P02 | Milo | Cat | 5 | Chen |
| P03 | Coco | Rabbit | 1 | Okafor |
| P04 | Max | Dog | 7 | Silva |
| P05 | Luna | Cat | 2 | Patel |
| P06 | Rocky | Dog | 4 | Chen |
Data types for fields
Each field is given a data type, which also works as validation:
| Data type | Holds | Example field |
|---|---|---|
| Text / alphanumeric | Letters, digits, and symbols | Name, PetID, phone number |
| Character | A single character | Size: S, M, or L |
| Boolean | True / false (yes / no) | Vaccinated |
| Integer | Whole numbers | Age |
| Real | Numbers with decimals | Weight in kg |
| Date / time | A date or time | DateOfBirth |
Primary keys
A primary key is a field that holds a unique value for every record, so each record can be identified. Names are not good keys because two pets could share a name; PetID is unique.
Key idea. No two records can have the same primary key value, and it can never be empty.
SELECT, FROM, WHERE, ORDER BY
SQL (Structured Query Language) is used to search a database. A query names the fields to show (SELECT), the table (FROM), the condition records must meet (WHERE), and how to sort the results (ORDER BY ... ASC or DESC).
In the diagram, the faded rows fail the WHERE condition and the highlighted columns are the ones selected.
SELECT Name, Age FROM Pets WHERE Type = 'Dog' ORDER BY Age DESC;
| PetID | Name | Type | Age | Owner |
|---|---|---|---|---|
| P01 | Bella | Dog | 3 | Khan |
| P02 | Milo | Cat | 5 | Chen |
| P03 | Coco | Rabbit | 1 | Okafor |
| P04 | Max | Dog | 7 | Silva |
| P05 | Luna | Cat | 2 | Patel |
| P06 | Rocky | Dog | 4 | Chen |
| Name | Age |
|---|---|
| Max | 7 |
| Rocky | 4 |
| Bella | 3 |
Combining conditions
Use AND and OR to build more specific conditions, and comparison operators such as <, >, and <> (not equal).
SELECT Name, Owner FROM Pets WHERE Age < 4 AND Type <> 'Dog';
| PetID | Name | Type | Age | Owner |
|---|---|---|---|---|
| P01 | Bella | Dog | 3 | Khan |
| P02 | Milo | Cat | 5 | Chen |
| P03 | Coco | Rabbit | 1 | Okafor |
| P04 | Max | Dog | 7 | Silva |
| P05 | Luna | Cat | 2 | Patel |
| P06 | Rocky | Dog | 4 | Chen |
| Name | Owner |
|---|---|
| Coco | Okafor |
| Luna | Patel |
SUM and COUNT
COUNT returns how many records match. SUM adds up the values in a numeric field for the matching records.
SELECT COUNT(PetID) FROM Pets WHERE Type = 'Cat'; -- Result: 2 SELECT SUM(Age) FROM Pets WHERE Owner = 'Chen'; -- Result: 9 (Milo 5 + Rocky 4)
Key terms
- Record
- A row in a table: all the data about one item.
- Field
- A column in a table: one category of data.
- Primary key
- A field that uniquely identifies each record.
- SQL
- Structured Query Language, used to query databases.
Check yourself
How many records and fields does the Pets table have?Show answer
6 records and 5 fields.
Why is Owner not a suitable primary key?Show answer
It is not unique: Chen owns two pets.
Write a query to show the PetID of every cat, youngest first.Show answer
SELECT PetID FROM Pets WHERE Type = 'Cat' ORDER BY Age ASC;
What does SELECT COUNT(PetID) FROM Pets WHERE Age > 3; return?Show answer
3 (Milo 5, Max 7, Rocky 4).