mySQL / MariaDB - Understanding PRIMARY KEY and auto_increment

by
Jeremy Canfield |
Updated: December 19 2021
| mySQL / MariaDB articles
When creating a table, adding a column to a table, or updating a column in a table, the column can have the Key flag set to PRI (PRIMARY KEY) and empty.
In this example, the "id" column is set as the PRIMARY KEY and the "name" column has no key set.
~]$ mysql -e "describe freekb.table001"
Field Type Null Key Default Extra
id int(11) NO PRI NULL auto_increment
name varchar(200) YES NULL
Each table can have only one column set as the PRIMARY KEY.
When a column has the PRIMARY KEY set, the column must adhere to these rules:
- The column must contain unique values (no duplicate identical values)
- The column cannot contain NULL (empty) values
Often, the "id" column has the PRIMARY KEY set and auto_increment, so that when a new row is added to the table, the ID is automatically incremented by 1, which ensure the "id" column contains unique values and does not contain any null/empty values.
Did you find this article helpful?
If so, consider buying me a coffee over at