AUTO_INCREMENT Constraints with MariaDB Enterprise Server
This page is part of MariaDB's Documentation.
The parent of this page is: Constraints for MariaDB Enterprise Server
Topics on this page:
Overview
MariaDB Enterprise Server supports AUTO_INCREMENT constraints:
A column with an
AUTO_INCREMENTconstraint can act as a table's primary key when a natural primary key is not availableGenerated values for auto-increment columns are guaranteed to be unique and monotonically increasing
Auto-increment columns provide compatibility with schemas designed for MariaDB Enterprise Server and MySQL
Alternatively, MariaDB Enterprise Server can use sequences as the primary key instead of columns with AUTO_INCREMENT constraints. Sequences are compliant with the SQL standard, while AUTO_INCREMENT constraints are not, so sequences are the better option for applications that require standard-compliant features.
Compatibility
MariaDB Enterprise Server 10.2
MariaDB Enterprise Server 10.3
MariaDB Enterprise Server 10.4
MariaDB Enterprise Server 10.5
MariaDB Enterprise Server 10.6
Choosing a Data Type for an AUTO_INCREMENT Column
When designing a schema, AUTO_INCREMENT columns should use integer data types. The following types can be used:
Data Type | Signed Range | Unsigned Range |
|---|---|---|
|
| |
|
| |
|
| |
|
| |
|
|
To determine which type to use, consider the following points:
Do you want to be able to manually insert negative values? If not, then specify the
UNSIGNEDattribute for the column.InnoDB can't generate negative
AUTO_INCREMENTvalues, so it is only beneficial to use a signed integer column if you want the option to manually insert negative values, which would bypass theAUTO_INCREMENThandling.How large will your table grow?
If your
AUTO_INCREMENTcolumn is being used as the table's primary key, then the maximum value for the chosen data type should be considered the maximum number of rows that can fit in the table:Data Type
Maximum Number of Rows (Signed)
Maximum Number of Rows (Unsigned)
127255327676553583886071677721521474836474294967295922337203685477580718446744073709551615If you want to give your table the most room to grow, then it would be best to choose
BIGINT UNSIGNED.
Creating an InnoDB Table with an AUTO_INCREMENT Column
Let's create an InnoDB table with an AUTO_INCREMENT column after confirming that the default storage engine is InnoDB:
Connect to the server using MariaDB Client:
$ mariadb --user=rootConfirm that the default storage engine is InnoDB by checking the default_
storage_ system variable using the SHOW SESSION VARIABLES statement:engine SHOW SESSION VARIABLES LIKE 'default_storage_engine';
+------------------------+--------+ | Variable_name | Value | +------------------------+--------+ | default_storage_engine | InnoDB | +------------------------+--------+
If the database does not exist, then create the database for the table using the CREATE DATABASE statement:
CREATE DATABASE hq_sales;Create the table using the CREATE TABLE statement:
CREATE TABLE hq_sales.invoices ( invoice_id BIGINT UNSIGNED AUTO_INCREMENT 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) );
Inserting a Row with an AUTO_INCREMENT Column
If a column is specified as AUTO_INCREMENT, then its value will be automatically generated. There are multiple ways to insert rows with these automatically generated values.
Omitting the Column
If the column is not specified, then InnoDB will automatically generate the value.
Let's insert a row into the table created in the Creating an InnoDB Table with an AUTO_
Connect to the server using MariaDB Client:
$ mariadb --user=rootInsert a row with the INSERT statement, but do not specify the
AUTO_INCREMENTcolumn:INSERT INTO hq_sales.invoices (branch_id, customer_id, invoice_date, invoice_total, payment_method) VALUES (1, 1, '2020-05-10 12:35:10', 1087.23, 'CREDIT_CARD');
Select the same row with the SELECT statement to confirm that a value was automatically generated:
SELECT invoice_id FROM hq_sales.invoices WHERE branch_id = 1 AND customer_id = 1 AND invoice_date = '2020-05-10 12:35:10';
+------------+ | invoice_id | +------------+ | 1 | +------------+
Specifying the Column's Value as 0
If the column's value is specified as 0, then InnoDB will automatically generate the value if the sql_NO_AUTO_VALUE_ON_ZERO.
Let's insert a row into the table created in the Creating an InnoDB Table with an AUTO_
Connect to the server using MariaDB Client:
$ mariadb --user=rootConfirm that the session's value of the sql_
mode system variable does not containNO_AUTO_VALUE_ON_ZEROwith the SHOW SESSION VARIABLES statement:SHOW SESSION VARIABLES LIKE 'sql_mode';
+---------------+-------------------------------------------------------------------------------------------+ | Variable_name | Value | +---------------+-------------------------------------------------------------------------------------------+ | sql_mode | STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION | +---------------+-------------------------------------------------------------------------------------------+
Insert a row with the INSERT statement, and specify the
AUTO_INCREMENTcolumn's value as0:INSERT INTO hq_sales.invoices (invoice_id, branch_id, customer_id, invoice_date, invoice_total, payment_method) VALUES (0, 1, 2, '2020-05-10 14:17:32', 1508.57, 'WIRE_TRANSFER');
Select the same row with the SELECT statement to confirm that a value was automatically generated:
SELECT invoice_id FROM hq_sales.invoices WHERE branch_id = 1 AND customer_id = 2 AND invoice_date = '2020-05-10 14:17:32';
+------------+ | invoice_id | +------------+ | 2 | +------------+
Specifying the Column's Value as NULL
If the column's value is specified as NULL, then InnoDB will automatically generate the value if the column is defined as NOT NULL.
Let's insert a row into the table created in the Creating an InnoDB Table with an AUTO_
Connect to the server using MariaDB Client:
$ mariadb --user=rootInsert a row with the INSERT statement, and specify the
AUTO_INCREMENTcolumn's value asNULL:INSERT INTO hq_sales.invoices (invoice_id, branch_id, customer_id, invoice_date, invoice_total, payment_method) VALUES (NULL, 1, 3, '2020-05-10 14:25:16', 227.15, 'CASH');
Select the same row with the SELECT statement to confirm that a value was automatically generated:
SELECT invoice_id FROM hq_sales.invoices WHERE branch_id = 1 AND customer_id = 3 AND invoice_date = '2020-05-10 14:25:16';
+------------+ | invoice_id | +------------+ | 3 | +------------+
Getting the Last Inserted AUTO_INCREMENT Value
After InnoDB inserts an automatically generated value into an AUTO_INCREMENT column, the application sometimes needs to know what value it inserted. For example, the application may need to use the value to insert a foreign key column in a dependent table. The LAST_AUTO_INCREMENT column without re-reading the row from the table.
Let's insert a row into the table created in the Creating an InnoDB Table with an AUTO_
Connect to the server using MariaDB Client:
$ mariadb --user=rootInsert a row with the INSERT statement, but do not specify the
AUTO_INCREMENTcolumn:INSERT INTO hq_sales.invoices (branch_id, customer_id, invoice_date, invoice_total, payment_method) VALUES (1, 4, '2020-05-10 12:37:22', 104.19, 'CREDIT_CARD');
Execute the LAST_
INSERT_ function to get theID() AUTO_INCREMENTvalue for the new row:SELECT LAST_INSERT_ID();+------------------+ | LAST_INSERT_ID() | +------------------+ | 4 | +------------------+
Select the same row with the SELECT statement to confirm that the
AUTO_INCREMENTcolumn has the same value:SELECT invoice_id FROM hq_sales.invoices WHERE branch_id = 1 AND customer_id = 4 AND invoice_date = '2020-05-10 12:37:22';
+------------+ | invoice_id | +------------+ | 4 | +------------+
Choosing an AUTO_INCREMENT Lock Mode for InnoDB
When multiple rows are inserted into a table concurrently, InnoDB needs to be able to generate multiple values concurrently in a safe manner. It has several different modes that can be used to do this, and each mode has its own advantages and disadvantages.
InnoDB's AUTO_INCREMENT lock mode is configured with the innodb_
Value | Description |
|---|---|
|
|
|
|
|
|
Configuring the AUTO_INCREMENT Lock Mode for InnoDB
The AUTO_INCREMENT Lock Mode for InnoDB is configured by the innodb_
Choose a configuration file for custom changes to system variables and options.
It is not recommended to make custom changes to Enterprise Server's default configuration files, because your custom changes can be overwritten by other default configuration files that are loaded after.
Ensure that your custom changes will be read last by creating a custom configuration file in one of the included directories. Configuration files in included directories are read in alphabetical order. Ensure that your custom configuration file is read last by using the
z-prefix in the file name.Some example configuration file paths for different distributions are shown in the following table:
Distributions
Example configuration file path
CentOSRHELRocky LinuxSLES/etc/my.cnf.d/z-custom-mariadb.cnfDebianUbuntu/etc/mysql/mariadb.conf.d/z-custom-mariadb.cnf
Set the innodb_
autoinc_ system variable in the configuration file.lock_ mode It needs to be set in a group that will be read by MariaDB Server, such as
[mariadb]or[server].For example:
[mariadb] ... innodb_autoinc_lock_mode = 2
Restart the server:
$ sudo systemctl restart mariadb
Setting a Table's Next AUTO_INCREMENT Value
A table's next AUTO_INCREMENT value can be set with the ALTER TABLE statement. The value is set using the AUTO_INCREMENT table option.
Let's alter the AUTO_INCREMENT value for the table created in the Creating an InnoDB Table with an AUTO_
Connect to the server using MariaDB Client:
$ mariadb --user=rootAlter the table's next
AUTO_INCREMENTvalue with the ALTER TABLE statement:ALTER TABLE hq_sales.invoices AUTO_INCREMENT = 100;
Insert a row with the INSERT statement, but do not specify the
AUTO_INCREMENTcolumn:INSERT INTO hq_sales.invoices (branch_id, customer_id, invoice_date, invoice_total, payment_method) VALUES (1, 5, '2020-05-10 12:43:19', 1105.98, 'CREDIT_CARD');
Execute the LAST_
INSERT_ function to get theID() AUTO_INCREMENTvalue for the new row:SELECT LAST_INSERT_ID();+------------------+ | LAST_INSERT_ID() | +------------------+ | 100 | +------------------+
Select the same row with the SELECT statement to confirm that the
AUTO_INCREMENTcolumn has the same value:SELECT invoice_id FROM hq_sales.invoices WHERE branch_id = 1 AND customer_id = 5 AND invoice_date = '2020-05-10 12:43:19';
+------------+ | invoice_id | +------------+ | 100 | +------------+
Configure the Offset and Increment Values
The offset and increment values can be configured by setting the auto_
When is used, the offset and increment values are managed automatically by default. They can be managed manually by disabling the wsrep_
