KS5H446 1.3.2 32 min read 10 question quiz 23 exam marks

Databases

Normalisation to third normal form, entity relationship modelling, SQL, indexing, transactions and the ACID properties.

Structure and normalisation

Key terms

  • Entity: something data is stored about, becoming a table
  • Attribute: a property of an entity, becoming a field
  • Record or tuple: one row
  • Primary key: an attribute uniquely identifying each record
  • Composite key: a primary key made of two or more attributes together
  • Foreign key: an attribute in one table that is the primary key of another, creating the link
  • Secondary key: an additional indexed field used for searching

Relationships

  • One to one: rare, and usually a sign the tables should be merged
  • One to many: the most common. The foreign key goes on the many side
  • Many to many: cannot be implemented directly and must be resolved with a linking table holding foreign keys to both

Normalisation

First normal form (1NF). No repeating groups, and every field holds a single atomic value. Each record is uniquely identifiable.

Second normal form (2NF). In 1NF, and every non key attribute depends on the whole primary key, not just part of a composite key.

Third normal form (3NF). In 2NF, and no non key attribute depends on another non key attribute. There are no transitive dependencies.

The usual summary: every non key attribute depends on the key, the whole key, and nothing but the key.

Why normalise

  • Removes redundancy, so the same fact is stored once
  • Removes update anomalies, where changing a fact in one place leaves other copies wrong
  • Removes insertion anomalies, where a record cannot be added without unrelated data
  • Removes deletion anomalies, where deleting a record destroys unrelated information
  • Reduces storage and improves data consistency

The cost is more tables and therefore more joins, which can slow complex queries. This is why some analytical systems deliberately denormalise for read performance.

Tables, keys and the link between themA relational database drawn as separate tables. One table holds students, with a row for each student and a column for each field: StudentID, Name and Year. One field is chosen as the primary key, here StudentID, and its value must be different in every row and must never be reused, which is why a name makes a poor key and an ID number makes a good one. A second table holds courses, with CourseID as its own primary key. The two are joined by a third table of enrolments holding pairs of StudentID and CourseID, and in that table each of those fields is a foreign key, meaning a field that holds the primary key of another table. In the picture the enrolment row S01 and C01 points back at student S01 and course C01. Splitting the data this way is normalisation, and the reason for it is that every fact is then stored exactly once: when a course changes teacher you change one row in the courses table rather than three hundred rows in one enormous table, and there is no way for two of those rows to end up disagreeing.StudentsStudentIDNameYearS01Aisha KhanY10S02Tom ReillyY11S03Mia OkaforY10primary key: different in every rowCoursesCourseIDSubjectTeacherC01ComputingMr AliC02PhysicsMs DaleC03ArtMr Boydits own primary keyEnrolmentsStudentIDCourseIDS01C01S01C03S02C02foreign keyforeign keyAisha is stored once,even though shetakes two courses.Change a teacheronce, not on300 rows.12345
  1. One table for students. Each row is a student, each column a field.
  2. A primary key: one field whose value is different in every row.
  3. A second table for courses, with its own primary key.
  4. A foreign key in one table holds the primary key of the other.
  5. That link is why the data is stored once and not repeated.
Store every fact once. Almost every database exam answer comes back to that sentence.

SQL, indexing and transactions

Querying

SQL
SELECT Name, Grade
FROM Students
WHERE Year = 11 AND Grade >= 7
ORDER BY Grade DESC;

SELECT Students.Name, Courses.Title
FROM Students
INNER JOIN Enrolments ON Students.ID = Enrolments.StudentID
INNER JOIN Courses ON Enrolments.CourseID = Courses.ID
WHERE Courses.Title LIKE 'Comp%';

SELECT Year, COUNT(*) AS Total, AVG(Grade) AS MeanGrade
FROM Students
GROUP BY Year
HAVING COUNT(*) > 5;

Modifying

SQL
INSERT INTO Students (Name, Year, Grade) VALUES ('Aisha', 11, 8);

UPDATE Students SET Grade = 9 WHERE Name = 'Aisha';

DELETE FROM Students WHERE Year = 13;

CREATE TABLE Courses (
    ID      INTEGER PRIMARY KEY,
    Title   VARCHAR(60) NOT NULL,
    Credits INTEGER
);

