← IB Computer Science

A3.2 · Book pp. 174–185

Database design

Your level

A good database is designed before it is built. Most of the skill lies in deciding what goes in which table.

What you need to know

Points A3.2.1 to A3.2.7 are for both SL and HL. Normalization is the big skill here, so practise it on paper.

Next: schemas →

A3.2.1 Database schema

A schema shows how data is organized and how the relationships are managed. A database management system (DBMS) creates, maintains and controls access to the database.

SchemaShowsExample
ConceptualEntities, attributes and relationships, with no technical detailCustomers place orders
LogicalTables, fields, data types, keys and constraints, for no specific DBMSOrderDate: DATE
PhysicalHow one DBMS stores it, with indexes and partitionsINDEX on LastName

Schemas improve organization, security (with permissions and views), integrity (with constraints) and scalability.

✏️ Book correction

The book’s sales schema puts one ProductID in Orders, yet says an order includes one or more products. That cannot work, since a foreign key holds one value. Orders and products are many-to-many, so add a linking table: OrderItem(OrderID*, ProductID*, Quantity).

Next: ERDs →

A3.2.2 Entity-relationship diagrams

An ERD shows entities and their relationships. It helps people discuss, document and maintain the design.

  • Cardinality is the maximum: one or many.
  • Modality is the minimum: zero (optional) or one (mandatory).
Customer Order OrderItem Product places contains appears in
Original crow’s foot ERD. Two bars mean exactly one. A circle and crow’s foot mean zero or many. A bar and crow’s foot mean one or many.
Symbol at the line’s endMeaning
Two barsOne and only one
Circle and barZero or one
Bar and crow’s footOne or many
Circle and crow’s footZero or many

✏️ Book correction

The book’s club example says a student can join many clubs and a club has many students, but calls this one-to-many. It is many-to-many, so it needs a linking table such as Membership(StudentID*, ClubID*).

✍️ Practice

Draw an ERD for a clinic: patients, doctors and appointments. Each appointment has one patient and one doctor. A new patient may have no appointments yet.

Model answer

Patient (exactly one) to Appointment (zero or many). Doctor (exactly one) to Appointment (zero or many). Appointment is the linking entity between patients and doctors.

Next: data types and tables →

A3.2.3 Data types

TypeHoldsExample use
CHARACTER(n)Fixed-length textA code that is always 8 characters
VARCHAR(n)Variable-length text, up to n charactersName, email
INTEGERWhole numberQuantity
REALNumber with a decimal partPrice
DATEYYYY-MM-DDDate of birth
TIMEHH:MM:SSDeparture time
BOOLEANTrue or FalseHas paid

Types control which operations work, and how efficiently. If you store a price as text, the application must convert it before any calculation. Data of the wrong type is rejected on insertion, and using different types for the same attribute across systems causes errors.

🔬 Lab: pick the data type

A3.2.4 Constructing tables

Each table needs a primary key, sensible field names and suitable types. Where no single field is unique, you have three options:

  • Use a composite key of two or more fields, which is usually best.
  • Add a new ID field, called a surrogate key.
  • Use a concatenated key, which joins values into one field. This breaks atomicity, so avoid it.

⚠️ Common mistake

A name like “Boris Brown” should usually be split into FirstName and LastName, so that each field is atomic.

Next: normalization →

A3.2.5 Normal forms

Normalization organizes data to reduce redundancy and improve integrity, so that each fact lives in one place.

A functional dependency means one attribute determines another: StudentID determines LastName.

FormRuleRemoves
1NFHas a primary key, with atomic values and no repeating groupsLists in cells
2NFIn 1NF, and every non-key field depends on the whole keyPartial dependencies
3NFIn 2NF, and no non-key field depends on another non-key fieldTransitive dependencies

Every field depends on the key, the whole key, and nothing but the key.

A3.2.6 Normalizing to 3NF

🔬 Lab: normalize step by step

One cell holds a list, and there are no clear keys.

