hanegreefs.com/Business Information SystemsBIS
Lecture 7 · unit 11 of 16

Classes and databases

From a spreadsheet list to a relational database: records and fields, sorting and filtering, UML class diagrams with associations and multiplicities, composition, aggregation and generalisation, first normal form, keys, mapping associations with foreign keys, and NULL.

Based on: lecture 7 (data bases: classes and databases) and the spreadsheet exercise session · About 70 minutes

Starting from a spreadsheetImportant

The lecture introduces databases with a spreadsheet because it is familiar, clear and easy to use. Every line is a record (an element), every record consists of fields (the columns), and the database holds zero or more records. In the exercise session you sort and filter such lists.

ToolWhat it doesTip
Sortorders records on one or more fields, ascending or descendingsort on several levels, e.g. city, then name
Filter (AutoFilter)shows only the records that meet a condition per columnconditions on different columns combine with AND
Advanced filteruses a separate criteria range with the field names on topcriteria on the same row mean AND; criteria on different rows mean OR; results can be copied elsewhere and duplicates removed

But a spreadsheet database is two-dimensional: fields vertically, records horizontally. For large databases that causes two problems. Many cells stay empty (a university sheet with a column for every course, filled in only for the courses a student takes). And information is repeated (every time postal code 2000 appears, the city Antwerp follows). The relational database solves both by splitting the data into tables connected by keys.

Characteristics of a relational databaseImportant

  • There is always a difference between structure and data, and you can switch between both views (for example to change the structure).
  • Relationships can be defined globally or locally: as usual in computing, what is global is known locally, but what is local is not known globally.
  • Tables are linked by keys.
  • A key is primary when each value may appear only once in its table: postal code 2000 appears once, for Antwerp.

Class diagramsImportant

After the processes (BPMN) and functionalities (use cases), the data are modelled. Data modelling is more technical and exact. A class diagram describes a system by its entities, which hold data, the relationships between them and their functionalities. Together with use case diagrams it forms the core of UML. It shows neither real-world things nor objects in memory, but a description of what those objects look like.

ElementMeaning
Classdescription of all objects of that kind; an object is an entity with actual data stored in the computer
Attributea property listed inside the class box; a class linked with multiplicity 1 on both sides and no other associations can become an attribute
Associationa relationship between instances of two classes, drawn as a line
Association classextra data stored for the association itself, e.g. the grade of a student for a course
Compositionthe strong form of "part of": every part sits inside one single whole (filled diamond)
Aggregationa weak "part of" relationship, used when composition does not apply (hollow diamond)
Generalisationan "is a" relationship (hollow triangle); it is not an association
MultiplicityMeaning
nexactly n objects
n..mat least n and at most m objects
*any number, zero included
n..*at least n objects
Worked scenario

University

A university stores its students and programs. A program has at least one compulsory course and may have optional courses. A student is enrolled in one program and in selected optional courses, and receives a grade for every course.

1

Classes

Student, Program, Course
2

Associations

Program to Course twice: compulsory (1..*) and optional (*); Student to Program (each student exactly 1); Student to Course for optional choices
3

Association class

Grade on the link between Student and Course
Answer

The grade belongs neither to the student nor to the course alone, so it is an association class.

From class to table: first normal formImportant

A database stores data in tables of rows and columns; each row is one instance. A class becomes a table with one column per attribute, provided the table is in first normal form (1NF):

  • every column has a type and every row has a single value of that type in each column (no lists),
  • there are no duplicated columns or rows,
  • the order of rows and columns has no meaning.

Column types depend on the DBMS (database management system): typically number, text, Boolean and date, but not complex structures such as a list, a matrix or a tree.

KeysImportant

KeyDefinition
Keyone or more columns used to identify rows
Composite keya key of two or more columns
Candidate keya key that determines all other columns of the table
Primary keythe candidate key chosen to identify rows: exactly one per table, may be composite; ideally short, numeric and never changing
Surrogate keyan artificial column added as primary key, supplied by the DBMS, short, numeric, never changing, meaningless to users and usually hidden
Foreign keythe primary key of one table placed in another table to link both; its values are "foreign" to the table where they appear

Mapping associationsImportant

AssociationMappingExample
multiplicity 1 or 0..1 on one sideadd a foreign key to the table of the other classProduct gets column categoryID referring to Category
many-to-many (both sides not 1)add an extra table with both foreign keystable Enrolment(studentID, courseID)
many-to-many with an association classthe extra table also gets the attributes of the association classEnrolment(studentID, courseID, grade)

