Design · lesson 5 of 10
Ideas in this lesson
AEntity, attribute, relationshipthe things, their details, how they connect
BTable, keyhow a database stores them
CData minimisationcollect only what's needed

Designing the data

Last updated

Code can be rewritten in a week; data lasts for years. What Pila stores, how its pieces connect and what it deliberately doesn't keep are some of the hardest decisions to change later. In this lesson you'll build Pila's data model entity by entity, find a field the user stories need, and learn why the best data to protect is the data you never collected.

By the end of this lesson you can

  • Describe a data model with entities, attributes and relationships
  • Turn user stories into the fields a model needs
  • Explain why storing less data is part of good design

Things, details and links

A data model describes what a system remembers. It has three kinds of piece:

  • Entities: the kinds of thing it keeps. For Pila: queue days, tickets, calls and staff.
  • Attributes: the details of each. A ticket has a number, a phone, a priority, a status, a time taken.
  • Relationships: how they connect, and how many of each.

Peter Chen's 1976 paper introduced the entity-relationship diagram for drawing them. Today the same model is often drawn as a UML class diagram: each entity a class, with its attributes listed under its name, and each relationship an association, a line with its multiplicity at each end, how many of that class take part.

10..60has ▸10..*called in ▸10..*makes ▴QueueDaydate: Dateopens: Timecloses: Timelimit: IntegerTicketnumber: Integerphone: String [0..1]priority: Booleanstatus: Statustaken_at: DateTimeCallcalled_at: DateTimeStaffname: Stringpassword_hash: String
  1. A QueueDay (date, opening and closing times, limit) has 0 to 60 Tickets; each ticket belongs to exactly 1 day.
  2. A Ticket (number, phone, which may be empty, priority, status, time taken) is called in 0 or more Calls (zero if the patient never came, more than once if they were skipped and called again); each call is of exactly 1 ticket.
  3. A Staff member (name, password hash) makes 0 or more calls; each call is made by exactly 1 staff member.
Pila’s data model. A UML class diagram: each box is a class, with its name on top and its attributes below; each line is an association, and the numbers at its ends are multiplicities, how many of that class take part (1 exactly one, 0..* any number, 0..60 up to 60). A ▸ shows which way to read an association’s name.

There are no ids in the drawing yet: keys are a database detail, added in the next step. The "how many" at each end of a line matters. "A ticket is called zero or more times" is a decision: it lets staff skip a patient who isn't there and call them again later, which the counter wireframe in lesson 3.7 needed.

Tables and keys

In a relational database each entity usually becomes a table, each attribute a column, and each thing a row. Every row needs a key that identifies it; relationships are kept by storing one row's key in another row.

Pila’s tables
TableColumnsLinks to
queue_daydate (the key), opens, closes, limit
ticketid (the key), day, number, phone (may be empty), priority, status, taken_atqueue_day, by its date
callid (the key), ticket, called_at, staffticket, by its id; staff, by its id
staffid (the key), name, password hash

The ticket's id and its queue number aren't the same: numbers start again at 1 each day, but ids never repeat. Using the number as the key would have made yesterday's 38 and today's 38 the same row.

What the stories need

Every attribute should be there because a story or requirement needs it, and every story's data should have somewhere to live. Go through Pila's stories from lesson 3.P and check each candidate field.

0 of 7 placed right

  1. Ticket: priority (senior, pregnant, or none)

  2. Ticket: status (waiting, called, done, cancelled)

  3. Ticket: patient’s birthday

  4. Ticket: reason for the visit

  5. Ticket: phone

  6. Queue day: numbers given so far

  7. Call: called at (time)

What not to store

The Data Privacy Act of 2012 says personal information should be collected for a declared purpose, be no more than that purpose needs, and be kept only as long as needed (Section 11, in plain words; this isn't legal advice). Design is where that becomes real: a field that doesn't exist can't leak.

For Pila, that means:

  • Phone numbers, but not names. Replies need the phone; nothing needs the name. (Staff sign-in needs staff names, which is different data, about staff.)
  • No health information at all. The reason for a visit stays between the patient and the midwife.
  • Deleted after 30 days. The monthly report needs counts and times, not phone numbers, so tickets lose their phone number when the month's report is done.

Lesson 9.2 returns to this as privacy by design. And remember lesson 4.1: data is hard to change later. Adding a field next year is a migration; removing one that leaked is far worse.

Spot the trap: “in case it's useful”

Spot the trapKap. Ernesto

Kap. Ernesto suggests Pila ask every patient for their full name, address, birthday and medical history when they take a number. “The barangay could use it for planning. It’s free to collect, since we’re building it anyway.”

What happens if…

Check what you learned

Why does Pila’s ticket have an id as well as a queue number?
What is the safest way to protect a piece of personal data?
“A ticket is called zero or more times.” What does this relationship allow?

Sources

Every claim on this page comes from these sources: the standard, the founding text of a method, a study, or the official record of a project, from the body that publishes it. Industry reports are labelled as such.

All sources for Simple Software Development