📑 Contents

Chapter 8.1: Database Concepts

9618 AS Computer Science

📚 Learning Objectives
📖 Prior Knowledge Required
🌟 What is a Database?

A database is a structured collection of related data stored in an efficient and compact manner that can be accessed by different application programs. The word "Efficient" means stored data can be accessed very easily and quickly. The word "Compact" means stored data takes up as little space as possible.

1. File-Based Approach

In a file-based approach, data is stored in one or more flat files. Each application or department has its own files, often with redundant or duplicated data. There is no central control or shared structure - files are often standalone.

📖 How File-Based Systems Work

1.1 Limitations of File-Based Approach

Limitation Explanation
Data Redundancy Same data is stored in multiple files, leading to duplication - wasteful as it costs time and money
Data Inconsistency If one file is updated but others aren't, data becomes unreliable and inconsistent
Data Dependency Data formats are defined in application programs; changing formats requires changing whole programs
Poor Data Integrity Harder to ensure data is accurate and consistent across all files
No Data Privacy Data privacy would be properly handled by a database system, not file-based
No Data Security Anyone can easily change or delete data stored in files
Difficult to Update Same information stored at different places - never sure changes are made everywhere
Limited Scalability Not suitable for large volumes of data or complex data relationships
HR Dept Employee File (Name, Address) Payroll Salary File (Name, Address) Sales Customer File (Name, Address) DUPLICATE DATA! × × ×
💡 Exam Tip

When asked about limitations of file-based approach, always mention data redundancy (duplication) and data inconsistency (different versions of same data). These are the most commonly tested points!

2. Relational Databases

A relational database is a collection of relational tables where data items are linked by internal pointers. It solves the problems of the file-based approach through organized structure and relationships between tables.

📖 Key Principles

2.1 Advantages of Relational Databases

Advantage How It Solves File-Based Problems
No Redundant Data Storage space not wasted as data items stored only once
Data Consistency Data altered in one application is available in another
Data Independence Enquiries not dependent on structure of data and software used
Complex Queries Easier to run complex queries across related tables
Reduced Dependency Data separate from software - changes to data don't require programs to be re-written

2.2 Flat File vs Relational Database

📖 Flat File Database
⚠️ Why Use Relational Over Flat File?

A large flat-file database is inefficient as it takes up more space and memory. It requires new data to be added every time you enter a new record. Data redundancy occurs when data is partially duplicated across records. Relational databases avoid these problems through proper normalization.

Example: A retail company might have separate tables for: customer details, customer orders, product details, stock levels, stock locations, and staff details. Each table holds specific information about one entity.

3. Database Terminology

When creating a database, think about what data you need to store. A database is essentially a collection of details about different items called entities.

Term Definition
Entity Anything that can have data stored about it - a person, place, event, or object (e.g., Student, Book, Film)
Table A group of similar data organized in rows and columns; entity is implemented as a table
Record (Tuple) A single row in a table representing one instance of an entity
Field (Attribute) A single column in a table storing one piece of data about the entity
Primary Key Unique identifier for each record in a table (e.g., StudentID)
Candidate Key A field (or combination of fields) that could be used as a primary key
Secondary Key A candidate key not selected as primary key; used for searching or sorting
Foreign Key A field that links to the primary key in another table to create relationships
Composite Key A primary key consisting of two or more attributes together
Index A technique to speed up searching by creating an ordered list of key fields
STUDENT PK StudentID FirstName LastName FK ClassID CLASS PK ClassID Location TeacherName 1:M Foreign Key links to Primary Key
📝 Primary Key Rules

4. Relationships & Referential Integrity

A relationship is formed when one table in a database has a foreign key that refers to a primary key in another table. The type of relationship determines how records relate to each other.

4.1 Types of Relationships

Relationship Type Description Example
One-to-One (1:1) Each record in one table is associated with exactly one record in another table Country ↔ Capital City (each country has one capital)
One-to-Many (1:M) One record can be associated with one or more records in another table Student ↔ Class (many students in one class)
Many-to-Many (M:M) Records in one table relate to many in another, and vice versa Students ↔ Courses (students take many courses)
Country 1:1 Capital Teacher 1:M Student Student M:M Course M:M needs Junction Table: StudentCourse(StudentID, CourseID)

4.2 Referential Integrity

📖 What is Referential Integrity?

Referential Integrity is a concept that ensures consistency and accuracy of relationships between tables. It uses foreign keys to ensure that a value can only be entered in one table when the same value already exists in the referenced table.

⚠️ Why Referential Integrity is Important
💡 Many-to-Many Relationships

A foreign key can only have a single value, so it cannot handle many references. Solution: Create a junction table (link entity) that links two tables by having fields which are primary keys of both tables.