Where a row has no value for a column, the value is NULL. A table can also refer to itself: in the staff example each employee has a supervisor column holding the staff number of another employee, and the top manager has NULL.

staffNumbernamesupervisor
11NoraNULL
12Bram11
13Lies11
14Omar13
15Tine13

Read it as: Nora supervises Bram and Lies, and Lies supervises Omar and Tine.

Worked scenario

Library with reservations

Members borrow copies of books; when no copy is available they reserve the book, and the order of reservations matters.

1

Tables

Member, Book, Copy (bookID as foreign key, since a copy belongs to exactly one book)
2

Loan

Copy gets a foreign key memberID (NULL when not lent), or a separate Loan table if history matters
3

Reservation

many-to-many between Member and Book: Reservation(memberID, bookID, reservationDate)
Answer

The reservation date (or a sequence number) keeps the order, so the first member in line can be notified when a copy comes back.

Querying such tables uses SQL. SQL is not part of the course itself; the SQL lab on this page is an optional extra to see foreign keys at work.

Key takeaways

  • Spreadsheet: records in rows, fields in columns; advanced filter: same row AND, different rows OR.
  • Two-dimensional lists give empty cells and repetition; a relational database splits data into linked tables.
  • Class diagram: classes, attributes, associations with multiplicities, association class, composition, aggregation, generalisation.
  • 1NF: one typed value per cell, no duplicate rows or columns, order has no meaning.
  • Multiplicity 1: foreign key; many-to-many: extra table with both foreign keys; missing value: NULL.

Loading cards
Question
Click, tap or press space to flip
Answer

Multiple choice (9)

Question 1 · Lecture 7

Advanced filter criteria: row 1 City = Brussels and Amount > 100; row 2 City = Ghent. Which records are shown?

Why: Same row is AND, different rows OR.
Question 2 · Lecture 7

Which is the ideal primary key?

Why: Ideal keys are short, numeric and stable.
Question 3 · Lecture 7

Students and courses: a student takes many courses and a course has many students. How is this mapped?

Why: Many-to-many needs a junction table; a list in a cell breaks 1NF.
Question 4 · Lecture 7

A cell holding "Python, Excel, SQL" as the skills of an employee violates:

Why: Each cell must hold a single value.
Question 5 · Lecture 7

A room belongs to exactly one building and disappears with it. Which relationship?

Why: Strong part-of with exactly one whole.
Question 6 · Lecture 7

"A savings account is an account." Which relationship?

Why: "Is a" means generalisation.
Question 7 · Lecture 7

In the staff table, Nora has supervisor NULL. This means:

Why: NULL means no value: Charles is at the top.
Question 8 · Lecture 7

What does multiplicity 0..1 on the Category side of Product mean?

Why: Read the multiplicity at the far end of the association.
Question 9 · Lecture 7

Which statement about a surrogate key is FALSE?

Why: Surrogate keys are meaningless to users.

Exam-style open questions (2)

Open question · Lecture 7

Online store tables

An online store keeps customers and products; a product belongs to a category; a category can contain subcategories; a customer places orders with an amount per product. Give the tables with primary and foreign keys.

Show a model answer outline
  • Customer(customerID, name, ...)
  • Category(categoryID, name, parentCategoryID FK to Category, NULL for top level).
  • Product(productID, name, price, categoryID FK).
  • Order(orderID, customerID FK, date).
  • OrderLine(orderID FK, productID FK, amount): composite key, many-to-many with association data.
Open question · Lecture 7

Spreadsheet versus relational

Explain with the postal code example why a relational database is better than one big spreadsheet list, and what a primary key does there.

Show a model answer outline
  • Two-dimensional list: empty cells and repetition (2000 always followed by Antwerp).
  • Split into tables: PostalCode(code, city) and Customer(..., code FK).
  • Primary key code: 2000 appears once, so the city is stored once.
  • Less repetition, fewer inconsistencies, easier updates.

LabAdvanced filter simulator

Criteria in the same row must all hold (AND). A record that matches any row is shown (OR). Numbers accept >, <, >=, <= and =.

LabFrom association to tables

Pick the multiplicity at each end of the line between the two classes. The verdict explains the mapping rule of the lecture.

LabWhich key is it?

Use the definitions of the lecture: identify, determine, combine, artificial, or refer to another table.

LabBonus SQL lab: query a small database

SQL is not exam material. The database is created fresh in your browser for every run and only SELECT queries are allowed.