Data in tables
After this: Read the school's four tables: rows, columns, and one row read by eye.
On this page · 5 sections · 3 checks
A school keeps track of its students, its teachers, its courses, and which student takes which course. Before computers this lived on paper: a sheet with one line for each student, and a column for the name, the year and the city. Many programs still keep their data the same way, as sheets of lines and columns. This tutorial is about the program that looks after those sheets.
A is a store of data that is organised so that you can ask it questions. This lesson shows how a database lays out its data. Lesson 2 says why you would want one.
One table: the students
Here are the school's six students, as the database keeps them.
| id | name | year | city |
|---|---|---|---|
| 1 | Ana | 2 | Leeds |
| 2 | Ben | 1 | York |
| 3 | Chloe | 2 | Leeds |
| 4 | Dev | 3 | Hull |
| 5 | Eve | 1 | York |
| 6 | Femi | 3 | Leeds |
This grid is called a , and its name is students. Each line across is a : one row is one student. Each line down is a : it has a name at the top and holds the same kind of fact for every student.
So the students table has 6 rows and 4 columns. Every row has a value in each of the four columns, and in this table each column holds one kind of value: year holds whole numbers, city holds text.
Reading one row by eye
Find Chloe and read her row.
- Go down the
namecolumn until you reach Chloe. She is on the third row. - Read across that row:
id3,nameChloe,year2,cityLeeds.
That is everything the table knows about Chloe. A row in a table is a lot like one dictionary from "Nested data" (a map in Java): the column names are the keys, and the row holds one value for each.
Four tables for one school
The school keeps three more tables. The teachers:
| id | name | room |
|---|---|---|
| 1 | Mrs Okafor | R1 |
| 2 | Mr Silva | R2 |
| 3 | Ms Chen | R3 |
The courses. The last column, teacher_id, holds the id of the teacher who teaches the course:
| id | title | credits | teacher_id |
|---|---|---|---|
| 1 | Maths | 4 | 1 |
| 2 | Physics | 4 | 1 |
| 3 | History | 3 | 2 |
| 4 | Art | 2 | 2 |
| 5 | Music | 2 | 3 |
And the enrolments: one row for each time a student takes a course, with the mark they got.
| student_id | course_id | mark |
|---|---|---|
| 1 | 1 | 72 |
| 1 | 2 | 65 |
| 1 | 3 | 80 |
| 2 | 1 | 58 |
| 2 | 4 | 90 |
| 3 | 1 | 85 |
| 3 | 3 | |
| 4 | 2 | 47 |
| 4 | 4 | 76 |
| 6 | 1 | 91 |
The enrolments table holds numbers, not names. To read the row 3, 1, 85, look the numbers up:
student_id3 is the student whoseidis 3 in the students table: Chloe.course_id1 is the course whoseidis 1 in the courses table: Maths.- So the row says: Chloe takes Maths, and her mark is 85.
The row 3, 3 has an empty mark: Chloe takes History, and nobody has marked her work yet. The database writes a missing value like this as NULL, and the next tutorial shows how to work with it.
Why four tables and not one big one? Each fact is written once. Mrs Okafor's room is in one row of the teachers table, not copied next to every course she teaches. Lesson 3 shows how the numbers tie the tables together.
Check yourself
In the students table, what is one row?
How many rows and how many columns does the courses table have?
One row of the enrolments table reads 2, 4, 90 (student_id, course_id, mark). What does it say?
Honesty notes
- Say what a database is: a store of data, organised so that you can ask it questions.
- Say what a table, a row and a column are: a named sheet, one thing in it, and one kind of fact about every thing.
- Read one row by eye: find Chloe in the name column, then read across to id 3, year 2 and Leeds.
- Follow the numbers in an enrolments row to the student and the course they name: 3, 1, 85 is Chloe taking Maths with 85.