Using an AUTO_INCREMENT column as the primary key in MariaDB is beneficial for ensuring unique row identification and efficient data retrieval. This approach automatically generates a unique integer for each new row, simplifying primary key management. It is especially useful in tables where data insertion occurs frequently, as it facilitates quick indexing and minimizes storage overhead due to its numeric nature. This practice helps maintain optimal performance in database operations, particularly when combined with clustered indexing in InnoDB.
MariaDB Server supports PRIMARY KEY constraints to uniquely identify rows:
There can only be a single primary key for a given table.
InnoDB uses the primary key as a clustered index, which means that InnoDB stores table data in the order determined by the primary key.
Primary key indexes are B+ trees, which are very efficient for searching for exact values, performing range scans, and checking uniqueness.
If no primary key is defined for a table, then InnoDB will use the table's first NOT NULL unique index as the table's primary key.
If your table does not have a column or a set of columns that could act as a natural primary key, then you can define a single AUTO_INCREMENT column, which can serve as the table's primary key. See for more details.
If your table does not have a column or a set of columns that could act as a natural primary key, then you can use a sequence to generate an integer value to use as the table's primary key. Sequences were first added in MariaDB Server 10.3 and MariaDB Community Server 10.3. See for more details.
Let's key after confirming that the default storage engine is InnoDB:
Connect to the server using MariaDB Client:
Confirm that the default storage engine is InnoDB by checking the using the statement:
If the database does not exist, then create the database for the table using the statement:
Create the table using the statement and specify the primary key with the PRIMARY KEY() clause:
For a single column primary key, the primary key can also be specified with the PRIMARY KEY column option:
Let's create an InnoDB table with a composite (multi-column) primary key after confirming that the default storage engine is InnoDB:
Connect to the server using MariaDB Client:
Confirm that the default storage engine is InnoDB by checking the using the statement:
If the database does not exist, then create the database for the table using the statement:
Create the table using the statement and specify the primary key with the PRIMARY KEY() clause:
Let's , and then add a primary key to it:
Connect to the server using MariaDB Client:
Confirm that the default storage engine is InnoDB by checking the using the statement:
If the database does not exist, then create the database for the table using the statement:
Create the table without a primary key using the statement:
Alter the table using the statement and specify the new primary key with the ADD PRIMARY KEY() clause:
Let's drop the primary key from the table created in the section:
Alter the table using the statement and specify the DROP PRIMARY KEY clause:
Let's change the primary key from the table created in the section:
Alter the table using the statement and specify the DROP PRIMARY KEY clause to drop the old primary key, and specify the new primary key with the ADD PRIMARY KEY() clause:
For best performance, every InnoDB table should have a primary key. It is possible to find tables without a primary key using a basic statement.
Let's create an InnoDB table without a primary key, and then use a statement to confirm that it does not have one:
Connect to the server using MariaDB Client:
Confirm that the default storage engine is InnoDB by checking the using the statement:
If the database does not exist, then create the database for the table using the statement:
Create the table without a primary key using the statement:
Query the .TABLES and tables to find InnoDB tables that do not have a primary key:
To add a primary key, alter the table using the statement, and specify the primary key with the ADD PRIMARY KEY() clause:
A primary key uniquely identifies every row. Therefore, if a second row is inserted with an identical value, it will fail.
Let's try to insert two identical primary key values into the table created in the section:
Insert a row with the statement:
Insert a second row that has the same primary key value with the statement:
This will fail with the ER_DUP_ENTRY error code:
Fix the problem by inserting the row with a unique primary key value:
To easily generate unique values for a primary key, consider using one of the following options:
NOT NULLGEN_CLUST_INDEX48-bit DB_ROW_IDMIXED or ROW!For best performance, users should always create primary keys for their tables, and primary keys should be short, because the primary key columns are duplicated in every secondary index record.
$ mariadb --user=rootSHOW SESSION VARIABLES
LIKE 'default_storage_engine';
+------------------------+--------+
| Variable_name | Value |
+------------------------+--------+
| default_storage_engine | InnoDB |
+------------------------+--------+CREATE DATABASE hq_sales;CREATE TABLE hq_sales.invoices (
invoice_id BIGINT UNSIGNED NOT NULL,
branch_id INT NOT NULL,
customer_id INT,
invoice_date DATETIME(6),
invoice_total DECIMAL(13, 2),
payment_method ENUM('NONE', 'CASH', 'WIRE_TRANSFER', 'CREDIT_CARD', 'GIFT_CARD'),
PRIMARY KEY(invoice_id)
);CREATE TABLE hq_sales.invoices (
invoice_id BIGINT UNSIGNED NOT NULL PRIMARY KEY,
branch_id INT NOT NULL,
customer_id INT,
invoice_date DATETIME(6),
invoice_total DECIMAL(13, 2),
payment_method ENUM('NONE', 'CASH', 'WIRE_TRANSFER', 'CREDIT_CARD', 'GIFT_CARD')
);$ mariadb --user=rootSHOW SESSION VARIABLES
LIKE 'default_storage_engine';
+------------------------+--------+
| Variable_name | Value |
+------------------------+--------+
| default_storage_engine | InnoDB |
+------------------------+--------+CREATE DATABASE hq_sales;CREATE TABLE hq_sales.invoices (
invoice_id BIGINT UNSIGNED NOT NULL,
branch_id INT NOT NULL,
customer_id INT,
invoice_date DATETIME(6),
invoice_total DECIMAL(13, 2),
payment_method ENUM('NONE', 'CASH', 'WIRE_TRANSFER', 'CREDIT_CARD', 'GIFT_CARD'),
PRIMARY KEY(invoice_id, branch_id)
);$ mariadb --user=rootSHOW SESSION VARIABLES
LIKE 'default_storage_engine';
+------------------------+--------+
| Variable_name | Value |
+------------------------+--------+
| default_storage_engine | InnoDB |
+------------------------+--------+CREATE DATABASE hq_sales;CREATE TABLE hq_sales.invoices (
invoice_id BIGINT UNSIGNED NOT NULL,
branch_id INT NOT NULL,
customer_id INT,
invoice_date DATETIME(6),
invoice_total DECIMAL(13, 2),
payment_method ENUM('NONE', 'CASH', 'WIRE_TRANSFER', 'CREDIT_CARD', 'GIFT_CARD')
);ALTER TABLE hq_sales.invoices ADD PRIMARY KEY (invoice_id);ALTER TABLE hq_sales.invoices DROP PRIMARY KEY;ALTER TABLE hq_sales.invoices
DROP PRIMARY KEY,
ADD PRIMARY KEY (invoice_id, branch_id);$ mariadb --user=rootSHOW SESSION VARIABLES
LIKE 'default_storage_engine';
+------------------------+--------+
| Variable_name | Value |
+------------------------+--------+
| default_storage_engine | InnoDB |
+------------------------+--------+CREATE DATABASE hq_sales;CREATE TABLE hq_sales.invoices (
invoice_id BIGINT UNSIGNED NOT NULL,
branch_id INT NOT NULL,
customer_id INT,
invoice_date DATETIME(6),
invoice_total DECIMAL(13, 2),
payment_method ENUM('NONE', 'CASH', 'WIRE_TRANSFER', 'CREDIT_CARD', 'GIFT_CARD')
);SELECT t.TABLE_SCHEMA, t.TABLE_NAME
FROM information_schema.TABLES AS t
LEFT JOIN information_schema.KEY_COLUMN_USAGE AS c
ON t.TABLE_SCHEMA = c.CONSTRAINT_SCHEMA
AND t.TABLE_NAME = c.TABLE_NAME
AND c.CONSTRAINT_NAME = 'PRIMARY'
WHERE t.TABLE_SCHEMA != 'mysql'
AND t.ENGINE = 'InnoDB'
AND c.CONSTRAINT_NAME IS NULL;+--------------+------------+
| TABLE_SCHEMA | TABLE_NAME |
+--------------+------------+
| hq_sales | invoices |
+--------------+------------+ALTER TABLE hq_sales.invoices ADD PRIMARY KEY (invoice_id);INSERT INTO hq_sales.invoices
(invoice_id, branch_id, customer_id, invoice_date, invoice_total, payment_method)
VALUES
(1, 1, 1, '2020-05-10 12:35:10', 1087.23, 'CREDIT_CARD');INSERT INTO hq_sales.invoices
(invoice_id, branch_id, customer_id, invoice_date, invoice_total, payment_method)
VALUES
(1, 1, 2, '2020-05-10 14:17:32', 1508.57, 'WIRE_TRANSFER');ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'INSERT INTO hq_sales.invoices
(invoice_id, branch_id, customer_id, invoice_date, invoice_total, payment_method)
VALUES
(2, 1, 2, '2020-05-10 14:17:32', 1508.57, 'WIRE_TRANSFER');This page is: Copyright © 2025 MariaDB. All rights reserved.