liveBook · Manning

Primary key

As explained in Starting Data Analytics with Generative AI and Python

A primary key is a column or set of columns whose values uniquely identify records in a dataset.

Before combining datasets, ask: which column identifies each record reliably? A primary key is a column or set of columns whose values uniquely identify records. It provides the reference point for relating DataFrames and checking whether other datasets point to existing records. section 2.5.4

Customers, orders, and payments

In a customer DataFrame, customer identifiers act as primary keys. An orders DataFrame can reference those identifiers, while an order_payments DataFrame can use order_id as a foreign key referencing the primary key in df_orders. Inspect the relationships rather than trusting identical column names: missing references or unexpected one-to-one relationships can reveal data-quality problems. section 2.5.4

Listing 2.2 Identifying foreign key violations
foreign_key_violations = 
df_orders[~df_orders['order_id'].isin(df_order_payments['order_id'])]
if not foreign_key_violations.empty:
    print("Foreign key violations found:")
    print(foreign_key_violations)
    # Handle violations (e.g., drop rows or correct the foreign key values)

Use the foreign-key violation check to find records whose supposed references do not exist in the primary-key dataset. section 2.5.4

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Explains reference and foreign-key relationships with customer, order, and payment examples, then stops short of resolving one violation.
Examples
Uses customer identifiers, order_id, customer orders, payment records, and a foreign-key violation check.
What it assumes
Assumes familiarity with DataFrames and identifiers while teaching how reference and foreign-key datasets relate.
What you can do after
Diagnose broken references and unexpected dataset relationships by comparing foreign-key values with primary-key records.
How it connects
Connects primary keys with foreign keys, DataFrames, data validation, and data lineage.

Also explained in

Compare how 21 books explain primary key →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Starting Data Analytics with Generative AI and Python. The section links point to the parts of the book it draws on.