liveBook · Manning

Foreign key

As explained in Effective Data Analysis

A foreign key is a column or set of columns whose values reference a key in another table to represent a relationship between rows.

A foreign key is a column or set of columns whose values reference a key in another table, allowing related rows to be joined. In analytics warehouses, foreign keys connect new data sources to existing tables through shared dates, user IDs, or location keys. section 11.3.1

Reliable foreign-key relationships depend on practical data-design checks:

  • Keep key values clean, accurate, complete, and stored in the same format across tables. section 11.3.1
  • Map alternate identifiers to shared keys; for example, join customer emails to a unique user_id before wider analysis. section 11.3.1
  • Check malformed records, data types, missing values, and data grain before joining or prejoining datasets. section 11.3.1
Table 11.3 Normalized schema containing available tables collected by the startup
Table name
Description
Columns
customers
Individuals or businesses who have purchased a product or signed up for the utility cost-saving service.
id, first_name, last_name, email, signup_date, customer_type_id, utility_id
customer_types
Type of customer, such as a subscriber to the utility service or purchaser of a product.
id, type
customer_activities
Customer sign-in counts per day.
id, customer_id, day, n_logins, n_activities
addresses
Customer addresses where utilities are being monitored for the utility service. There can be multiple addresses per customer.
id, created_at, customer_id, address, zip_code, country
products
Products sold, such as solar panels and thermostats.
id, created_at, name, type, price
utilities
Utility companies used by customers who signed up for the utility service.
id, name, created_at
utility_readings
Monthly utility usage readings for each customer.
id, month, utility_id, customer_id, usage, cost

Use the normalized schema to identify primary and foreign keys, then trace relationships between entities such as customers, addresses, utilities, and customer activities. section 11.4.3

Foreign keys support restructuring normalized data into fact and dimension tables, including star schemas that make business events and entities easier for analysts and stakeholders to use. section 11.4.3

How Effective Data Analysis covers it

How deep it goes
Explains key matching, prejoining, quality checks, and grain, then applies relationships to fact, dimension, and star-schema design.
Examples
Uses dates, user IDs, location keys, customer identifiers, addresses, utilities, and customer activities as relationship examples.
What it assumes
Assumes familiarity with tables and business data while teaching key matching, data grain, and relationship design through a normalized schema.
What you can do after
Apply it via SQL and schema design to join reliable tables and propose connected fact and dimension models.
How it connects
Connects foreign keys with primary keys, SQL, normalized schemas, data warehouses, fact tables, dimension tables, and star schemas.

Also explained in

Compare how 18 books explain foreign key →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Effective Data Analysis. The section links point to the parts of the book it draws on.