SIGNUPS
StudentWorkshops
1, Mali RakdeeW1 Robotics (Lab 2), W2 Pottery (Art 1)
2, Niran ChaiW1 Robotics (Lab 2)

* marks the primary key. Student names here are made up.

✏️ Book corrections

  • The book’s library example removes author names at the 2NF step. With a single-field key like BookID, no partial dependency is possible, so the table is already in 2NF. Author names depending on AuthorID is a transitive dependency, which belongs in the 3NF step.
  • It describes partial dependency using a single StudentID. Partial dependency only exists with a composite key.
  • Its CLUB table repeats ClubID 105 even though ClubID is the primary key, which cannot happen.
Check yourself

Sale(SaleID*, ProductID, ProductName, Price, CashierID, CashierName). Which fields move out for 3NF?

ProductName and Price move to Product(ProductID*, ProductName, Price). CashierName moves to Cashier(CashierID*, CashierName). Sale keeps SaleID, ProductID and CashierID.

Next: denormalization →

A3.2.7 Denormalizing

Denormalization deliberately allows redundancy to speed up queries.

NormalizedDenormalized
RedundancyMinimalDeliberately allowed
IntegrityEasier to protect, with each fact in one placeCopies can drift out of step
UpdatesChange one rowChange every copy
ReadsOften need several joinsFewer joins, so faster
StorageLess repeated data, but more tablesMore, due to the copies
SchemaMany tables can be complexSimpler to query, harder to maintain

When it makes sense

  • Read-intensive applications, where data is read far more than it is changed.
  • Reporting and analytics, where complex reports join many tables.
  • Data warehousing, where historical data is analysed but rarely changed.

To balance the two, analyse whether the system mainly reads or updates. Check that the speed gain outweighs the risk, partly denormalize if needed, and then validate data and monitor performance.

✏️ Book correction

The book lists “updating records may require changing data in different places” as a drawback of normalization. That problem belongs to denormalized data, which is why it appears in the table above on that side.

Next: review →

Quick quiz

1. Which schema defines keys and data types without naming a DBMS?
2. Where would you find an index defined?
3. Modality describes…
4. Cardinality describes…
5. A value in 1NF must be…
6. Partial dependencies can only appear with…
7. In (OrderID*, CustomerID, CustomerCity), what breaks 3NF?
8. Which suits denormalizing?

Exam-style questions

These are written for this site in the style of Paper 1. Answer on paper first, then open the mark points.

  1. Define the term “schema”. [1]

    Mark points
    • An architecture showing how data is organized and how the relationships between data are managed [1].
  2. Distinguish between a conceptual schema and a logical schema. [2]

    Mark points
    • A conceptual schema shows entities and relationships at a high level, with no technical detail [1].
    • A logical schema adds tables, data types, keys and constraints [1].
  3. Identify two issues caused by data redundancy. [2]

    Mark points
    • Wasted storage space [1].
    • Inconsistency, when one copy is updated and another is not [1].
  4. State the requirements of 1NF, 2NF and 3NF. [3]

    Mark points
    • 1NF: atomic values, no repeating groups, a primary key [1].
    • 2NF: in 1NF, with no partial dependencies [1].
    • 3NF: in 2NF, with no transitive dependencies [1].
  5. A table has Loan(LoanID*, BookID, BookTitle, MemberID, MemberName). Normalize it to 3NF. [4]

    Mark points
    • BookTitle depends on BookID, and MemberName on MemberID, which are transitive dependencies [1].
    • Book(BookID*, BookTitle) [1].
    • Member(MemberID*, MemberName) [1].
    • Loan(LoanID*, BookID, MemberID), with foreign keys [1].
  6. Evaluate denormalizing a data warehouse. [3]

    Mark points
    • Fewer joins make complex analytical queries faster [1].
    • Historical data rarely changes, so updates are few [1].
    • The cost is extra storage and careful loading to keep copies consistent, which is usually acceptable here [1].