ALTER TABLE Courses ADD Department VARCHAR(40);
UPDATE and DELETE without WHERE affect every row

This is one of the most damaging mistakes in professional practice, and it is entirely preventable.

Indexing

An index is a separate structure mapping the values of a field to the locations of matching records.

  • Searches and sorts on that field become far faster, since the whole table need not be scanned
  • Every insert, update and delete becomes slower, because the index must also be maintained
  • The index consumes additional storage

Index fields that are searched frequently. Do not index fields that are written far more often than they are read.

Transactions and ACID

A transaction is a group of operations treated as a single indivisible unit.

  • Atomicity. All operations succeed or none do. A transfer that debits one account must not fail after the debit but before the credit.
  • Consistency. The database moves from one valid state to another, with all rules and constraints satisfied.
  • Isolation. Concurrent transactions do not interfere. The result matches some sequential ordering of them.
  • Durability. Once committed, changes survive a crash or power failure, guaranteed by writing to non volatile storage.

Concurrency control

  • Record locking prevents two users modifying the same record simultaneously, but can produce deadlock where each transaction waits for a record the other holds
  • Timestamp ordering gives each transaction a timestamp and resolves conflicts by ordering, avoiding deadlock at the cost of restarting some transactions
  • Serialisation ensures the outcome of concurrent transactions matches some sequential execution of them
An SQL query labelled clause by clause, and what each clause does to the tableAn SQL query is read clause by clause. SELECT name, mark chooses which columns. FROM results says which table. WHERE mark greater than 60 chooses which rows. ORDER BY mark DESC puts them in order, highest first. Applied to a table of five pupils, id, name and mark, WHERE drops Ben on 55 and Eli on 48 and keeps three rows. SELECT keeps only the name and mark columns. ORDER BY puts the result in order: Chloe 91, Aisha 72 and Dev 64.query.sqlSELECT name, markFROM resultsWHERE mark > 60ORDER BY mark DESC;which columnswhich tablewhich rowsin what order1The table: results2idnamemark1Aisha722Ben553Chloe914Dev645Eli48WHERE mark > 60 keeps 3 rows and drops 2The result3namemarkChloe91Aisha72Dev64only the two chosen columns,highest mark first
  1. Each clause has a job: SELECT picks columns, FROM names the table, WHERE picks rows, ORDER BY sorts.
  2. WHERE keeps the rows that meet the condition and drops the rest.
  3. The result has only the selected columns, in the order asked for.
Read a query in the order FROM, WHERE, SELECT, ORDER BY: pick the table, filter the rows, choose the columns, then sort.
The four ACID properties of a database transaction: atomicity, consistency, isolation and durabilityThe four properties that make a database transaction reliable. Atomicity means all or nothing: moving 50 pounds from account A to account B either completes both steps, which is committed, or if one step fails both are undone. Consistency means the rules always hold: before the move A and B have 100 pounds each, after it they have 50 and 150, the total is still 200 pounds, and no account goes below zero. Isolation means transactions cannot see each other's half finished work: transaction 2 waits while transaction 1 has the balance locked, then runs after it commits. Durability means that once a transaction is committed it stays, even if the power is cut.Atomicity1all or nothingAccount A£100Account B£0move £50both steps done: commitone step fails: undo bothConsistency2the rules always holdbeforeA£100B£100total £200afterA£50B£150total £200no account may go below £0Isolation3no peeking at half-finished workTransaction 1works on balanceTransaction 2waits, then runscommitDurability4once committed, it stayspower cutstill there
  1. Atomicity: a transaction is all or nothing.
  2. Consistency: the database goes from one valid state to another, and the rules always hold.
  3. Isolation: concurrent transactions do not see each other's half finished work.
  4. Durability: committed data survives a crash.
Learn the four words and give the bank transfer as the example for each.

Key terms you must be able to define

