liveBook · Manning

Primary key

As explained in Grokking Software Architecture

A relational database key that uniquely identifies each row in a table and cannot contain a null value.

A primary key (PK) is a table’s anchor for row identity. Its value must be unique and cannot be null, so it identifies one record distinctly. section 8.2.1

Two users, one name

Two users can both be named John Smith. Deleting by name could remove both accounts when only one requested closure. Use the primary key to identify the intended row instead. section 8.2.1

A foreign key can reference a primary key in another table. For example, Orders.CustomerId can reference Customers.Id. The foreign-key constraint must be explicitly defined; the column name alone does not create the link. section 8.2.1

Figure 8.2 Showing how Primary Keys and Foreign Keys work together to link tables. The child table (Orders) uses the Customer_ID as a Foreign Key to point directly back to its parent record (Customers), establishing a clear one-to-many relationship.

See how an Orders foreign key points to a Customers primary key in a one-to-many relationship. section 8.2.1

How Grokking Software Architecture covers it

How deep it goes
Defines uniqueness and non-null requirements and explains relationships, without showing primary-key creation syntax.
Examples
Illustrates duplicate-name deletion and an Orders-to-Customers relationship.
What you can do after
Evaluate/understand how primary keys identify rows and support links between tables.
How it connects
Connects primary keys to foreign keys, relational integrity, and one-to-many relationships.

Also explained in

Compare how 22 books explain primary key →

Related concepts

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