Part 13 · Technology for designers

Chapter 78: Data and databases

Part 13 · Technology for designers4 min read

A database is an organized store of data that software reads and writes. In the food-delivery app from chapter 77, the API is the door the phone knocks on; the database is the room behind it, holding every restaurant, menu and order. Its arrangement decides what a screen can show, which combinations a user can make and what happens when something is deleted.

Data models

A data model describes the things a product stores and how they relate, before anyone picks a database. Its parts are entities (kinds of thing: customer, restaurant, menu item, order), attributes (properties of each: a restaurant’s name, an order’s total) and relationships (a customer places an order). Peter Chen set out this vocabulary in 1976, along with the entity-relationship diagram for drawing it.1

Cardinality says how many of one entity can relate to another. A restaurant has many menu items, but each menu item belongs to one restaurant: one-to-many. An order holds many menu items, and a menu item appears in many orders: many-to-many, which is usually resolved with a third entity, the order item, holding the quantity.

Customer id name email Order id customer_id restaurant_id status placed_at Restaurant id name cuisine rating Order item order_id menu_item_id quantity price_paid Menu item id restaurant_id name price places receives appears in offers contains one restaurant per order: a cart can’t mix restaurants copied at checkout, so later price changes don’t alter old receipts
Figure 78.1 The data model behind the food-delivery app. Each line is a rule the interface has to respect.

Every line in the model becomes a rule in the interface: if an order belongs to exactly one restaurant, a cart can’t hold dishes from two, and the design needs an answer for the user who tries: a prompt to start a new cart, say. The order item stores the price paid, copied at checkout, so a later price rise doesn’t rewrite last month’s receipt. Sketching the entities behind a screen with an engineer, early, finds these rules before the mockups do. It’s information architecture done for the machine (→ 59 Information architecture).

Relational and non-relational databases

A relational database stores data in tables. E. F. Codd, a mathematician at IBM’s San Jose lab, proposed the model in 1970, arguing that users shouldn’t need to know how data is stored inside the machine.2 Each table holds one entity; each row is a record and each column an attribute. A primary key uniquely identifies a row and can’t be empty. A foreign key is a column whose values must match a primary key in another table, which is how an order points to its customer.3 Queries combine tables by matching these values, an operation called a join. Most relational systems are queried in SQL, which Donald Chamberlin and Raymond Boyce developed alongside IBM’s System R prototype from 1973.4

NoSQL is an umbrella term for databases that aren’t relational. The main kinds are document, key-value, wide-column and graph databases.5 A document database such as MongoDB stores each record as a JSON-like document that can nest related data, so a restaurant document might carry its whole menu. MongoDB’s guidance is that data read together should be stored together, and documents in one collection need not share fields.6 The cost is duplication: a restaurant name copied into thousands of order documents must be updated everywhere, or old orders show the old name.

Large systems often keep copies of the data on several machines. Many offer only eventual consistency: once updates stop, every copy will in time return the latest value.7 On screen, a review someone just posted may be missing when they reload. If the data is eventually consistent, show the user’s own change straight away instead of waiting for the server to agree.

Schemas and CRUD

A database schema is the definition of a database’s structure: its tables, columns, data types and constraints, the rules the database enforces. NOT NULL makes a value required, UNIQUE forbids duplicates and CHECK tests a condition such as a price above zero.3 Each one is a form rule waiting to happen: a unique email column means the sign-up form needs an “already registered” message, best shown at the field, not after a failed submit (→ 61 Interaction patterns, forms and states). Schemas change through migrations, versioned scripts that alter the structure in order.8 A new required field is a migration plus a decision about every existing record that lacks it.

CRUD stands for create, read, update and delete, the four basic operations on stored data.9 Through an API they usually arrive as POST, GET, PUT or PATCH, and DELETE (→ 77 APIs). For each entity a screen shows, ask which of the four the user may do, and design each, including empty and error states. Delete needs the most thought. A database can refuse to delete a row that others point to, or delete the dependent rows with it.3 Many products use a soft delete instead, marking the record as deleted and hiding it.9 That makes an Undo button possible, and it means “deleted” data may still exist, which is a privacy question (→ 95 Privacy and data ethics).

See also

59 Information architecture · 61 Interaction patterns, forms and states · 76 Frontend, backend and architecture · 77 APIs · 95 Privacy and data ethics

Sources

  • Chen, P. P.-S. (1976). “The Entity-Relationship Model: Toward a Unified View of Data.” ACM Transactions on Database Systems 1(1), 9–36.
  • Codd, E. F. (1970). “A Relational Model of Data for Large Shared Data Banks.” Communications of the ACM 13(6), 377–387.
  • IBM (n.d.). “The relational database.” IBM History.
  • MDN Web Docs (n.d.). “CRUD.” Mozilla.
  • MongoDB (n.d.). “What Is NoSQL?” and “Data Modeling.” MongoDB documentation.
  • PostgreSQL Global Development Group (n.d.). “Constraints.” PostgreSQL Documentation.
  • Rails Guides (n.d.). “Active Record Migrations.”
  • Vogels, W. (2008). “Eventually Consistent - Revisited.” All Things Distributed; also published in ACM Queue.

Notes

  1. Chen, P. P.-S. (1976). “The Entity-Relationship Model: Toward a Unified View of Data.” ACM Transactions on Database Systems 1(1), 9–36. https://www.comp.nus.edu.sg/~lingtw/papers/tods76.chen.pdf ↩
  2. Codd, E. F. (1970). “A Relational Model of Data for Large Shared Data Banks.” Communications of the ACM 13(6), 377–387. https://www.engineering.upenn.edu/~zives/03f/cis550/codd.pdf ↩
  3. PostgreSQL Global Development Group (n.d.). “5.5. Constraints.” PostgreSQL Documentation. https://www.postgresql.org/docs/current/ddl-constraints.html ↩ ↩ ↩
  4. IBM (n.d.). “The relational database.” IBM History. https://www.ibm.com/history/relational-database ↩
  5. MongoDB (n.d.). “What Is NoSQL? NoSQL Databases Explained.” https://www.mongodb.com/resources/basics/databases/nosql-explained ↩
  6. MongoDB (n.d.). “Data Modeling.” MongoDB Manual. https://www.mongodb.com/docs/manual/data-modeling/ ↩
  7. Vogels, W. (2008, December 23). “Eventually Consistent - Revisited.” All Things Distributed. Revised for ACM Queue from a December 2007 post. https://www.allthingsdistributed.com/2008/12/eventually_consistent.html ↩
  8. Rails Guides (n.d.). “Active Record Migrations.” https://guides.rubyonrails.org/active_record_migrations.html ↩
  9. MDN Web Docs (n.d.). “CRUD.” Glossary. https://developer.mozilla.org/en-US/docs/Glossary/CRUD ↩ ↩