Skip to content

Lesson 7 ยท Data Architecture and Storage

Data Modeling Essentials

Learn conceptual, logical and physical data models, entities, keys, relationships and normalization, and use database constraints to enforce your model's rules.

A data model describes the things your organization keeps data about and how they relate. It is a shared language between business people and builders. A good model exposes disagreements early, when they are cheap to fix: can one order have two billing addresses? Can a product belong to several categories?

Three levels of models

Conceptual

What things exist Customer, Order, Product
For Business conversations
1 / 3

Start conceptual. If the business cannot agree on what a "customer" is, no table design will save you.

Entities, attributes and relationships

An entity is a thing you track, such as a customer. An attribute is a fact about it, such as a name. A relationship connects entities, with a cardinality:

  • one-to-one: one employee has one payroll profile;
  • one-to-many: one customer places many orders;
  • many-to-many: a student takes many courses and a course has many students, usually modeled with a linking entity such as enrollment.
erDiagram
  CUSTOMER ||--o{ ORDER : places
  ORDER ||--|{ ORDER_LINE : contains
  PRODUCT ||--o{ ORDER_LINE : "appears in"

A customer places orders; each order has one or more lines; each line refers to a product.

Keys

A primary key uniquely identifies a row. A natural key comes from the business, such as a national tax number. A surrogate key is an artificial number or ID created for the system. Natural keys can change or be reused, so many designs use a surrogate primary key and still place a uniqueness rule on the natural key. A foreign key points to the primary key of another table.

Normalization in one minute

Normalization organizes data so each fact is stored in one place. If a customer's city is repeated on every order, updating it means changing many rows, and some will be missed. Move the city to the customer table. Analytical designs often denormalize on purpose for speed and simplicity; see normalization for the formal steps. Remember that historical documents, such as an invoice, may need to keep the address as it was at the time. That is a business rule, not a mistake.

Let the database enforce the model

Rules written only in documents get broken. Constraints make the database refuse bad data. The example below uses NOT NULL, UNIQUE, CHECK and a foreign key.

SQL Constraints that protect a model (SQL)

Press Run (or Ctrl + Enter) to see the result here.

OR IGNORE is used here only so you can see that the bad rows are rejected without stopping the script. In real loading code, silently skipping rows hides problems. Record rejected rows in an error table and tell someone. Note that OR IGNORE does not apply to foreign key violations; those raise an error.

Model documentation

For each entity, record its definition in business words, an example, the key, the owner and important rules:

Entity Definition Key Rule
Customer A person or company that has bought at least once customer_id One email per customer
Order A confirmed request to buy order_id Has one or more lines

The definition "bought at least once" is a choice. A different company might count a registered visitor as a customer. Writing it down prevents silent disagreement.

Changing a model safely

Models evolve. Safer changes are additive, such as adding an optional column. Riskier changes rename or remove things that others use. Before a risky change:

  1. Find who consumes the data (lineage helps).
  2. Announce the change and a timeline.
  3. Run old and new forms side by side if possible.
  4. Migrate consumers, then remove the old form.

Do not use free text where a controlled list is needed. A "status" column that accepts anything will soon contain "Active", "active", "ACT" and "yes". Use a lookup table or a check constraint.

Questions that expose hidden rules

Modeling is mostly asking careful questions. For any relationship, try these:

  • Can there be zero? A new customer has no orders yet.
  • Can there be more than one? A person may have several phone numbers.
  • Can it change over time? A product may move to another category.
  • Do we need the old value? A report about last year may need the old category.
  • Who is allowed to change it, and what happens to related records if it is deleted?

In a fictional gym, a manager says each member has one membership plan. Asking about time reveals that members switch plans and that finance needs the price they paid in each month. The model then needs a membership period entity with a start date, an end date and the agreed price, not just a plan column on the member.

Naming and readability

Use full, consistent names. Decide whether table names are singular or plural and keep to it. Use the same word for the same concept everywhere: if one table says client and another says customer, readers will wonder whether they differ. Include units in names where confusion is likely, such as weight_kg or amount_cents. A model that a new colleague can read without a guided tour will be used correctly more often.

Check the model with examples

Before building, walk through real-looking cases with business people: a new customer with no orders, an order with three lines, a cancelled order, a product that is no longer sold and a customer who changes address. For each case, point to where the facts would be stored. If a case has no clear home, the model is not finished. Keep these cases, because they make good tests when the tables are built or changed later.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which model level is best for agreeing with business people on what things exist?

    Conceptual models focus on entities and relationships without technical detail.
  2. 2. How is a many-to-many relationship usually modeled?

    A linking entity, such as enrollment, holds one row per pair.
  3. 3. What is the main benefit of normalization?

    Reducing repetition reduces update errors.
  4. 4. Why record rejected rows instead of silently ignoring them?

    Silent loss hides quality problems and can make totals wrong.

Summary

  • Work from conceptual to logical to physical models.
  • Entities, attributes, relationships and keys form the core vocabulary.
  • Normalize operational data; denormalize analytics deliberately.
  • Use constraints to enforce rules, and log rejected data.
  • Plan breaking changes with consumers in mind.

Next: Data Integration and Interoperability

Your progress is saved in this browser. No account needed.