Skip to content

Lesson 11 ยท Facts and Dimensions

Dimension Tables and Surrogate Keys

Design useful dimension tables with surrogate and natural keys, hierarchies and unknown rows, and generate a date dimension with a runnable SQL query.

Dimensions give facts their meaning. Every filter, group and label in a report comes from a dimension attribute, so a thoughtfully built dimension makes the whole warehouse more useful.

What goes into a dimension

A typical dimension has:

  • A surrogate key: the warehouse's own identifier and primary key.
  • The natural key (also called business key): the identifier from the source, such as a product code.
  • Many descriptive attributes, preferably readable text.
  • Hierarchy columns, such as product, category and department.
  • Technical columns, such as source system and load time.

Prefer words to codes. A column member_status with values Gold, Silver and Not a member is more useful than a flag with 1, 2 and 0.

Why use surrogate keys?

A surrogate key is usually a simple generated integer, or sometimes a hash of the source identifiers. It protects the warehouse from the sources:

  1. Several sources may use the same ID for different things.
  2. Sources may reuse or change their IDs after a migration.
  3. History: one customer can have several dimension rows over time, each with its own surrogate key. This is central to SCD Type 2.
  4. Special rows such as "Unknown" need keys that no source will ever issue.

Keep the natural key as an ordinary column so you can trace rows back and match incoming data.

SQL Surrogate keys protect against clashing source IDs

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

This design keeps both people apart. Deciding that two source records are the same person is a separate matching job; surrogate keys do not do it for you.

How keys are generated differs by platform. Check whether sequences or identity columns on yours promise consecutive numbers; often they do not, and nothing should depend on it.

Hierarchies

A hierarchy is a set of levels, such as city, region and country. In a star you usually store all levels as columns in the same dimension, so users can drill from one level to the next with a change in GROUP BY.

Check that each lower value maps to exactly one higher value. Names can repeat: there are several cities called Springfield, so grouping by city name alone can merge different places. Group by city together with region and country, or use a city identifier.

The date dimension

Almost every warehouse has a date dimension with one row per day. It stores what is tedious or impossible to calculate reliably in each query: fiscal periods, holidays, week numbering rules and friendly labels. Different countries have different holidays and calendars, so build yours from agreed rules.

A common key is an integer such as 20260301. This is a readable exception to meaningless surrogate keys.

SQL Generate a date dimension

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

The weekend rule in this example is itself an assumption: in some countries the weekend falls on other days. Time of day is usually kept in a separate time dimension or a timestamp column, because combining every day with every second would make a huge table. Decide on time zones too: a sale near midnight can fall on different dates in UTC and in the store's local time.

Surrogate key options

There are two common ways to generate surrogate keys:

Option How it works Things to check
Sequence or identity The database hands out numbers Keys must be preserved when tables are rebuilt
Hash of business columns A hash of source, natural key and, for versions, a start value Consistent text formatting, collision risk, wider keys

Hashed keys can be computed independently by parallel jobs and reproduced during a rebuild, provided the inputs are formatted identically every time. Define how you treat case, spaces, NULL values and the separator between parts. Without a separator, the parts ab and c look the same as a and bc. A hash is not a way to hide personal data: identifiers from a small or guessable set can often be recovered by hashing every candidate.

Special members

One "Unknown" row is a start, but different reasons for a missing link can matter. You might define:

Key Meaning
-1 Unknown: a value should exist but could not be matched
-2 Not applicable: this event has no such thing, such as no promotion
-3 Not yet happened: for a future milestone date

The numbers are a convention you choose; document them. Distinct rows let a report separate "no promotion" from "promotion data is broken", which call for very different actions.

Names that people understand

Dimension columns appear directly in filters and chart labels, so name them for readers. Prefer store_country to a bare country when several dimensions have countries. Give sortable partners to text labels: a month_name column needs a month_number column, or charts will list April before January. Keep the same label for the same concept across all dimensions.

Check a dimension before you publish it

Run a few quick checks. Is every surrogate key unique? Does each natural key appear only as often as your history rules allow? Does every reserved row exist? Which attributes are mostly empty? Does any code still lack a readable label? Then browse the distinct values of important attributes. Spellings such as Footwear, footwear and Foot wear show up at once, and each would become a separate bar in a chart.

Common mistakes

Do not split a dimension into many tiny lookup tables just because values repeat. And do not leave attributes empty without explanation: a label such as "Not recorded" is clearer than a blank cell in a chart.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a surrogate key?

    It is the warehouse's own key; the source's natural key is stored alongside.
  2. 2. Why keep the natural key in the dimension?

    Loads use the natural key to find the right dimension row.
  3. 3. What is a good reason for a date dimension?

    Calendar rules are defined once and used by every query.
  4. 4. Why can grouping by city name alone be wrong?

    Include the parent levels or a city identifier.

Summary

  • Dimensions provide the words used to filter, group and label facts.
  • Surrogate keys isolate the warehouse from sources and allow history and special rows.
  • Store hierarchy levels as columns and check how the levels map.
  • A date dimension holds your organization's calendar rules.

Next: Special Dimension Patterns

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