5. Entity-Relationship Diagrams

An E-R diagram provides an understandable visual representation of how entities in a database are related. It documents the design of a database before implementation.

📖 E-R Diagram Components

5.1 Cardinality: Optional vs Mandatory

Type Description Notation
Optional Relationship may or may not exist (zero or one) 0 or 0..1
Mandatory Relationship must exist (one or many) 1 or 1..M
Example: In a workroom with desks, each employee has one desk, but there could be spare desks. The relationship between desk and employee is optional (zero or one). However, the relationship between mother and child is mandatory - every mother must have at least one child.
School Database E-R Diagram CUSTOMER PARTY PHOTO_DATA CAMERA_DATA 1 M 1 M 1 M Legend: 1 = One M = Many Crow's foot = Many side
💡 E-R Diagram Tips

6. Normalization

Normalization is a process used to construct a relational database that has integrity and in which data redundancy is reduced. Tables that are not normalized will be larger, harder to update, and more difficult to query.

📖 Why Normalize?

6.1 Normal Forms Overview

Normal Form Key Requirement What It Eliminates
First Normal Form (1NF) Atomic values, no repeating groups, unique rows Repeating groups and multi-valued attributes
Second Normal Form (2NF) All non-key attributes fully dependent on entire primary key Partial dependencies
Third Normal Form (3NF) No non-key attribute depends on another non-key attribute Transitive dependencies
UNF Unnormalized 1NF Atomic values 2NF No partial dep. 3NF No transitive dep. Ideal Normalization Process →
🧠 Memory Trick: The Key Rule

For 3NF, remember: "Each attribute must depend on the key, the whole key, and nothing but the key!"

7. First Normal Form (1NF)

For a table to be in First Normal Form, it must meet specific requirements that ensure each piece of data is properly organized.

📖 1NF Requirements

7.1 Types of Repeating Groups

📝 Repeating Similar Data

Same type of data repeated across multiple fields within a single record.

Example: Fields like Phone1, Phone2, Phone3 in a table where each stores a phone number - violates 1NF because same information stored in separate fields.

📝 Repeating Groups of Attributes

A group of attributes is repeated for different instances within a single record.

Example: In a Student table, if RollNo, FirstName, LastName are repeated for each subject entry - represents a repeating group of attributes.

✗ NOT in 1NF: name phone country John Smith 07373 929122 UK Iram Iravani 07234 543422 Iraq No primary key! Name not atomic! Fix ✓ In 1NF: customer_id forename surname 1 John Smith 2 Iram Iravani PK ✓ Atomic ✓
❌ Common Mistakes in 1NF
💡 1NF Quick Check

Ask yourself: "Can I split any cell into smaller pieces?" If yes, it's not in 1NF! Each cell should hold ONE value only.

8. Second Normal Form (2NF)

For a table to be in Second Normal Form, it must fulfill all 1NF requirements AND have no partial dependencies. This only applies to tables with a composite primary key.

📖 2NF Requirements

8.1 Understanding Partial Dependency

📝 What is Partial Dependency?

A partial dependency occurs when a non-key attribute depends on only part of a composite primary key, rather than the entire key.

Example: In a table with composite key (StudentName + Subject), if SubjectTeacher depends only on Subject (not StudentName), this is a partial dependency and violates 2NF.

✗ NOT in 2NF: StudentName Subject SubjectTeacher Level Tom Math SAN A Tom Physics MEB A Composite PK underlined Partial dependency! SubjectTeacher depends only on Subject ✓ Solution - Split tables: StudentSubject StudentID Subject 1 MEB Subject SubjectName Teacher
💡 2NF Quick Check

If a table has a single-column primary key, it's automatically in 2NF (no partial dependencies possible)! 2NF issues only occur with composite keys.

9. Third Normal Form (3NF)

For a table to be in Third Normal Form, it must fulfill all 2NF requirements AND have no transitive dependencies. This is the target normal form for most database designs.

📖 3NF Requirements

9.1 Understanding Transitive Dependency

📝 What is Transitive Dependency?

A transitive dependency occurs when: A → B → C (A determines B, B determines C). If a non-key attribute depends on another non-key attribute, it violates 3NF.

Example: Staff(StaffID, StaffName, City, Country). Here, Country depends on City (not StaffID directly). If we know City, we can determine Country - this is a transitive dependency!

✗ NOT in 3NF: Film Table FilmID Title Cert Desc 1 Saw IV 18 18+ Desc depends on Cert, not on FilmID! ✓ Solution: Film FilmID Title Certificate (FK) Certificate CertID Description 18 Eighteen+ 3NF Rule: Non-key attributes depend ONLY on the primary key!
❌ Common 3NF Violations
💡 3NF Quick Check

