liveBook · Manning

Composite key

As explained in The Well-Grounded Data Analyst

A composite primary key is a record key whose uniqueness is determined by a combination of multiple columns.

A composite key identifies a record through a combination of columns rather than one ID. It is useful when a single identifier repeats across merged databases or when each observation is defined by several dimensions, such as location, date, hour, and travel direction. section 8.4.1

Establish the intended row granularity before analysis: propose the columns that should make an observation unique, count their distinct combinations, and compare that count with the dataset’s row count. A shortfall means the proposed key omits a distinguishing column or the data contains repeated records. section 8.4.1

Traffic counts by place, time, and direction

For hourly traffic measurements, Count_point_id, Year, Count_date, and hour produce too few distinct combinations. Because the gap is roughly a factor of two, Direction_of_travel is a practical next candidate: traffic may be measured separately in both directions at one location and hour. Remaining duplicate key groups require inspection rather than blind removal, since rows sharing the candidate key can still differ in measurement values. section 8.4.1

len(traffic[["Count_point_id", "Year", "Count_date",
 "hour", "Direction_of_travel"]].drop_duplicates())

Count distinct candidate-key combinations after adding travel direction, then compare the result with the total row count. section 8.4.1

How The Well-Grounded Data Analyst covers it

How deep it goes
Defines composite keys and works through candidate-key counts and duplicate-group inspection without formal database design details.
Examples
Uses hourly traffic counts keyed by count point, date, year, hour, and direction of travel.
What you can do after
Diagnose/debug with candidate-key counts to identify omitted dimensions and investigate duplicate record groups.
How it connects
Connects composite primary keys with time-series granularity, duplicate records, missing values, and aggregation.

Also explained in

Compare how 8 books explain composite key →

Related concepts

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