Entity
Something that data is stored about, implemented as a table.
Primary key
An attribute or set of attributes uniquely identifying each record in a table.
Foreign key
An attribute in one table that is the primary key of another, forming the link between them.
Composite key
A primary key made up of two or more attributes together.
Normalisation
Organising data to remove redundancy and eliminate update, insertion and deletion anomalies.
Third normal form
A table in second normal form with no non key attribute depending on another non key attribute.
Transitive dependency
A non key attribute depending on another non key attribute rather than directly on the key.
Linking table
A table resolving a many to many relationship by holding foreign keys to both entities.
Index
A structure mapping field values to record locations, speeding searches at the cost of slower writes.
Transaction
A group of database operations treated as a single indivisible unit.
ACID
Atomicity, Consistency, Isolation and Durability, the guarantees a reliable transaction system provides.
Deadlock
A state where two transactions each hold a lock the other needs, so neither can proceed.

Flashcards

Say the definition out loud before you flip the card. Retrieving it from memory is what builds recall, reading it again is not.

DefineEntity
AnswerSomething that data is stored about, implemented as a table.
DefinePrimary key
AnswerAn attribute or set of attributes uniquely identifying each record in a table.
DefineForeign key
AnswerAn attribute in one table that is the primary key of another, forming the link between them.
DefineComposite key
AnswerA primary key made up of two or more attributes together.
DefineNormalisation
AnswerOrganising data to remove redundancy and eliminate update, insertion and deletion anomalies.
DefineThird normal form
AnswerA table in second normal form with no non key attribute depending on another non key attribute.
DefineTransitive dependency
AnswerA non key attribute depending on another non key attribute rather than directly on the key.
DefineLinking table
AnswerA table resolving a many to many relationship by holding foreign keys to both entities.

Reach the top grade on this topic

  • Apply 1NF, 2NF and 3NF to an actual table rather than reciting the definitions
  • Name the anomalies removed by normalisation: update, insertion and deletion
  • Resolve a many to many relationship with a correctly described linking table
  • Write SQL with joins, aggregate functions and GROUP BY, not just SELECT and WHERE
  • Explain each ACID property with a concrete failure it prevents
  • State the cost of indexing as well as the benefit

Mistakes that cost marks here

Avoid these
  • Reciting normal form definitions without applying them to the table in the question.
  • Putting the foreign key on the wrong side of a one to many relationship. It belongs on the many side.
  • Implementing a many to many relationship directly rather than with a linking table.
  • Forgetting the WHERE clause on UPDATE or DELETE.
  • Describing indexing as purely beneficial, with no mention of slower writes or extra storage.
  • Confusing atomicity with consistency. Atomicity is all or nothing, consistency is about valid states.
Fun fact

The ACID properties exist because of one question: what happens if the power fails halfway through moving money between two accounts? Every serious database is built around making that question answerable.

Knowledge check

0 of 10 answered
1Which normal form removes transitive dependencies?
2How is a many to many relationship implemented?
3Where does the foreign key go in a one to many relationship?
4What is an update anomaly?
5What does the atomicity property guarantee?
6What is one disadvantage of adding an index to a field?
7Which SQL clause filters groups after aggregation?
8What is deadlock?
9A table has a composite key and an attribute depending on only part of it. Which normal form is violated?
10Why might a data warehouse be deliberately denormalised?
Score 0 / 10

Exam-style questions

Five written questions in the style of the real paper. Write a full answer, then let the marker check it against the mark scheme.

23 marks
1Explain what is meant by a foreign key.2 marks
Explain

Mark scheme0 / 2
  • LAn attribute in one table that is the primary key of another table
  • LIt creates the link between the two tables
Model answer

A foreign key is an attribute in one table which holds values drawn from the primary key of another table. It is the mechanism that creates a relationship between the two, allowing each record in the first table to be associated with exactly one record in the second, and it is what makes joins possible when querying data spread across several tables.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
2Explain why a database should be normalised to third normal form.5 marks
Explain

Mark scheme0 / 5
  • LNormalisation removes redundant duplicated data
  • LUpdate anomalies are avoided, since a fact is changed in only one place
  • LInsertion anomalies are avoided, so records can be added independently
  • LDeletion anomalies are avoided, so removing a record does not destroy unrelated data
  • LLess storage is used and data consistency is improved
Model answer

