IB Diploma Computer Science · Concepts of computer science
Databases
1Relational databases
A database is an organised, persistent store of data that many users and programs can query and update. Most organisations use relational databases, which store data in tables (relations).
- Each record (row, or tuple) holds data about one item, such as one student.
- Each field (column, or attribute) holds one piece of data, such as a surname, with a data type: integer, real, text (VARCHAR), Boolean or date.
- A primary key uniquely identifies each record, such as StudentID. It must be unique and never empty.
- A foreign key is a field in one table that is the primary key of another table. It links the tables.
- A composite key is a primary key made of two or more fields together.
Why not one big spreadsheet? Storing data in separate linked tables avoids data redundancy (the same data stored many times), which wastes space and causes inconsistency when one copy is updated and another is not.
a school database might have a Students table (StudentID, Name, Year) and a Courses table (CourseID, Title, Teacher), linked by an Enrolments table.
Check yourself
What must be true of a primary key?
Show the answer
It must be unique for every record and never empty
It identifies each record exactly.
2Relationships and entity-relationship diagrams
An entity is a thing we store data about (student, course). Relationships between entities can be:
- One-to-one (1:1): each student has one locker, and each locker belongs to one student.
- One-to-many (1:M): one teacher teaches many classes, but each class has one teacher. The foreign key goes in the "many" table.
- Many-to-many (M:N): a student takes many courses, and a course has many students. Relational databases cannot store this directly, so it is broken into two one-to-many relationships with a link (junction) table, here Enrolments, whose primary key is the composite (StudentID, CourseID).
An entity-relationship (ER) diagram shows entities as boxes and relationships as lines, often with "crow's feet" for the "many" end.
Entity-relationship diagram: students, enrolments and courses
Check yourself
How is a many-to-many relationship stored in a relational database?
Show the answer
With a link table that turns it into two one-to-many relationships
Enrolments links Students and Courses.
3Normalisation
Normalisation organises tables to remove redundancy and avoid anomalies (problems when inserting, updating or deleting data).
- First normal form (1NF): every field holds a single (atomic) value, there are no repeating groups, and each record has a primary key. So "Courses: Maths, Physics" in one cell must be split up.
- Second normal form (2NF): in 1NF, and every non-key field depends on the whole primary key, not just part of a composite key. (Only matters when the key is composite.)
- Third normal form (3NF): in 2NF, and no non-key field depends on another non-key field (no transitive dependencies). "The key, the whole key and nothing but the key."
Removing a transitive dependency
- A Classes table has (ClassID, TeacherID, TeacherName).
- TeacherName depends on TeacherID, not on ClassID, so this is not in 3NF. If a teacher changes their name, many rows must be updated.
- Split it: Classes (ClassID, TeacherID) and Teachers (TeacherID, TeacherName).
- TeacherID in Classes is now a foreign key, and each name is stored once.
Fully normalised databases can need many joins, so some systems (such as data warehouses built for fast reporting) deliberately denormalise.
Check yourself
A table has a field "Phone numbers" holding three numbers in one cell. Which normal form does it break?
Show the answer
First normal form
Each field must hold one atomic value.
4SQL: querying data
SQL (Structured Query Language) is used to define, query and change relational databases.
A query to list Year 12 students in name order:
SELECT Name, Year
FROM Students
WHERE Year = 12
ORDER BY Name;
Joining tables using the foreign keys, to list who takes Physics:
SELECT Students.Name
FROM Students
JOIN Enrolments ON Students.StudentID = Enrolments.StudentID
JOIN Courses ON Courses.CourseID = Enrolments.CourseID
WHERE Courses.Title = 'Physics';
Summarising with aggregate functions:
SELECT Year, COUNT(*) AS NumStudents
FROM Students
GROUP BY Year;
WHEREfilters rows; conditions can be combined withAND,OR,NOT, andLIKE 'A%'matches text starting with A.ORDER BY ... DESCsorts in reverse.- Aggregate functions:
COUNT,SUM,AVG,MIN,MAX.GROUP BYgroups rows before aggregating;HAVINGfilters the groups.
Check yourself
Which SQL clause filters rows by a condition?
Show the answer
WHERE
ORDER BY only sorts.
5SQL: changing data, and integrity
Changing the data:
INSERT INTO Students (StudentID, Name, Year)
VALUES (1042, 'Amara Osei', 11);UPDATE Students SET Year = 12
WHERE StudentID = 1042;
DELETE FROM Students
WHERE StudentID = 1042;
Always include a WHERE with UPDATE and DELETE, or every row changes.
Keeping data correct:
- Entity integrity: every record has a unique, non-empty primary key.
- Referential integrity: every foreign key must match an existing primary key. The database will not let you enrol student 9999 if there is no student 9999, or delete a student who still has enrolments (unless set to cascade).
- Validation checks data is sensible before it is accepted: range check (age 11–19), type check, length check, format check (an email contains @), presence check.
- Verification checks data was entered correctly: double entry (type a password twice) or visual check against the source. Valid data can still be wrong: a mistyped but realistic date passes validation.
Check yourself
A student's year is entered as 21 but must be between 7 and 13. Which check catches this?
Show the answer
A range check
Validation checks that data is reasonable.
6Transactions, concurrency and wider issues
A transaction is a group of operations that must succeed or fail together, such as moving money: take it from one account and add it to another. Transactions follow ACID:
- Atomicity: all or nothing; if any step fails, the whole transaction is rolled back.
- Consistency: the database moves from one valid state to another, obeying all rules.
- Isolation: transactions running at the same time do not interfere with each other.
- Durability: once committed, changes survive crashes and power cuts.
Concurrency: when two users update the same record at the same time, one update can be lost. Record locking stops a second user changing a record until the first finishes.
Beyond relational databases:
- NoSQL databases (document, key-value, graph) handle huge volumes of loosely structured data and scale across many servers, as used by social media.
- A data warehouse combines historical data from many sources for analysis; data mining looks for patterns in it.
- Privacy and ethics: databases hold personal data, so laws such as the EU's GDPR require a lawful reason to store data, security, and giving people access to and control over their data.
Check yourself
What does atomicity mean for a transaction?
Show the answer
Either all of its steps happen or none of them do
A failed transfer must not leave money missing.
Now test yourself
This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.
Practise in Impera