Primary Keys
A primary key is a column, or group of columns, used to identify a row.
- must be unique and not
NULL - identified by a bullet (●) preceding the primary key in table diagrams
- is usually left-most column
Composite Primary Keys
A simple primary key consists of a single column.
A composite primary key consists of multiple columns.
- must be:
- Unique
- Not NULL
- Minimal
- All primary key columns are necessary for uniqueness
- enclosed in parentheses
- e.g.,
(ColumnA, ColumnB)
- e.g.,
Primary Key Constraint
The primary key constraint in a CREATE TABLE statement names the table’s primary key.
- ensures that a column or group of columns is always unique and non-null
- primary key column definition usually appears first
- followed by other column definitions and the primary key constraint
Auto-increment Columns
An auto-increment column is a numeric column that is assigned an automatically incrementing value when a new row is inserted.
AUTO_INCREMENTkeyword defines an auto-increment column- follows the column’s data type in a
CREATE TABLEstatement
- follows the column’s data type in a
- Integer primary keys are commonly implemented as auto-increment columns
- in MySQL, may be applied only to primary key columns