Ask: "Does any non-key attribute determine another non-key attribute?" If yes, split into separate tables!

10. Normalization Worked Example

Let's normalize a School Database held in a single unnormalized table.

⚠️ Problems with Unnormalized Table

10.1 Step 1: First Normal Form (1NF)

📝 Process

Remove repeating groups (subjects and subject teachers) to separate table with foreign key.

STUDENT(StudentID, FirstName, SecondName, DateOfBirth, ClassID, Location, TeacherName, LicenceNumber, Address, TeacherDateOfBirth)

STUDENTSUBJECT(StudentID, SubjectName, SubjectTeacher)

Note: StudentID in STUDENTSUBJECT is both part of composite PK and a FK linking to STUDENT table.

10.2 Step 2: Second Normal Form (2NF)

📝 Process

In STUDENTSUBJECT table, SubjectTeacher depends only on SubjectName (partial dependency). Remove by creating SUBJECT table.

STUDENT(StudentID, FirstName, SecondName, DateOfBirth, ClassID, Location, TeacherName, LicenceNumber, Address, TeacherDateOfBirth)

STUDENTSUBJECT(StudentID, SubjectName)

SUBJECT(SubjectName, SubjectTeacher)

10.3 Step 3: Third Normal Form (3NF)

📝 Process

Remove non-key dependencies from STUDENT table:

Final 3NF Design:

STUDENT(StudentID, FirstName, SecondName, DateOfBirth)

CLASS(ClassID, Location, LicenceNumber)

TEACHER(LicenceNumber, TeacherName, Address, TeacherDateOfBirth)

STUDENTSUBJECT(StudentID, SubjectName)

SUBJECT(SubjectName, LicenceNumber)

11. Exam-Style Questions (Part 1)

1. Describe three limitations of using a file-based approach for data storage. [6 marks]

Answer (any 3 limitations, 2 marks each):

  • Data Redundancy: Same data stored in multiple files causing duplication, wasteful use of storage space
  • Data Inconsistency: When one file is updated but others aren't, data becomes unreliable
  • Data Dependency: Data formats defined in programs; changing formats requires rewriting programs
  • Poor Security: No proper control over who can access or modify different files
  • Difficult to Update: Same information in different places - never sure all are updated
  • Limited Scalability: Not suitable for large volumes of data or complex relationships

Additional points for deeper understanding: No central control, hard to manage relationships, lack of data privacy, no data integrity enforcement.

2. Explain how a relational database addresses the limitations of the file-based approach. [5 marks]

Answer:

  • No Redundant Data: Data items stored only once, reducing storage space
  • Data Consistency: Data altered in one application available in all others
  • Data Independence: Enquiries not dependent on data structure or software
  • Complex Queries: Easier to run queries across related tables
  • Reduced Program-Data Dependency: Data separate from software; changes don't require program rewrites

Additional points: Centralized control, better security through access controls, referential integrity enforcement, standard query language (SQL).

3. Describe the difference between a primary key, a candidate key, and a foreign key. [6 marks]

Answer:

  • Primary Key: A unique identifier for each record in a table; each tuple must have a value and values must be unique
  • Candidate Key: A field (or combination of fields) that could potentially be used as a primary key; all have unique values
  • Foreign Key: An attribute in one table that refers to the primary key in another table; creates relationships between tables
  • One candidate key is selected as the primary key
  • Remaining candidate keys become secondary keys
  • Foreign keys ensure referential integrity between related tables
4. Explain why referential integrity is important in a database. [4 marks]

Answer:

  • Ensures data is consistent across related tables
  • Every foreign key has a corresponding primary key in the referenced table
  • Prevents records from being added, deleted, or modified incorrectly
  • Changes made in one place are reflected in all related records
  • Queries return accurate and complete results

Additional points: Prevents orphaned records, maintains relationship validity, ensures all data is up-to-date.

5. A database stores information about students and the courses they are enrolled in. Draw an entity-relationship diagram to show the relationship between Students and Courses. Explain why a junction table is needed. [5 marks]

Answer:

  • Relationship is Many-to-Many (M:M) - a student can enroll in many courses, and a course can have many students
  • A junction table (e.g., StudentCourse) is needed because:
  • Foreign keys can only have single values - cannot store multiple course IDs in one student record
  • Junction table contains composite primary key (StudentID + CourseID)
  • Both StudentID and CourseID are foreign keys linking to their respective tables

E-R Diagram: STUDENT ||---o{ STUDENTCOURSE }o---|| COURSE

11. Exam-Style Questions (Part 2)

6. Explain the requirements for a table to be in First Normal Form (1NF). Give an example of a table that violates 1NF and explain why. [6 marks]

Answer - 1NF Requirements:

  • All values must be atomic (indivisible, single values)
  • No repeating groups or repeating attributes
  • Each column has a unique name
  • Each row has a primary key for unique identification

