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 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 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 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:
-
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.
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: