Important
The SingleStore 9.1 release candidate (RC) gives you the opportunity to preview, evaluate, and provide feedback on new and upcoming features prior to their general availability. In the interim, SingleStore 9.0 is recommended for production workloads, which can later be upgraded to SingleStore 9.1.
CREATE TABLE with an AUTO_ INCREMENT Column
On this page
AUTO_ can be used to automatically generate a unique value for new rows.AUTO_ field is DEFAULT, NULL, or 0, SingleStore automatically assigns a value.AUTO_ only guarantees that automatically-generated values are unique.
-
are consecutive or sequential
-
are monotonically increasing
-
start from any particular value
-
are distinct from explicitly-set values
If you explicitly set a value in an INSERT or UPDATE statement, it may collide with past or future automatically-generated values.AUTO_ column.AGGREGATOR SYNC AUTO_ is run by the end user (which will reset the auto-increment counter to a value higher than 10).AUTO_ value reaches 10 eventually on its own as additional rows are added to the table.
CREATE TABLE ct_auto (c1 INT AUTO_INCREMENT PRIMARY KEY);INSERT INTO ct_auto (c1) VALUES (10);
A table can have only one AUTO_ column.AUTO_ column must be included in an index (not necessarily a PRIMARY or UNIQUE key, a regular key is also allowed).
If syntax such as CREATE TABLE table_ is used to create table_ where table_ has an AUTO_ column, it will be created as a non-auto-increment column in table_.
See LAST_AUTO_ behavior.
If the AUTO_ behavior described here does not satisfy your requirements you can create your own sequence generator using LAST_.
Warning
Restarting an aggregator, such as during upgrades or host machine maintenance, will introduce a large gap between any AUTO_ values inserted before the restart and any values inserted after.
These jumps are because each aggregator defines and manages its own range of values to start incrementing from to prevent collisions in a table.AUTO_ values increases to the next 1,000,000 after each restart (e.
AUTO_ INCREMENT in Sharded Tables
On a sharded (distributed) table, AUTO_ can only be used on a BIGINT column (as they usually use the entire 64 bits).AUTO_ values and uses those values when new rows are added to a table.AUTO_ values, the automatically-generated values from inserts across multiple aggregators are only unique, never sequential.
Note
If an AUTO_ column (without the SEQUENCE modifier) in a sharded table is declared with a smaller integer type, it is implicitly converted to BIGINT.
Here is an example to illustrate how AUTO_ values are generated across aggregators in a cluster as new rows are inserted into table ct_:
SELECT * FROM ct_tb ORDER BY b;
+-------------------+------+------------+
| a | b | c |
+-------------------+------+------------+
| 1 | 1 | from MA |
| 2 | 2 | from MA |
| 3 | 3 | from MA |
| 4 | 4 | from MA |
| 5 | 5 | from MA |
| 13510798882111489 | 6 | from CA 96 |
| 13510798882111490 | 7 | from CA 96 |
| 13510798882111491 | 8 | from CA 96 |
| 13510798882111492 | 9 | from CA 96 |
| 13510798882111493 | 10 | from CA 96 |
| 14636698788954113 | 11 | from CA 20 |
| 14636698788954114 | 12 | from CA 20 |
| 14636698788954115 | 13 | from CA 20 |
| 14636698788954116 | 14 | from CA 20 |
| 14636698788954117 | 15 | from CA 20 |
| 6 | 16 | from MA |
| 15762598695796737 | 17 | from CA 17 |
| 13510798882111494 | 18 | from CA 96 |
| 7 | 19 | from MA |
| 14636698788954118 | 20 | from CA 20 |
+-------------------+------+------------+As shown in the example above, automatically-generated AUTO_ values can differ depending on which aggregator you run the inserts on.
AUTO_ INCREMENT in Reference Tables
The AUTO_ value for a reference table is tracked by the master aggregator.AUTO_ value will always be greater than any value previously seen in this column.INSERT or UPDATE statement will not create a collision with future automatically generated values.
The next example shows some queries using AUTO_ fields on reference tables.
CREATE REFERENCE TABLE ct_ref_1(id INT AUTO_INCREMENT PRIMARY KEY);INSERT INTO ct_ref_1 VALUES();INSERT INTO ct_ref_1 VALUES(5);INSERT INTO ct_ref_1 VALUES();
SELECT id FROM ct_ref_1 ORDER BY id;
+----+
| id |
+----+
| 1 |
| 5 |
| 6 |
+----+UPDATE ct_ref_1 SET id = 9 WHERE id = 5;INSERT INTO ct_ref_1();
SELECT id FROM ct_ref_1 ORDER BY id;
+----+
| id |
+----+
| 1 |
| 6 |
| 9 |
| 10 |
+----+DELETE FROM ct_ref_1;INSERT INTO ct_ref_1 VALUES();
SELECT id FROM ct_ref_1 ORDER BY id;
+----+
| id |
+----+
| 11 |
+----+Setting AUTO_ INCREMENT Starting Values
It is possible to override the starting AUTO_ value for reference tables by setting the AUTO_ option on a CREATE TABLE statement.
The following example shows how to set the AUTO_ start value during table creation:
CREATE REFERENCE TABLE ct_ref_2 (id int AUTO_INCREMENT PRIMARY KEY) AUTO_INCREMENT = 7;INSERT INTO ct_ref_2 VALUES (), ();
SELECT * FROM ct_ref_2;
+----+
| id |
+----+
| 7 |
| 8 |
+----+This syntax has no effect on sharded tables.
AUTO_ INCREMENT During Replication
When replicating data between clusters, the secondary cluster has all the replicated AUTO_ values from the primary cluster.AUTO_ values on every aggregator.
CREATE TABLE IF NOT EXISTS my_MemSQL_table (id INT PRIMARY KEY AUTO_INCREMENT, v VARCHAR(10) NOT NULL);
AUTO_ INCREMENT AS SEQUENCE
The SEQUENCE modifier ("the sequence" or "Sequences") for AUTO_ ensures generation of a unique sequence of numbers while greatly reducing the difference between the generated numbers.SEQUENCE modifier allows you to set the column type for AUTO_ columns to other integer types.
SEQUENCE Behavior
When the SEQUENCE modifier is specified, each aggregator pre-fetches a range of sequences and reserves the range in cache.200 and the SingleStore deployment has two aggregators, the insert on the first aggregator inserts 1 and the insert on the second aggregator inserts 201 as follows:
-- Create a table with AUTO_INCREMENT AS SEQUENCE column --CREATE TABLE stock (stockID BIGINT AUTO_INCREMENT AS SEQUENCE PRIMARY KEY,Code CHAR(4));-- Insert via aggregator 1, cached values in the range 1-200, inserts 1 --INSERT INTO stock (Code) VALUES ('AG01');-- Insert via aggregator 2, cached values in the range 201-400, inserts 201 --INSERT INTO stock (Code) VALUES ('AG02');-- Query the table --SELECT * FROM stock ORDER BY stockID;
+---------+------+
| stockID | Code |
+---------+------+
| 1 | AG01 |
| 201 | AG02 |
+---------+------+The same applies to different transactions running on the same aggregator.
If the SEQUENCE modifier is specified for an AUTO_ column, synchronization of the sequence counter across aggregators is not required unless manual inserts are performed to correct the sequence (which requires synchronization).
Key Benefits of Using the SEQUENCE Modifier
The SEQUENCE modifier provides the following improvements over AUTO_:
-
Generates unique sequential numbers and greatly reduces the difference between the generated numbers.
-
Auto-synchronization of the sequence across aggregators is not required (unless manual inserts or updates to the sequence are performed).
-
Supports any integer type for column type (
AUTO_only supports theINCREMENT BIGINTtype). -
Allows specifying a starting value for the sequence and resetting or updating the current sequence value to a specific number.
Create a Column with AUTO_ INCREMENT AS SEQUENCE
Use the following syntax to create an AUTO_ column with the SEQUENCE modifier:
CREATE TABLE (table_name) (column_name <type> AUTO_INCREMENT AS SEQUENCE [ <index/key> ]) [ AUTO_INCREMENT=<starting_value> ]
where,
-
<type>specifies the column type.Columns with the SEQUENCEmodifier support any integer type. -
AUTO_specifies a custom starting value for the sequence.INCREMENT=<starting_ value> -
index/keyspecifies the type of index or key.Note
The
AUTO_column must be included in an index (not necessarily aINCREMENT AS SEQUENCE PRIMARYorUNIQUEkey, a regular key is also allowed).
For example, the following command creates an AUTO_ column orderID with the SEQUENCE modifier and specifies a custom starting value for the sequence:
CREATE TABLE orders (orderID BIGINT AUTO_INCREMENT AS SEQUENCE PRIMARY KEY,customerID CHAR(4)) AUTO_INCREMENT = 1000;INSERT INTO orders (customerID)VALUES ('AA01'), ('AAO2');SELECT * FROM orders;
+---------+------------+
| orderID | customerID |
+---------+------------+
| 1000 | AA01 |
| 1001 | AAO2 |
+---------+------------+Reset or Update the Current Sequence Value
Use the following syntax to reset the current value of the sequence to a custom value:
ALTER TABLE <table_name> AUTO_INCREMENT = <new_value>;
Note
The ALTER TABLE . command does not validate whether the specified value exists or is lower than the current maximum value in the table.Duplicate entry '<value>' for key 'PRIMARY' error on insert operations.
Alternatively, run the following command to reset the sequence counter to a minimum of <maximum_:
AGGREGATOR SYNC AUTO_INCREMENT ON <database>.<table_name> ALL;
Additionally, if manual inserts are performed in a table with a SEQUENCE column, run the AGGREGATOR SYNC AUTO_ command to reset the sequence counter.
Because the sequence must synchronize to all the aggregators at once, only AGGREGATOR SYNC AUTO_ statements are supported on tables with SEQUENCE columns.
Example: Update the Sequence after Manual Insert
The following example shows how to synchronize the current sequence value after a manual update to the sequence column and the sequence behavior during this operation.
In the orders table created earlier:
-
Manually insert a few rows with custom values for the
SEQUENCEcolumn:INSERT INTO ordersVALUES (1100, 'AB10'), (1200, 'AC10'); -
Synchronize the sequence across aggregators:
AGGREGATOR SYNC AUTO_INCREMENT ON dbTest.orders ALL; -
Insert row(s) using the automatically generated sequence:
INSERT INTO orders (customerID)VALUES ('BA01'), ('BA02'); -
Query the values in the
orderstable:SELECT * FROM orders ORDER BY orderID;+---------+------------+ | orderID | customerID | +---------+------------+ | 1000 | AA01 | | 1001 | AAO2 | | 1100 | AB10 | | 1200 | AC10 | | 1201 | BA01 | | 1202 | BA02 | +---------+------------+Notice that the current sequence value was reset to "the highest value in the sequence column after the manual insert plus one" (
1200 + 1), and the new rows added with the automatically generated sequence have greater values (1201+).
Example: Reset the Current Sequence Value
To manually reset the sequence:
-
Run the
ALTER TABLE .command as:. . AUTO_ INCREMENT ALTER TABLE orders AUTO_INCREMENT = 1500; -
The next
INSERTstatement uses sequence values from1500.For example: INSERT INTO orders (customerID)VALUES ('CA01');SELECT * FROM orders ORDER BY orderID;+---------+------------+ | orderID | customerID | +---------+------------+ | 1000 | AA01 | | 1001 | AAO2 | | 1100 | AB10 | | 1200 | AC10 | | 1201 | BA01 | | 1202 | BA02 | | 1500 | CA01 | +---------+------------+
SEQUENCE Remarks
-
Only one sequence column is allowed for each table.
-
Sequences are not supported in reference or temporary tables.
To generate unique values on reference tables, use AUTO_(without theINCREMENT SEQUENCEmodifier).Refer to AUTO_ INCREMENT in Reference Tables for more information. -
The
AUTO_column must be included in an index (not necessarily aINCREMENT AS SEQUENCE PRIMARYorUNIQUEkey, a regular key is also allowed). -
The
AGGREGATOR SYNC AUTO_command does not guarantee the correctness of the sequence value if inserts are being performed on the table concurrently.INCREMENT ON <table> ALL
Configure Sequences
Use the following engine variables to configure Sequences (refer to the list of engine variables for more information including default values):
|
Variable Name |
Description |
|---|---|
|
|
Minimum number of sequence values pre-fetched by an aggregator. |
|
|
Maximum size the pre-fetch cache can reach after |
|
|
Specifies a time interval in microseconds. |
|
|
Specifies a minimum time interval (in microseconds) that must elapse between cache requests before SingleStore downgrades the cache size for the sequence by 2x. |
The sequence_ and sequence_ time is measured at the owner partition, taking into account requests from all aggregators.
AUTO_ INCREMENT Data Type Requirements in Client Applications
When reading AUTO_ values into client applications, ensure that the application-side data type can store the entire range of values generated by an AUTO_ column.AUTO_ can only be used on a BIGINT column and generated identifiers typically use the full 64-bit range.AUTO_ column (without the SEQUENCE modifier) in a sharded table is declared with a smaller integer type, it is implicitly converted to BIGINT.AUTO_ value and can consequently lead to the following:
-
duplicate key errors when they are written back to the database, or
-
client-side overflow errors or other loss of information in the client application.
If your application uses ORMs, frameworks, or runtime libraries that abstract underlying SQL types, verify that the mapped type used for AUTO_ identifiers can hold the generated values.
Warning
Although AUTO_ values may initially fall in the range of a smaller integer type, they can eventually grow to require the full 64-bit range.
Do not use the following types in client applications to store auto_
-
32-bit integer types
-
Double-precision floating-points types
-
numbertype in JavaScript or TypeScript
The following table specifies the recommended type for common programming languages:
|
Language |
Recommended Data Type |
|---|---|
|
JavaScript |
|
|
Python |
|
|
C/C++ |
|
|
C#/. |
|
|
Go |
|
|
Rust |
|
|
Java |
|
|
Other languages |
Signed integer type with at least 64 bits of precision. |
If the application code or framework does not support 64-bit identifiers or if smaller identifier types are preferred, use the following:
-
Use AUTO_
INCREMENT AS SEQUENCE: A column defined as AUTO_ INCREMENT AS SEQUENCE can use any integer type, which allows you to configure a smaller data type while still using sequence semantics. However, you must ensure the following: -
The table row count does not exceed the maximum value supported by the selected integer type.
-
Gaps introduced in the sequence (for example, from restarts, manual inserts, or sequence resets) do not exhaust the available integer range earlier than expected.
-
-
Generate identifiers in the application logic: Generate primary key values within the application using a scheme compatible with the application’s preferred identifier type.
In this approach, AUTO_is not used for the primary key, and the application is responsible for ensuring uniqueness, handling concurrency, and preventing identifier conflicts.INCREMENT
SingleStore recommends using 64-bit integer types for AUTO_ columns and in client applications to avoid precision loss, identifier range conflicts, and duplicate key errors.
AUTO_ INCREMENT Best Practices
AUTO_ INCREMENT On Sharded Tables
On sharded tables, define the AUTO_ column as BIGINT.
-
Syncing auto-increment values after a manual insert or update
-
Node restart
Note
AUTO_ gaps are expected on sharded tables and do not indicate data loss.
When large, sparse auto-increment values are acceptable, use AUTO_ without SEQUENCE.AUTO_, which also supports smaller integer types.SEQUENCE columns may lead to premature exhaustion of values on high-volume tables.
Ensure AUTO_
Manual Inserts, Updates, or Deletes
Avoid manual inserts or updates to the AUTO_ column.AUTO_ column, run AGGREGATOR SYNC AUTO_ to sync the auto-increment values across all the aggregators and reset the counter to a value greater than the current maximum.
AGGREGATOR SYNC AUTO_INCREMENT [ON <database>[.<table>]] ALL;
This command is not required for delete operations because SingleStore does not reuse auto-increment values by default.
AUTO_ INCREMENT On Reference Tables
AUTO_ values on reference tables are always strictly increasing.AUTO_ column in a reference table, subsequent automatically generated values are always greater than any existing value in the AUTO_ column.
Schema and Data Type Design
-
Use
AUTO_columns as surrogate keys, and enforce business semantics in a separate column or a separate unique key.INCREMENT Use a separate column for the following: -
Encoding semantic dimensions (e.
g. , tenant, region, type) -
Multi-tenant or domain-scoped IDs
-
-
Use
BIGINT(orBIGINT UNSIGNED) as the auto increment column data type for high-volume tables.
Semantics and Application Behavior
-
Because
AUTO_values can have gaps due to rollbacks, failed inserts, etc.INCREMENT , the sequence isn't contiguous. Design your application code to accept and accommodate this. -
Avoid using
AUTO_columns for ordering, especially with multi‑threaded ingestion or multi‑region architectures.INCREMENT If “most recent” or “first” semantics are required, order by a DATETIME[(6)]column or another dedicated ordering column, not by theAUTO_value.INCREMENT
Capacity Planning and Monitoring Usage
For high-volume ingest/update tables, plan AUTO_ capacity based on expected ingest rates and identifier consumption over time.
Monitor how quickly the values are consumed and ensure there is sufficient capacity in the identifier space.
Last modified: