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.
| Tool | What it does | Tip |
|---|---|---|
| Sort | orders records on one or more fields, ascending or descending | sort on several levels, e.g. city, then name |
| Filter (AutoFilter) | shows only the records that meet a condition per column | conditions on different columns combine with AND |
| Advanced filter | uses a separate criteria range with the field names on top | criteria 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.
| Element | Meaning |
|---|---|
| Class | description of all objects of that kind; an object is an entity with actual data stored in the computer |
| Attribute | a property listed inside the class box; a class linked with multiplicity 1 on both sides and no other associations can become an attribute |
| Association | a relationship between instances of two classes, drawn as a line |
| Association class | extra data stored for the association itself, e.g. the grade of a student for a course |
| Composition | the strong form of "part of": every part sits inside one single whole (filled diamond) |
| Aggregation | a weak "part of" relationship, used when composition does not apply (hollow diamond) |
| Generalisation | an "is a" relationship (hollow triangle); it is not an association |
| Multiplicity | Meaning |
|---|---|
n | exactly n objects |
n..m | at least n and at most m objects |
* | any number, zero included |
n..* | at least n objects |
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.
Classes
Associations
Association class
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
| Key | Definition |
|---|---|
| Key | one or more columns used to identify rows |
| Composite key | a key of two or more columns |
| Candidate key | a key that determines all other columns of the table |
| Primary key | the candidate key chosen to identify rows: exactly one per table, may be composite; ideally short, numeric and never changing |
| Surrogate key | an artificial column added as primary key, supplied by the DBMS, short, numeric, never changing, meaningless to users and usually hidden |
| Foreign key | the primary key of one table placed in another table to link both; its values are "foreign" to the table where they appear |
Mapping associationsImportant
| Association | Mapping | Example |
|---|---|---|
| multiplicity 1 or 0..1 on one side | add a foreign key to the table of the other class | Product gets column categoryID referring to Category |
| many-to-many (both sides not 1) | add an extra table with both foreign keys | table Enrolment(studentID, courseID) |
| many-to-many with an association class | the extra table also gets the attributes of the association class | Enrolment(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.
| staffNumber | name | supervisor |
|---|---|---|
| 11 | Nora | NULL |
| 12 | Bram | 11 |
| 13 | Lies | 11 |
| 14 | Omar | 13 |
| 15 | Tine | 13 |
Read it as: Nora supervises Bram and Lies, and Lies supervises Omar and Tine.
Library with reservations
Members borrow copies of books; when no copy is available they reserve the book, and the order of reservations matters.
Tables
Loan
Reservation
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.
All cards known
Reset the progress or pick another unit to keep practising.
Multiple choice (9)
Advanced filter criteria: row 1 City = Brussels and Amount > 100; row 2 City = Ghent. Which records are shown?
Which is the ideal primary key?
Students and courses: a student takes many courses and a course has many students. How is this mapped?
A cell holding "Python, Excel, SQL" as the skills of an employee violates:
A room belongs to exactly one building and disappears with it. Which relationship?
"A savings account is an account." Which relationship?
In the staff table, Nora has supervisor NULL. This means:
What does multiplicity 0..1 on the Category side of Product mean?
Which statement about a surrogate key is FALSE?
Exam-style open questions (2)
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.
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.