CSwithZain
WhatsApp Join a class

AS Level 9618 · Paper 1 · 8.1

Database Concepts

Why relational databases replace files, and how to design one. Read the notes, try the past-paper questions, then check the mark scheme.

Past papers from June 2021 to June 202631 questions · 174 marks

    In this subtopic

    • Limits of a file-based approach, and how a relational database fixes them
    • Relational database terms: tables, keys and relationships
    • Entity-relationship (E-R) diagrams
    • Normalisation to third normal form (3NF)

    8.1 · 1 of 21Book pages Contents

    Limitations of a file-based approach: one file

    Before databases, data was often kept in a file-based approach: each program had its own text files, and each line of a file held the data for one item.

    Think of a theatrical agency that makes bookings for bands. It sets up a computerised system that uses text files. One text file stores data about individual band members. Each line of the file holds this data for one band member:

    • name
    • contact details
    • banking details
    • band name
    • band agent name
    • band agent contact details

    The file can be used when the agency needs to contact a band member, directly or through the band’s agent, and after a gig when the band member has to be paid. The file might have a thousand or more lines. Here is a selection of the given names, family names and band names from various lines:

    Given nameFamily nameBand name
    XiangfeiJhaComputerKidz
    MaheshRavuruITWizz
    DylanStoddart
    GrahamVandanaITWizz
    VandanaGrahamITWizz
    MaheshRavuruITWizz
    PreciousOlsenComputerKidz
    PreciousOlsenITWizz

    There are clear problems with this data:

    • When Vandana Graham’s data was first entered, her names were put in the wrong order. A correct entry was added later, but the wrong one was never deleted. No validation check could have found the first mistake, and this problem can happen in any system, not only a file-based one.
    • Dylan Stoddart has no band name. Validation should have caught this missing value.
    • The Precious Olsen lines are examples of duplicated data and inconsistent data: she is stored twice, in two different bands. (Mahesh Ravuru is also stored twice.)
    • A band name could also be typed in for a band that does not exist, and nothing in the file would show this.

    So a file-based approach can lead to data integrity problems in a single file. The reason is that there is no built-in control when data is entered. The database approach can prevent these problems, or at least make them less likely.

    A second problem is data privacy. The file was designed so that the finance section could find banking details and the recruitment section could find contact details. But there is no way to control access to only part of a file, so staff in recruitment could also see the band members’ banking details. A database system can handle data privacy properly.

    8.1 · 2 of 21Book page Contents

    Data redundancy and inconsistency in multiple files

    Because of the privacy problem, the agency decides to keep a separate file for each department. The table shows the main data stored in each department’s file:

    DepartmentData items in the department’s file
    ContractMember names, band name, gig details
    FinanceMember names, bank details, gig details
    PublicityBand name, gig details
    RecruitmentMember names, band name, agent details

    Now the same data is stored in several files. This is called data redundancy. It does not mean the data is no longer useful: it means that once data has been stored in one file, there should be no need to store it again in a different file. In a file-based system some data redundancy cannot be avoided.

    Data redundancy can lead to data inconsistency: two copies of the same data that do not match. This happens because of mistakes when the data is first entered, or because one copy is edited later and the other is not. This is a second cause of data lacking integrity. One of the main aims of the database approach is to remove data redundancy.

    Data redundancy and data inconsistency

    • Data redundancy: the same data is stored more than once, in the same file or in different files.
    • Data inconsistency: copies of the same data do not match, for example after only one copy is updated.

    8.1 · 3 of 21Book page Contents

    Data dependency

    The problems so far are about storing the data. There are also problems when programs use the files.

    Traditionally, a programmer wrote a program and, at the same time, defined the data files that the program needed. In the agency, each department would have its own programs to use its own files. The programmer must know exactly how the data is laid out in the files, for example that the fourth item on a line is the band name. This is called data dependency: the programs depend on the structure of the files.

    Files used by one department probably hold some of the same data as other departments’ files, but there is no plan for sharing files.

    Data dependency causes two more problems:

    • Changing what is stored is expensive. Suppose more and more bands perform with extra session musicians, and their data must be added to some files. The existing files must be rewritten, and then the programs must be rewritten so that they read the new files correctly. With a database, the existing programs could still run after the new data is added: the only new programming is for the programs that use the new data.
    • New applications are hard to build. File structures designed to suit particular programs may not suit a new use. If the agency wanted to analyse how successful its gigs have been over several years, getting that data out of the files would be complex and would take a long time.

    Limitations of a file-based approach

    • Data redundancy: the same data is stored in many files, which wastes storage space.
    • Data inconsistency: when one copy is changed and another is not, the copies no longer match, so data integrity is lost.
    • No built-in control when data is entered, so errors such as a missing value or a band that does not exist are not stopped.
    • Data privacy: access cannot be controlled to just part of a file.
    • Data dependency: programs are written for one file structure, so if the structure changes, the programs must be rewritten.
    • Data is hard to share between departments, and hard to extract for new applications.

    8.1 · 4 of 21Book pages Contents

    Relational databases

    A flat file keeps all the data in one single table. Here is a single table that stores which students are in which classes:

    StudentNameSubjectTeacherClassTime
    Virat KohliComputer ScienceZain MerchantCSC1018:00
    Virat KohliMathJohn von NeumannMTH10611:00
    Cristiano RonaldoComputer ScienceZain MerchantCSC10314:00
    Virat KohliPhysicsAlbert EinsteinPHY11015:00
    Shaheen AfridiComputer ScienceZain MerchantCSC1018:00

    A relational database avoids many of the flat-file problems by storing the data in several tables that are linked together by relationships. The single table above could be stored as a relational database with this structure:

    Four linked tables: Student (StudentID, LastName, FirstName, DateOfBirth), Enrolment (enrolmentID, ClassCode, StudentID), Class (ClassCode, Subject, Time, TeacherID) and Teacher (teacher_ID, Title, FirstName, Surname). Lines join StudentID in Student to StudentID in Enrolment, ClassCode in Class to ClassCode in Enrolment, and teacher_ID in Teacher to TeacherID in Class, each with a crow’s foot at the many end.
    The single table as four linked tables

    Each box is a database table, and the list in each box shows the categories of data (the fields) stored in that table. The lines between the tables are the relationships that link them.

    Look at the Student and Enrolment tables. The StudentID field is in both. Each student’s details appear in the Student table only once, but the student can be enrolled in many classes. The data could now look like this:

    StudentIDLastNameFirstNameDoB
    246524AfridiShaheen06/04/2008
    451421KohliVirat05/11/2007
    466712RonaldoCristiano05/02/2007
    EnrolmentIDClassCodeStudentID
    1CSC101451421
    2MTH106451421
    3CSC103466712
    4PHY110451421
    5CSC101246524

    In the Student table each student is recorded once. In the Enrolment table, only the StudentID is repeated. If a student’s details need to change, they are changed in one place, the Student table. So there is much less data redundancy, and less chance of errors that lead to inconsistent data.

    How a relational database addresses the limitations of a file-based approach

    • Data is stored in separate linked tables, and each item of data is stored only once, so there is less data redundancy.
    • A change is made in one place only, so there is less data inconsistency and better data integrity.
    • Rules such as validation and referential integrity are checked when data is entered.
    • Access can be controlled, so each user sees only the data they need (better data privacy).
    • Programs are independent of how the data is stored, so the structure can change, or data be added, without rewriting existing programs.
    • The same data can be shared by many users and programs, and queried in new ways.

    8.1 · 5 of 21Book pages Contents

    Database, table, record, field and entity

    A database is an organised collection of data. In theory, even a well organised filing cabinet is a database. But computer systems produce huge amounts of data, so electronic databases have become a necessity. You could not realistically keep an index of every website on the internet in a filing cabinet, and even if you could, imagine how long it would take to find all the websites about cats.

    Electronic databases must be well designed from the start, so that data can be stored and retrieved as efficiently as possible, and to avoid problems such as inconsistent and duplicated data.

    A table is a collection of related data, made up of records and fields. Imagine a school that stores data about students, staff, classes and grades. This is a table it might use to store data about each student:

    StudentIDLastNameFirstNameEmail
    246524AfridiShaheens.afridi@cswithzain.com
    451421KohliViratv.kohli@cswithzain.com
    466712RonaldoCristianoc.ronaldo@cswithzain.com
    • Fields give the category heading for each item of data in the table. Here the fields are StudentID, LastName, FirstName and Email.
    • A record is a collection of data about one object, person or thing. In the student table, a record is one row: the data about one student.

    Each table in a database should represent a single entity. An entity is an object in the real world that can be told apart from other objects. It can have:

    • a physical existence, for example a customer or a product
    • a conceptual existence, for example an order or a booking.

    In the example, the entity is a student. Everything the school wants to store about a student is an attribute of the entity, and each attribute becomes a field. Each student has their own row in the table: a record.

    In relational database language, a record is also called a tuple. So a tuple is one row of a table, such as (246524, Afridi, Shaheen, s.afridi@cswithzain.com).

    Exam definitions

    • Entity: a thing (object, person, event or idea) about which data is stored.
    • Table: a collection of related records about one entity, arranged in rows and columns.
    • Record: all the data about one instance of the entity, one row of a table.
    • Tuple: one row of a table, another name for a record.
    • Field: one column of a table, holding one item of data for every record.
    • Attribute: one item of data stored about an entity, such as an email address; it is stored as a field.

    Note Indexing is also part of this topic: an index is a separate, quick-to-search list of key values with pointers to the matching records, which makes searching a large table faster. It is explained with the other DBMS features in 8.2.

    8.1 · 6 of 21Book pages Contents

    Primary key

    A primary key is a field (or fields) in a table that acts as a unique identifier for each record. A primary key value can never be repeated within a table, so each record in the table is unique.

    Here is part of a Subjects table from a school’s database:

    SubjectIDSubjectNameLevel
    1Computer ScienceGCSE
    2Computer ScienceA Level
    3MathGCSE
    4MathA Level
    5PhysicsGCSE

    Which field is suitable as the primary key?

    • SubjectName is not unique: Computer Science and Math each appear more than once, so it cannot be the primary key.
    • Level is not unique either: GCSE and A Level are repeated, so it cannot be the primary key.
    • SubjectID has been included to make sure each record has something unique about it.

    Making SubjectID the primary key means no other record in the table can have a SubjectID that is already in use.

    Sometimes there is an obvious choice for the primary key. Every published book has an ISBN (International Standard Book Number) that is unique to that book, so a table of the books in a library can use the ISBN as its primary key.

    In other cases it is harder to find a suitable field. A common solution is to give each record a number as its primary key, like StudentID in the student table:

    StudentID (PK)LastNameFirstNameEmail
    246524AfridiShaheens.afridi@cswithzain.com
    451421KohliViratv.kohli@cswithzain.com
    466712RonaldoCristianoc.ronaldo@cswithzain.com

    Exam definition

    • A primary key is an attribute, or combination of attributes, that uniquely identifies each tuple (record) in a table.

    Practise this: past-paper questions

    Show 5 more

    8.1 · 7 of 21Book pages Contents

    Composite key

    A composite key is a primary key made up of more than one attribute.

    Suppose a school offers tutoring sessions after lessons. Each teacher must deliver one session a week for each subject they teach. The Tutoring table stores which teacher delivers each session, on which day, and in which room:

    SubjectIDTeacherIDDayRoom
    2130MondayH18
    3278MondayS14
    4278TuesdayS14
    5345MondayS22
    5785FridayS22

    Even in this small sample, no single attribute can be guaranteed to be unique:

    • There can be more than one session for the same subject: subject 5 has sessions on Monday and Friday.
    • The same teacher can deliver more than one session a week: teacher 278 delivers sessions for subjects 3 and 4.
    • There can be more than one session (for different subjects) on the same day.
    • More than one session can take place in the same room (on different days).

    But a teacher delivers only one session for a given subject each week. So the combination of SubjectID and TeacherID is unique for every record, and these two attributes together form a composite primary key.

    When you design your first database with several tables, you may want to avoid composite keys by adding a new attribute that is unique for each record, such as an auto-incremented TutoringID. This is not necessary. Even with a TutoringID, the table would still need the SubjectID and TeacherID columns to show which teacher delivers each session. Since that data is already in the table, and the two values together are unique, they are an effective and correct choice for the primary key.

    8.1 · 8 of 21Book page Contents

    Candidate key and secondary key

    A candidate key is an attribute, or the smallest set of attributes, in a table where no tuple has the same value. In other words, it is any attribute (or set of attributes) that could be chosen as the primary key.

    StudentID (primary key)LastNameFirstNameEmail (secondary key)
    246524AfridiShaheens.afridi@cswithzain.com
    451421KohliViratv.kohli@cswithzain.com
    466712RonaldoCristianoc.ronaldo@cswithzain.com
    456879KhanShaheens.khan@cswithzain.com
    325689RonaldoNazarion.ronaldo@cswithzain.com

    In this table, StudentID and Email are both candidate keys: no two students share either value. LastName (two students called Ronaldo) and FirstName (two called Shaheen) are not.

    StudentID has been chosen as the primary key. A secondary key is a candidate key that is an alternative to the primary key, so here Email is a secondary key.

    Exam definitions

    • Candidate key: an attribute, or smallest set of attributes, in a table where no tuple has the same value; it could be the primary key.
    • Secondary key: a candidate key that is not chosen as the primary key.

    Used later in

    8.1 · 9 of 21Book pages Contents

    Foreign key

    A foreign key is the primary key field of one table that appears in another table, to make a link between (join) the two tables. Foreign keys help keep the relationships between tables correct: for example, a foreign key value can only be put into a table if that value already exists as a primary key in the other table.

    The example below has three tables:

    • Student: StudentID, LastName, FirstName, Email
    • Subject: SubjectID, SubjectName, Level
    • Enrolment: EnrolmentID, SubjectID, StudentID, TargetGrade
    Three tables. Student has primary key StudentID, then LastName, FirstName, Email. Enrolment has primary key EnrolmentID, foreign keys SubjectID and StudentID, then TargetGrade. Subject has primary key SubjectID, then SubjectName and Level. Lines join StudentID and SubjectID to Enrolment, with a crow’s foot at the Enrolment end.
    PK marks a primary key and FK a foreign key

    Each table has its own primary key, and the Enrolment table has two foreign keys that link it to the Student and Subject tables. The data in the Enrolment table could look like this:

    EnrolmentID (PK)SubjectID (FK)StudentID (FK)TargetGrade
    97841246524A
    97553246524B
    95675246524A

    SubjectID and StudentID are foreign keys: the data in these fields links to the tables where those fields are the primary keys. A student can enrol on more than one subject, so including StudentID as a foreign key links each enrolment to the student’s record in the Student table. This avoids storing all the student’s details again for every enrolment: only the StudentID is repeated.

    To find out who student 246524 is, look in the Student table:

    StudentIDLastNameFirstNameEmail
    246524AfridiShaheens.afridi@cswithzain.com
    451421KohliViratv.kohli@cswithzain.com
    466712RonaldoCristianoc.ronaldo@cswithzain.com

    Student 246524 is Shaheen Afridi, and the Enrolment table shows she takes the subjects with IDs 1, 3 and 5. To find out what these are, look in the Subject table:

    SubjectIDSubjectNameLevel
    1Computer ScienceGCSE
    2Computer ScienceA Level
    3MathGCSE
    4MathA Level
    5PhysicsGCSE

    Opening every table to match up the foreign keys by hand would take a long time. Because the data is linked, SQL can query the database and pull out this information for you. For example, an SQL query can find all the subjects taken by Shaheen and give this output:

    SubjectNameLevelTargetGrade
    Computer ScienceGCSEA
    MathGCSEB
    PhysicsGCSEA

    Exam definition

    • A foreign key is an attribute in one table that refers to (links to) the primary key of another table, to create a relationship between the two tables.

    Practise this: past-paper questions

    Show 8 more

    8.1 · 10 of 21Book page Contents

    Referential integrity

    A relational database can improve data integrity further by enforcing referential integrity. At its simplest, when a record is added to a table and a value is put in a foreign key field, the system checks that the value exists in the primary key field of the linked table.

    For example, students are enrolled on subjects. If referential integrity is enforced, the system checks that a student with the given StudentID exists in the Student table. If it does not, that StudentID cannot be used in an enrolment record.

    Referential integrity can also say what happens when a record is updated or deleted:

    • If a primary key value is changed, the matching foreign key values can be updated automatically.
    • If a record is deleted, the records with matching foreign key values can be deleted too.
    • There are other options as well, such as stopping the change.

    Very often you will not want data to be deleted automatically, because it gives useful management information or is still needed for day-to-day queries. When a student leaves the college, it is more likely that you would store a ‘date left’ attribute than delete their data. The student can then be left out of registers and progress monitoring, but still be counted when enrolments or course completions are analysed.

    As a database designer, one of your jobs is to keep the integrity of the data as high as possible. You learn how to enforce referential integrity when you create tables with SQL.

    Why referential integrity is important

    • Every foreign key value must match an existing primary key value in the linked table.
    • A record cannot be added with a foreign key value that does not exist, for example an enrolment for a student who is not in the Student table.
    • A record cannot be deleted (or its primary key changed) while other records link to it, unless the change is passed on to those records, so no record is left linking to nothing.
    • This keeps the data in linked tables consistent and accurate.

    8.1 · 11 of 21Book pages Contents

    E-R diagrams and relationships

    In an entity-relationship (E-R) diagram, each entity is drawn as a rectangle. A relationship between two entities is drawn as a line, and it is one of three types:

    Three pairs of boxes. One-to-one: a plain line with no crow’s foot at either end. One-to-many: a crow’s foot at the right-hand end only. Many-to-many: a crow’s foot at both ends.
    One-to-one, one-to-many and many-to-many

    The line shows the cardinality of the relationship. Cardinality can be one or many: it is the number of times one instance of an entity can be linked to instances of the other entity. Read the cardinality at the end of the line nearest each entity. For a cardinality of one, the end of the line is straight. For a cardinality of many, the end splays out into a crow’s foot.

    By far the most common relationship is one-to-many. In the early stages of design you will also find the other types. A many-to-many relationship cannot be implemented directly in a relational database: it has to be resolved first (see below).

    Examples of each type:

    • Many-to-many: pupil and teacher. Each pupil is taught by many teachers, and each teacher has many pupils.
    • One-to-many: pupil and appointment. Each pupil books many appointments, and each appointment is for one pupil.
    • One-to-one: head teacher and school. Each head teacher runs one school, and each school is run by one head teacher.

    Once you have found all the entities and relationships, you can put them together into an E-R diagram:

    E-R diagram with five entities. Head teacher to School is a plain line (one-to-one). School to Teacher has a crow’s foot at Teacher (one-to-many). Teacher to Appointment and Student to Appointment each have a crow’s foot at Appointment (one-to-many). Teacher to Student has a crow’s foot at both ends (many-to-many).
    E-R diagram for a school

    Describing a relationship in an exam answer

    • Name the type, for example one-to-many, and say it both ways: one customer makes many orders; each order is made by one customer.
    • Name the keys that make the link: the primary key of the table on the “one” side is a foreign key in the table on the “many” side.

    Practise this: past-paper questions

    Show 16 more

    8.1 · 12 of 21Book pages Contents

    Worked example: an E-R diagram for a sports club

    There is a standard notation for describing an entity:

    EntityName(PrimaryKey, attribute1, attribute2, attribute3, …)

    It starts with the name of the entity, followed by its attributes in brackets. The primary key is underlined, and is usually written first. If the primary key is a composite key, every attribute in it is underlined. (On this page, the primary key is shown in bold. In the exam, underline it.) Entity names are written in the singular, for example Member, not Members.

    A sports club records the courses its members are assessed on. These are its entities in standard notation:

    • Member(MemberId, FirstName, LastName, Phone)
    • Course(CourseCode, Description, Fee)
    • Instructor(InstructorId, FirstName, LastName, Email)
    • Certificate(MemberId, CourseCode, AssessmentDate, InstructorId)

    Certificate has a composite primary key. The combination of MemberId and CourseCode makes each record unique, so no entry can be duplicated.

    The relationship between Member and Course is many-to-many: each member can be assessed for many courses, and each course is assessed for many members.

    Member and Course joined by a line with a crow’s foot at both ends.
    Member to Course: many-to-many

    The relationship between Member and Certificate is one-to-many: each member can gain many certificates, and each certificate is awarded to just one member.

    Member and Certificate joined by a line with a crow’s foot at the Certificate end.
    Member to Certificate: one-to-many

    The relationship between Course and Certificate is also one-to-many: each course can have many certificates (one for each member who completes it), and each certificate is for just one course.

    Course and Certificate joined by a line with a crow’s foot at the Certificate end.
    Course to Certificate: one-to-many

    The relationship between Instructor and Certificate is also one-to-many: each certificate has one instructor, and each instructor can carry out many assessments.

    Instructor and Certificate joined by a line with a crow’s foot at the Certificate end.
    Instructor to Certificate: one-to-many

    The completed E-R diagram looks like this:

    Completed E-R diagram. Member and Course are joined by a many-to-many line along the top. Member, Course and Instructor are each joined to Certificate by a line with a crow’s foot at the Certificate end.
    The sports club E-R diagram

    8.1 · 13 of 21Book page Contents

    Finding relationships from entity descriptions

    If an exam question gives you entity descriptions, you can find the relationships from the keys. Here are the sports club entities again:

    • Member(MemberId, FirstName, LastName, Phone)
    • Course(CourseCode, Description, Fee)
    • Instructor(InstructorId, FirstName, LastName, Email)
    • Certificate(MemberId, CourseCode, AssessmentDate, InstructorId)

    Look at each entity whose primary key is a single attribute. If that attribute is also in another entity, either as a non-key attribute or as part of a composite primary key, there is a one-to-many relationship from the first entity to the second.

    • If two entities have the same primary key, the relationship is one-to-one.
    • If two entities share an attribute that is the primary key (made of a single attribute) of one of them, the relationship is one-to-many.
    • If two entities share an attribute that is not a single-attribute primary key in either of them, there is a many-to-many relationship that needs resolving.

    Worked example

    Find the relationships between the sports club entities.

    1. Step 1
      Member has the single-attribute primary key MemberId. MemberId is also in Certificate, as part of its composite primary key, so Member to Certificate is one-to-many.
    2. Step 2
      Course has the single-attribute primary key CourseCode. CourseCode is also part of the composite key of Certificate, so Course to Certificate is one-to-many.
    3. Step 3
      Instructor has the single-attribute primary key InstructorId. InstructorId is a non-key attribute in Certificate, so Instructor to Certificate is one-to-many.

    Take care with composite primary keys. No part of a composite key is unique on its own: only the combination is a unique identifier. In Certificate, each part of the composite key is also a foreign key, so CourseCode is a foreign key (and part of the composite primary key), and the relationship between Course and Certificate is one-to-many.

    8.1 · 14 of 21Book page Contents

    Resolving a many-to-many relationship

    A many-to-many relationship cannot be implemented directly in a relational database. One record in a table cannot hold a list of many foreign key values, because that would break first normal form.

    Instead, it is resolved with a link table (also called a link entity): a new table between the two entities. The link table holds the primary key of each of the two tables as foreign keys, and usually these two foreign keys together form its composite primary key. The one many-to-many relationship becomes two one-to-many relationships, each with the crow’s foot at the link table.

    The sports club already does this. Member to Course is many-to-many, and Certificate is the link table: Member to Certificate and Course to Certificate are both one-to-many, and Certificate’s primary key is MemberId plus CourseCode. In the same way, the Enrolment table links Student and Subject.

    Purpose of a link table

    • To resolve a many-to-many relationship into two one-to-many relationships.
    • It holds the primary keys of both tables as foreign keys (often together as its composite primary key), so each record links one record from each table.

    8.1 · 15 of 21Book page Contents

    Normalisation and the normal forms

    Normalisation is a technique used when designing a database to reduce duplicated data. It also improves data integrity.

    In his important work on relational databases, Edgar F. Codd proposed a three-stage process, in which each stage reaches a normal form. His work was later extended, and you may see other stages in textbooks or online, but a database is usually considered normalised if it is in third normal form (3NF). For the exam, you only need to normalise to 3NF.

    Normal formRules
    1NFEach record has a primary key. Data is atomic. No repeating groups of attributes.
    2NFIn 1NF, and no partial dependencies.
    3NFIn 2NF, and no non-key (transitive) dependencies.

    Data that has not been normalised at all is sometimes called 0NF (zero normal form, or unnormalised). Each stage of normalisation removes one kind of problem:

    StageTask
    0NF to 1NFRemove any repeating groups of attributes, and make every attribute atomic.
    1NF to 2NFRemove any partial key dependencies.
    2NF to 3NFRemove any non-key dependencies.

    Describing the three normal forms

    • 1NF: there are no repeating groups of attributes, every attribute is atomic, and each table has a primary key.
    • 2NF: it is in 1NF, and every non-key attribute depends on the whole primary key, not just part of it (no partial dependencies).
    • 3NF: it is in 2NF, and every non-key attribute depends only on the primary key, not on another non-key attribute (no non-key dependencies).

    8.1 · 16 of 21Book pages Contents

    Worked example: the sports club as a flat file

    Back to the sports club. If a flat-file approach has been used to store the data about members and their certificates, the instructor might keep it in a spreadsheet or even a word-processed document:

    Member IdNamePhoneAssessment​ DateCourse CodeDescriptionFeeInstructor​Email
    012010Emily Marr01632 96174321/02/2019, 15/04/2019DG0011, CR0020Tennis, Badminton30, 20Sue, Sue Rsue.​james@​example.​org, susan.​robbins@​example.​net
    131092Joe Donald01632 96000715/04/2019CR0001Kayaking100Mohammedm.​franks@​example.​com
    132099Abdel Patel01632 96726701/03/2019, 15/04/2019, 02/06/2019DG3002, CR0001, SO0112Swimming, Kayaking, Judo L120, 100, 20Greta, Mohammed, Jayg.​geuze@​example.​com, m.​franks@​example.​com, jay12@​example.​net
    145543Precious Jones01632 96281621/02/2019, 02/06/2019, 01/07/2019DG0011, SO0112, CR0014Tennis, Judo L1, Climbing30, 20, 40Sue, Jay, Sue Rsue.​james@​example.​org, jay12@​example.​net, susan.​robbins@​example.​net
    148765Jack Marr01632 96174311/06/2019SP8701Judo L230Gretag.​geuze@​example.​com

    This gives one record for each member, and a member’s certificate data is spread over several values in one cell. The instructor could sort and filter on member details, but could not work with the certificate data, because there are several values in each cell.

    Another way is to make each row the award of one certificate. Then the member’s details must be repeated on every row:

    Member IdNamePhoneAssessment​ DateCourse CodeDescriptionFeeInstructor​Email
    145543Precious Jones01632 96281621/02/2019DG0011Tennis30.0Suesue.​james@​example.​org
    012010Emily Marr01632 96174321/02/2019DG0011Tennis30.0Suesue.​james@​example.​org
    132099Abdel Patel01632 96726701/03/2019DG3002Swimming20.0Gretag.​geuze@​example.​com
    131092Joe Donald01632 96000715/04/2019CR0001Kayaking100.0Mohammedm.​franks@​example.​com
    132099Abdel Patel01632 96726715/04/2019CR0001Kayaking100.0Mohammedm.​franks@​example.​com
    012010Emily Marr01632 96174315/04/2019CR0020Badminton20.0Sue Rsusan.​robbins@​example.​net
    132099Abdel Patel01632 96726702/06/2019SO0112Judo L120.0JayJay12@​example.​net
    145543Precious Jones01632 96281602/06/2019SO0112Judo L120.0JayJay12@​example.​net
    148765Jack Marr01632 96174311/06/2019SP8701Judo L230.0Gretag.​geuze@​example.​com
    145543Precious Jones01632 96281601/07/2019CR0014Climbing40.0Sue Rsusan.​robbins@​example.​net

    Either way works when there are only a few records. But it is not an efficient way to hold the data: it is hard to analyse, and data is duplicated for no reason. In a large system, duplicated data can become inconsistent, as described earlier. If you look carefully at the data, you may spot some inconsistencies already.

    To design a better system, the data must be stored in one or more linked tables, and every table must be in third normal form.

    Normalisation has a reputation for being difficult, but the basic idea is simple: there must be a table for each entity, and in each record every attribute must be fully dependent on the primary key.

    8.1 · 17 of 21Book page Contents

    What is a dependency?

    Before the rules of normalisation, you need to know what a dependency is. In database design, a dependency is a value that varies in line with another value.

    In the flat file, the course description varies in line with the course code. Whenever you see the code CR0001, you find the description Kayaking. So the description is dependent on the course code.

    But be careful not to assume a dependency. Always check any assumption with someone who understands the organisation and its current system. If the design is wrong, the database structure may have to be changed later, which can mean a lot of extra work.

    8.1 · 18 of 21Book pages Contents

    First normal form (1NF)

    To decide whether data is in first normal form (1NF), check that:

    • each record has a primary key
    • the data in each field (column) is atomic
    • no record has repeating groups of attributes.

    Look again at the original flat file, with one record for each member (the first table in the worked example). Check each rule in turn.

    Does each record have a primary key? Yes. MemberId is unique for each record, so it can be the primary key. If no attribute, or combination of attributes, could be guaranteed to be unique, you would need to add a new attribute to give each member a unique ID.

    Is the data atomic? Data is atomic when a column holds a single value that cannot sensibly be divided further.

    • The Name field is not atomic. It has two separate parts, first name and last name, so it is split into two fields: FirstName and LastName.
    • The course description is atomic. Some descriptions have two parts, such as Judo L1, but the parts do not make sense on their own.
    • AssessmentDate could be said to have three parts (day, month and year), but date fields are not usually split, because there are many date functions that can pull out the parts.
    • The instructor’s name is not atomic. There are two instructors called Sue, and one is told apart from the other by being written “Sue R”. So it is split into InstructorFirstName and InstructorLastName.

    Are there repeating groups of attributes? A repeating group of attributes is a set of attributes that appears more than once in a record. Sometimes a single attribute repeats, and this must be dealt with too.

    Here there is a repeating group about certificates: Abdel Patel, for example, has gained three certificates. The attributes that repeat are CourseCode, Description, AssessmentDate, Fee, Instructor (now split into InstructorFirstName and InstructorLastName) and Email (now renamed InstructorEmail).

    A repeating group must be moved to a new table, and removed from the original table. When you move it, you must still be able to link each moved record back to its original record, so the new table also gets a copy of the original primary key. Here, all the attributes about certificates go to a new Certificate table, and each certificate gets the MemberId of the member who gained it.

    This is the new Certificate table, with all the atomic attributes from the repeating group. Each certificate now has its own record, linked back to its member by MemberId. The instructors’ surnames have been filled in. Its primary key is the composite key MemberId and CourseCode:

    Certificate(MemberId, CourseCode, AssessmentDate, Description, Fee, InstructorLastName, InstructorFirstName, InstructorEmail)

    MemberId (PK)CourseCode (PK)Assessment​DateDescriptionFeeInstructor​LastNameInstructor​FirstNameInstructor​Email
    145543DG001121/02/2019Tennis30.00JamesSuesue.​james@​example.​org
    012010DG001121/02/2019Tennis30.00JamesSuesue.​james@​example.​org
    132099DG300201/03/2019Swimming20.00GeuzeGretag.​geuze@​example.​com
    131092CR000115/04/2019Kayaking100.00FranksMohammedm.​franks@​example.​com
    132099CR000115/04/2019Kayaking100.00FranksMohammedm.​franks@​example.​com
    012010CR002015/04/2019Badminton20.00RobbinsSuesusan.​robbins@​example.​net
    132099SO011202/06/2019Judo L120.00LintonJayJay12@​example.​net
    145543SO011202/06/2019Judo L120.00LintonJayJay12@​example.​net
    148765SP870111/06/2019Judo L230.00GeuzeGretag.​geuze@​example.​com
    145543CR001401/07/2019Climbing40.00RobbinsSuesusan.​robbins@​example.​net

    This is the Member table, with the name split into two attributes and the repeating group removed:

    Member(MemberId, FirstName, LastName, Phone)

    MemberId (PK)FirstNameLastNamePhone
    012010EmilyMarr01632 961743
    131092JoeDonald01632 960007
    132099AbdelPatel01632 967267
    145543PreciousJones01632 962816
    148765JackMarr01632 961743

    Both tables are now in first normal form.

    8.1 · 19 of 21Book pages Contents

    Second normal form (2NF)

    The rule for second normal form (2NF) is: the tables are in 1NF, and there are no partial dependencies.

    A partial dependency is a non-key attribute that depends on only part of a composite primary key. So partial dependencies can only happen in a table with a composite key. The Member table has a primary key made of a single attribute, so it is already in 2NF.

    The Certificate table has a composite primary key (MemberId and CourseCode), so it must be checked. For each non-key field, ask: does it depend on the whole key, or on just MemberId or just CourseCode? (Remember: a dependency is a value that varies in line with another value.) The two key fields are the first two columns, marked PK:

    MemberId (PK)CourseCode (PK)Assessment​DateDescriptionFeeInstructor​LastNameInstructor​FirstNameInstructor​Email
    145543DG001121/02/2019Tennis30.00JamesSuesue.​james@​example.​org
    012010DG001121/02/2019Tennis30.00JamesSuesue.​james@​example.​org
    132099DG300201/03/2019Swimming20.00GeuzeGretag.​geuze@​example.​com
    131092CR000115/04/2019Kayaking100.00FranksMohammedm.​franks@​example.​com
    132099CR000115/04/2019Kayaking100.00FranksMohammedm.​franks@​example.​com
    012010CR002015/04/2019Badminton20.00RobbinsSuesusan.​robbins@​example.​net
    132099SO011202/06/2019Judo L120.00LintonJayJay12@​example.​net
    145543SO011202/06/2019Judo L120.00LintonJayJay12@​example.​net
    148765SP870111/06/2019Judo L230.00GeuzeGretag.​geuze@​example.​com
    145543CR001401/07/2019Climbing40.00RobbinsSuesusan.​robbins@​example.​net
    • AssessmentDate depends on the whole composite key: it is the date that a particular member was assessed on a particular course.
    • Description depends only on CourseCode. Whenever you see the code CR0001, you find the description Kayaking. This is a partial dependency.
    • Fee also depends only on CourseCode. Whenever you see the code SP8701, you find the fee 30.00. This is a partial dependency.
    • InstructorLastName depends on the whole composite key: it is the surname of the instructor who assessed a particular member on a particular course. InstructorFirstName and InstructorEmail are the same.

    Note In this small sample, each course happens to have only one assessment date and one instructor. The design follows how the club works: each member is assessed separately, possibly on a different day by a different instructor. This is why a dependency must be checked with the organisation, not guessed from a few rows of data.

    The two fields with a partial dependency, Description and Fee, are moved to a new table, together with a copy of the part of the key they depend on, CourseCode. The new table has one record for each course, so it is called Course, and its primary key is CourseCode:

    Course(CourseCode, Description, Fee)

    CourseCode (PK)DescriptionFee
    DG0011Tennis30.00
    CR0001Kayaking100.00
    DG3002Swimming20.00
    SO0112Judo L120.00
    SP8701Judo L230.00
    CR0020Badminton20.00
    CR0014Climbing40.00

    Description and Fee can now be removed from the Certificate table. They can be found from the Course table, by linking through CourseCode:

    Certificate(MemberId, CourseCode, AssessmentDate, InstructorLastName, InstructorFirstName, InstructorEmail)

    MemberId (PK)CourseCode (PK)Assessment​DateInstructor​LastNameInstructor​FirstNameInstructor​Email
    145543DG001121/02/2019JamesSuesue.​james@​example.​org
    012010DG001121/02/2019JamesSuesue.​james@​example.​org
    132099DG300201/03/2019GeuzeGretag.​geuze@​example.​com
    131092CR000115/04/2019FranksMohammedm.​franks@​example.​com
    132099CR000115/04/2019FranksMohammedm.​franks@​example.​com
    012010CR002015/04/2019RobbinsSuesusan.​robbins@​example.​net
    132099SO011202/06/2019LintonJayJay12@​example.​net
    145543SO011202/06/2019LintonJayJay12@​example.​net
    148765SP870111/06/2019GeuzeGretag.​geuze@​example.​com
    145543CR001401/07/2019RobbinsSuesusan.​robbins@​example.​net

    Member, Course and Certificate are now all in second normal form.

    8.1 · 20 of 21Book pages Contents

    Third normal form (3NF)

    For third normal form (3NF), the tables must be in 2NF and have no non-key dependencies. A non-key dependency is a non-key attribute that depends on another non-key attribute, rather than on the primary key (it depends on the key only indirectly, through the other attribute, so it is also called a transitive dependency). All three tables must be checked.

    Member table. This is in 3NF. Every field depends fully on the primary key, MemberId: each record describes one club member, and the attributes are only about that member.

    MemberId (PK)FirstNameLastNamePhone
    012010EmilyMarr01632 961743
    131092JoeDonald01632 960007
    132099AbdelPatel01632 967267
    145543PreciousJones01632 962816
    148765JackMarr01632 961743

    Course table. This is in 3NF. Every field depends fully on the primary key, CourseCode.

    CourseCode (PK)DescriptionFee
    DG0011Tennis30.00
    CR0001Kayaking100.00
    DG3002Swimming20.00
    SO0112Judo L120.00
    SP8701Judo L230.00
    CR0020Badminton20.00
    CR0014Climbing40.00

    Certificate table. This one has a non-key dependency:

    MemberId (PK)CourseCode (PK)Assessment​DateInstructor​LastNameInstructor​FirstNameInstructor​Email
    145543DG001121/02/2019JamesSuesue.​james@​example.​org
    012010DG001121/02/2019JamesSuesue.​james@​example.​org
    132099DG300201/03/2019GeuzeGretag.​geuze@​example.​com
    131092CR000115/04/2019FranksMohammedm.​franks@​example.​com
    132099CR000115/04/2019FranksMohammedm.​franks@​example.​com
    012010CR002015/04/2019RobbinsSuesusan.​robbins@​example.​net
    132099SO011202/06/2019LintonJayJay12@​example.​net
    145543SO011202/06/2019LintonJayJay12@​example.​net
    148765SP870111/06/2019GeuzeGretag.​geuze@​example.​com
    145543CR001401/07/2019RobbinsSuesusan.​robbins@​example.​net

    The instructor’s first name and last name depend on the instructor’s email address, which is not a key. Whenever you see a particular email address, such as Jay12@example.net, you find the same first name and last name, Jay Linton. So a new Instructor table is needed.

    Email is not a good primary key for the new table. Each email address seems to identify one instructor, but two people could share an email address. When you choose a primary key, do not only look at the data you have; ask yourself: “Could two records ever have the same value in this field?” You should also choose a field whose value will not change, and people often change their email address. InstructorLastName cannot be the key, because two instructors may have the same last name, and InstructorFirstName cannot either, for the same reason.

    A practical solution is to add a new attribute, InstructorId. Each instructor gets a unique number, and this number is stored in each certificate record, so the instructor can be identified. In the new Instructor table the word Instructor has been removed from most attribute names, because every record describes an instructor, so there is no confusion:

    Instructor​Id (PK)LastNameFirstNameEmail
    1JamesSuesue.​james@​example.​org
    2GeuzeGretag.​geuze@​example.​com
    3FranksMohammedm.​franks@​example.​com
    4LintonJayJay12@​example.​net
    5RobbinsSuesusan.​robbins@​example.​net

    The instructor details can now be removed from the Certificate table and replaced by the single attribute InstructorId. InstructorId is a foreign key in the Certificate table, and it lets the instructor’s details be found when they are needed. This is the Certificate table in third normal form:

    MemberId (PK, FK)CourseCode (PK, FK)Assessment​DateInstructor​Id (FK)
    145543DG001121/02/20191
    012010DG001121/02/20191
    132099DG300201/03/20192
    131092CR000115/04/20193
    132099CR000115/04/20193
    012010CR002015/04/20195
    132099SO011202/06/20194
    145543SO011202/06/20194
    148765SP870111/06/20192
    145543CR001401/07/20195

    Explaining why tables are, or are not, in 3NF

    • In 3NF: give a reason for each rule. There are no repeating groups and all attributes are atomic; tables with a composite key have no attribute that depends on only part of it; and no non-key attribute depends on another non-key attribute.
    • Not in 3NF: point to the actual problem in the given tables, for example “Description depends only on CourseCode, which is only part of the primary key” (a partial dependency), or “InstructorFirstName depends on InstructorEmail, which is not the primary key” (a non-key dependency).
    • Always use attribute and table names from the question in your answer.

    8.1 · 21 of 21Book pages Contents

    The final design in 3NF

    The tables are now in third normal form, and all the unnecessary duplicated data has been removed. Here is the final design of each table, in standard notation, with an extract of its data. A foreign key is marked FK.

    Member(MemberId, FirstName, LastName, Phone)

    MemberId (PK)FirstNameLastNamePhone
    012010EmilyMarr01632 961743
    131092JoeDonald01632 960007
    132099AbdelPatel01632 967267
    145543PreciousJones01632 962816
    148765JackMarr01632 961743

    Course(CourseCode, Description, Fee)

    CourseCode (PK)DescriptionFee
    DG0011Tennis30.00
    CR0001Kayaking100.00
    DG3002Swimming20.00
    SO0112Judo L120.00
    SP8701Judo L230.00
    CR0020Badminton20.00
    CR0014Climbing40.00

    Instructor(InstructorId, FirstName, LastName, Email)

    Instructor​Id (PK)LastNameFirstNameEmail
    1JamesSuesue.​james@​example.​org
    2GeuzeGretag.​geuze@​example.​com
    3FranksMohammedm.​franks@​example.​com
    4LintonJayJay12@​example.​net
    5RobbinsSuesusan.​robbins@​example.​net

    Certificate(MemberId, CourseCode, AssessmentDate, InstructorId)

    MemberId (PK, FK)CourseCode (PK, FK)Assessment​DateInstructor​Id (FK)
    145543DG001121/02/20191
    012010DG001121/02/20191
    132099DG300201/03/20192
    131092CR000115/04/20193
    132099CR000115/04/20193
    012010CR002015/04/20195
    132099SO011202/06/20194
    145543SO011202/06/20194
    148765SP870111/06/20192
    145543CR001401/07/20195

    Compare this with the E-R diagram drawn earlier for the same club: there is one table for each entity, and each one-to-many relationship is made by a foreign key in the Certificate table.

    8.1

    Concepts in 8.1

    Every idea explained in 8.1, and where else in the notes it is used.

    ConceptIn one lineAlso used in
    atomic dataA value that cannot sensibly be divided into smaller parts.8.1 Normalisation and the normal forms, 8.1 Third normal form (3NF)
    attributeOne item of data stored about an entity, stored as a field.only here
    candidate keyAn attribute, or smallest set of attributes, with no repeated values: it could be the primary key.only here
    cardinalityHow many instances of one entity can be linked to an instance of another: one or many.only here
    composite keyA primary key made up of more than one attribute.8.1 Worked example: an E-R diagram for a sports club, 8.1 Finding relationships from entity descriptions, 8.1 Resolving a many-to-many relationship, 8.1 First normal form (1NF), 8.1 Second normal form (2NF), 8.1 Third normal form (3NF), 8.3 Primary and foreign keys, 8.3 The sports club sample data
    crow’s footThe splayed end of a relationship line in an E-R diagram, meaning “many”.8.1 Resolving a many-to-many relationship
    data dependencyPrograms depend on the file structure, so changing the structure means rewriting programs.only here
    data inconsistencyCopies of the same data that do not match, for example after only one is updated.8.1 Limitations of a file-based approach: one file, 8.1 Data dependency, 8.1 Relational databases
    data redundancyThe same data stored more than once, in one file or in different files.8.1 Data dependency, 8.1 Relational databases
    dependencyA value that varies in line with another value, so knowing one tells you the other.8.1 Worked example: the sports club as a flat file
    E-R diagramA diagram of entities (rectangles) and the relationships (lines) between them.8.1 Worked example: an E-R diagram for a sports club, 8.1 The final design in 3NF, 8.2 Data modelling and the logical schema
    entityA thing (object, person, event or idea) about which data is stored.8.1 E-R diagrams and relationships, 8.1 Worked example: an E-R diagram for a sports club, 8.1 Finding relationships from entity descriptions, 8.1 Resolving a many-to-many relationship, 8.1 Worked example: the sports club as a flat file, 8.1 The final design in 3NF, 8.2 Data modelling and the logical schema, 8.3 Naming tables and fields
    fieldOne column of a table, holding one item of data for every record.only here
    file-based approachStoring data in separate files, each set up for the programs that use it.8.1 Data redundancy and inconsistency in multiple files, 8.1 Data dependency, 8.1 Relational databases, 8.2 What a DBMS is
    first normal formEach table has a primary key, all attributes are atomic and there are no repeating groups.8.1 Resolving a many-to-many relationship, 8.1 Normalisation and the normal forms, 8.1 Second normal form (2NF)
    flat fileA database that keeps all its data in one single table.8.1 Worked example: the sports club as a flat file, 8.1 What is a dependency?, 8.1 First normal form (1NF)
    foreign keyAn attribute in one table that links to the primary key of another table.8.1 Relational databases, 8.1 Referential integrity, 8.1 E-R diagrams and relationships, 8.1 Finding relationships from entity descriptions, 8.1 Resolving a many-to-many relationship, 8.1 Third normal form (3NF), 8.1 The final design in 3NF, 8.2 The data dictionary, 8.2 Data integrity in a DBMS, 8.3 Primary and foreign keys, 8.3 Table constraints, 8.3 ALTER TABLE, 8.3 INSERT INTO, 8.3 Selecting data from two tables, 8.3 GROUP BY, 8.3 Reading and correcting SQL
    link tableA table that resolves a many-to-many relationship, holding both primary keys as foreign keys.only here
    many-to-many relationshipEach instance of either entity can link to many instances of the other.8.1 Worked example: an E-R diagram for a sports club, 8.1 Finding relationships from entity descriptions, 8.1 Resolving a many-to-many relationship
    non-key dependencyA non-key attribute that depends on another non-key attribute, not on the primary key.8.1 Normalisation and the normal forms
    normal formA stage of normalisation with its own rules: 1NF, 2NF or 3NF.only here
    normalisationDesigning tables in stages (1NF, 2NF, 3NF) to reduce duplicated data and improve integrity.8.1 Worked example: the sports club as a flat file, 8.1 What is a dependency?, 8.2 Data integrity in a DBMS
    one-to-many relationshipOne instance of an entity links to many instances of the other, each linking back to one.8.1 Worked example: an E-R diagram for a sports club, 8.1 Finding relationships from entity descriptions, 8.1 Resolving a many-to-many relationship, 8.1 The final design in 3NF
    one-to-one relationshipEach instance of one entity links to exactly one instance of the other.5.2 Assemblers, 8.1 Finding relationships from entity descriptions
    partial dependencyA non-key attribute that depends on only part of a composite primary key.8.1 Normalisation and the normal forms, 8.1 Third normal form (3NF)
    primary keyAn attribute, or set of attributes, that uniquely identifies each record in a table.8.1 Composite key, 8.1 Candidate key and secondary key, 8.1 Foreign key, 8.1 Referential integrity, 8.1 E-R diagrams and relationships, 8.1 Worked example: an E-R diagram for a sports club, 8.1 Finding relationships from entity descriptions, 8.1 Resolving a many-to-many relationship, 8.1 Normalisation and the normal forms, 8.1 Worked example: the sports club as a flat file, 8.1 First normal form (1NF), 8.1 Second normal form (2NF), 8.1 Third normal form (3NF), 8.2 The data dictionary, 8.2 Data integrity in a DBMS, 8.2 Indexes, 8.3 CREATE TABLE, 8.3 Primary and foreign keys, 8.3 Table constraints, 8.3 ALTER TABLE, 8.3 INSERT INTO, 8.3 UPDATE, 8.3 DELETE FROM, 8.3 The sports club sample data, 8.3 Selecting data from two tables, 8.3 INNER JOIN, 8.3 GROUP BY, 8.3 Reading and correcting SQL
    recordAll the data about one instance of an entity: one row of a table.only here
    referential integrityMaking sure every foreign key value matches an existing primary key value in the linked table.8.1 Relational databases, 8.2 Data integrity in a DBMS, 8.3 Table constraints
    relational databaseA database that stores data in several tables linked by relationships.8.1 Database, table, record, field and entity, 8.1 Referential integrity, 8.1 E-R diagrams and relationships, 8.1 Resolving a many-to-many relationship, 8.1 Normalisation and the normal forms, 8.3 SQL, DDL and DML
    repeating groupA set of attributes that appears more than once in one record.8.1 Normalisation and the normal forms, 8.1 Third normal form (3NF)
    second normal formIn 1NF and with no partial dependencies.8.1 Normalisation and the normal forms, 8.1 Third normal form (3NF)
    secondary keyA candidate key that is not chosen as the primary key.8.2 Indexes
    standard notationWriting an entity as Name(PrimaryKey, attribute1, attribute2, …) with the primary key underlined.8.1 The final design in 3NF
    tableA collection of related records about one entity, in rows and columns.8.1 Relational databases
    third normal formIn 2NF and with no non-key dependencies.8.1 Normalisation and the normal forms, 8.1 Worked example: the sports club as a flat file, 8.1 The final design in 3NF
    tupleOne row of a table in a relational database: a record.8.1 Primary key, 8.1 Candidate key and secondary key, 8.2 Indexes

    When you want more than reading

    The notes are the front door. The class is where it clicks.

    Join a class →Ask on WhatsApp