Normalising to third normal form ensures that every non key attribute depends on the key, the whole key and nothing but the key, which means each individual fact is stored in exactly one place. The most important consequence is the elimination of anomalies. Without normalisation, an update anomaly occurs when a fact stored in several rows is changed in one of them and not the others, leaving the database internally inconsistent with no indication of which copy is right. An insertion anomaly occurs when a new record cannot be added because unrelated information is missing, for example being unable to record a new course until a student has enrolled on it. A deletion anomaly occurs when removing a record destroys information that was only stored there, such as losing all record of a department because its last employee left. Normalisation also reduces the total storage required, since data is not duplicated across many rows, and it improves overall data integrity because there is only ever one authoritative copy of each fact to keep correct.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
3Write an SQL query that returns the name and course title of every student enrolled on a course, given tables Students(ID, Name), Courses(ID, Title) and Enrolments(StudentID, CourseID).4 marks
Write

Mark scheme0 / 4
  • LSelects the required columns from the correct tables
  • LJoins Students to Enrolments on the student ID
  • LJoins Enrolments to Courses on the course ID
  • LUses correct SQL syntax throughout
Model answer

SELECT Students.Name, Courses.Title FROM Students INNER JOIN Enrolments ON Students.ID = Enrolments.StudentID INNER JOIN Courses ON Enrolments.CourseID = Courses.ID; The Enrolments table is the linking table resolving the many to many relationship between students and courses, so two joins are needed. The first connects each student to their enrolment records by matching the student ID, and the second connects each enrolment to the course it refers to by matching the course ID. Because the columns Name and Title come from different tables, they are qualified with their table names to avoid ambiguity.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
4Explain what is meant by the ACID properties of a transaction.6 marks
Explain

Mark scheme0 / 6
  • LAtomicity means all operations in the transaction succeed or none do
  • LGives an example such as a bank transfer not leaving money missing
  • LConsistency means the database moves from one valid state to another
  • LIsolation means concurrent transactions do not interfere with one another
  • LDurability means committed changes survive a system failure
  • LTogether they guarantee the database remains reliable despite failures and concurrency
Model answer

Atomicity means that a transaction is indivisible: either every operation within it completes or none of them does. The standard example is transferring money between accounts, where debiting one account and crediting the other must happen together, since a failure between the two would make money simply disappear. Consistency means that a transaction takes the database from one valid state to another, with every rule, constraint and relationship still satisfied when it completes, so a transaction that would violate a foreign key constraint is rejected rather than applied. Isolation means that transactions running at the same time do not interfere with each other, and the result is as though they had run one after another in some order, which prevents one transaction reading data another has partially modified. Durability means that once a transaction has been committed, its changes are permanent and will survive a crash or power failure, which is achieved by writing to non volatile storage and maintaining a transaction log that can be replayed after a restart. Together these four properties are what allow a database to be trusted with financial and other critical data despite hardware failures and thousands of simultaneous users.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
5A table stores StudentID, StudentName, CourseID, CourseTitle and TutorName. Explain why this table is not in third normal form and describe how it should be restructured.6 marks
Explain

Mark scheme0 / 6
  • LA student can take several courses, so there is a many to many relationship
  • LCourseTitle depends on CourseID rather than on the whole key
  • LThis is a partial dependency violating second normal form
  • LTutorName depends on CourseID, which is a transitive dependency violating third normal form
  • LCreate a Students table and a Courses table
  • LCreate a linking table holding StudentID and CourseID as foreign keys
Model answer

The table is not in third normal form for several reasons. Because a student can take more than one course and a course has many students, the relationship is many to many, so identifying a row requires a composite key of StudentID and CourseID. Once that is recognised, StudentName depends only on StudentID and CourseTitle depends only on CourseID, so both are partially dependent on part of the composite key rather than on the whole of it, which breaks second normal form and causes the student's name and the course title to be repeated in every row. TutorName is worse still: it depends on CourseID, which is not itself the whole key, so it is a transitive dependency and breaks third normal form. The consequences are redundancy and anomalies, since changing a course title requires updating many rows and deleting the last enrolment on a course loses all record of that course and its tutor. The correct structure is three tables. A Students table holds StudentID as its primary key with StudentName. A Courses table holds CourseID as its primary key with CourseTitle and TutorName, or with a TutorID foreign key if tutors need their own table. An Enrolments linking table holds StudentID and CourseID as foreign keys, together forming its composite primary key, which resolves the many to many relationship into two one to many relationships and stores each fact exactly once.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
Machine mark 0 / 23