Example violation: A table with columns: StudentName, Phone1, Phone2, Phone3 violates 1NF because it has repeating groups (Phone1, Phone2, Phone3 store the same type of data).

Additional points: Multi-valued fields like "Subjects: Math, Science, English" also violate 1NF. Each subject should be a separate row with the student ID.

7. A table has a composite primary key (StudentID, CourseID). Explain what is meant by partial dependency and why it violates Second Normal Form. [5 marks]

Answer:

  • Partial dependency occurs when a non-key attribute depends on only part of the composite primary key
  • For example, if CourseName depends only on CourseID (not StudentID), this is a partial dependency
  • 2NF requires all non-key attributes to be fully dependent on the entire primary key
  • Partial dependencies cause data redundancy and update anomalies
  • Solution: Create separate tables for partially dependent attributes

Additional points: Tables with single-column primary keys are automatically in 2NF - no partial dependencies possible.

8. A table contains: Staff(StaffID, StaffName, Department, DepartmentLocation). Explain why this table is not in Third Normal Form and show how to normalize it. [5 marks]

Answer:

  • The table is not in 3NF because of a transitive dependency
  • DepartmentLocation depends on Department (not directly on StaffID)
  • If we know the Department, we can determine DepartmentLocation
  • Solution - Create two tables:
  • STAFF(StaffID, StaffName, Department) - Department is FK
  • DEPARTMENT(Department, DepartmentLocation) - Department is PK

Additional points: This eliminates redundancy - DepartmentLocation stored once per department, not repeated for every staff member.

9. Describe the difference between one-to-one, one-to-many, and many-to-many relationships. Give an example of each. [6 marks]

Answer:

  • One-to-One (1:1): Each record in Table A relates to exactly one record in Table B
  • Example: Country ↔ Capital City (each country has one capital)
  • One-to-Many (1:M): One record in Table A can relate to many records in Table B
  • Example: Teacher ↔ Students (one teacher teaches many students)
  • Many-to-Many (M:M): Records in both tables can relate to multiple records in the other
  • Example: Students ↔ Courses (students take many courses, courses have many students)

Additional points: M:M relationships require a junction table. The "many" side always has the foreign key.

10. A relational database, TECHNOLOGY, stores data about staff and computer devices. The database has tables: STAFF(StaffID, Name, Department) and DEVICE(DeviceID, DeviceName, StaffID). Describe the relationship between the two tables. [4 marks]

Answer:

  • Primary key StaffID in STAFF links to foreign key StaffID in DEVICE
  • One-to-Many (1:M) relationship
  • One staff member can have many devices
  • Each device can only belong to one member of staff
  • StaffID in DEVICE table is the foreign key
  • Referential integrity ensures devices can only be assigned to existing staff

Additional points: If a staff member is deleted, corresponding device records would need handling (cascade delete or restrict).

12. Glossary

Term Definition
Attribute A property or characteristic of an entity; represented as a column in a table
Atomic Value A value that cannot be divided into smaller parts; single, indivisible data item
Candidate Key A field or combination of fields that could serve as a primary key
Composite Key A primary key made up of two or more attributes
Data Consistency Ensuring data is the same across all copies and throughout the database
Data Redundancy Unnecessary duplication of data in a database
Entity A real-world object or concept about which data is stored (e.g., Student, Product)
E-R Diagram Entity-Relationship diagram showing entities and their relationships graphically
Field A column in a database table; stores one attribute of an entity
Flat File Database A database stored in a single table, often as a CSV file
Foreign Key An attribute in one table that references the primary key in another table
Index A data structure that speeds up searching by creating ordered lists of key fields
Junction Table A table that implements a many-to-many relationship between two other tables
Normalization Process of organizing data to reduce redundancy and improve integrity
Partial Dependency When an attribute depends on only part of a composite primary key
Primary Key A unique identifier for each record in a table
Record (Tuple) A row in a database table representing one instance of an entity
Referential Integrity Ensuring foreign keys match valid primary keys in related tables
Relation A table in a relational database
Secondary Key A candidate key not selected as primary key; used for searching/sorting
Table A collection of related data organized in rows and columns
Transitive Dependency When a non-key attribute depends on another non-key attribute

13. Exam Success Tips

💡 Normalization - The Golden Rule

Remember: "The key, the whole key, and nothing but the key!"

💡 Identifying Normal Form Violations
💡 Relationship Types - Quick Identification
❌ Common Exam Mistakes to Avoid
🧠 Memory Tricks

14. Key Takeaways

📌 File-Based vs Relational Databases
📌 Keys and Relationships
📌 Normalization Summary
📌 E-R Diagrams
🌟 Final Exam Checklist