A primary key uniquely identifies each record in a table. No two rows share the same primary key value. The key cannot be null. It must be unique and stable. In a customer table, the primary key might be a customer ID. In an order table, it might be an order number. The primary key is the anchor for the record. Other tables reference it through foreign keys. Without a primary key, records are hard to identify, update, or delete reliably.
Primary keys come in two forms. Natural keys are derived from the data itself. A Social Security number, an email address, or a product SKU. They are meaningful but can change. People change email addresses. Companies change product codes. Surrogate keys are generated by the system. Auto-incrementing integers or UUIDs. They have no meaning outside the database. They never change. Surrogate keys are generally preferred because stability matters more than meaning. A natural key that changes forces updates across every table that references it. A surrogate key stays the same forever. Some tables use composite primary keys made of two or more columns. An order line item might be identified by the combination of order ID and line number. Composite keys work but complicate joins and foreign key references. A single surrogate key is simpler.
Primary key properties
- Unique — no duplicates
- Not null — always has a value
- Stable — does not change over time
- Single or composite — one column or several
- Indexed — automatically for fast lookups
A primary key is an identity. It says this record is this record and no other. Without it, the data has no anchor.
Comments
No comments yet. Be the first to share a thought.
Leave a comment