
Until MariaDB 10.2.3, InnoDB used an auto-increment counter that is stored in memory. ALTER TABLE animals AUTO_INCREMENT = 8 INSERT INTO animals ( name ) VALUES ( 'aardvark' ) SELECT * FROM animals + -+-+ | id | name | + -+-+ | 1 | dog | | 2 | cat | | 3 | penguin | | 4 | fox | | 5 | whale | | 6 | ostrich | | 8 | aardvark | + -+-+ SET insert_id = 12 INSERT INTO animals ( name ) VALUES ( 'gorilla' ) SELECT * FROM animals + -+-+ | id | name | + -+-+ | 1 | dog | | 2 | cat | | 3 | penguin | | 4 | fox | | 5 | whale | | 6 | ostrich | | 8 | aardvark | | 12 | gorilla | + -+-+ InnoDB LAST_INSERT_ID() can be used to see the last AUTO_INCREMENT value inserted by the current session.

You can use an ALTER TABLE statement to assign a new value to the auto_increment table option, or set the insert_id server system variable to change the next AUTO_INCREMENT value inserted by the current session. row *************************** Table : t Create Table : CREATE TABLE ` t ` ( ` id ` bigint ( 20 ) unsigned NOT NULL AUTO_INCREMENT, ` c ` char ( 1 ) DEFAULT NULL, UNIQUE KEY ` id ` ( ` id ` ) ) ENGINE = InnoDB DEFAULT CHARSET = latin1 Setting or Changing the Auto_Increment Value
SQLITE AUTOINCREMENT ONLY UNIQUE SERIAL
SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE. CREATE TABLE animals ( id MEDIUMINT NOT NULL AUTO_INCREMENT, name CHAR ( 30 ) NOT NULL, PRIMARY KEY ( id ) ) INSERT INTO animals ( name ) VALUES ( 'dog' ),( 'cat' ),( 'penguin' ), ( 'fox' ),( 'whale' ),( 'ostrich' ) SELECT * FROM animals + -+-+ | id | name | + -+-+ | 1 | dog | | 2 | cat | | 3 | penguin | | 4 | fox | | 5 | whale | | 6 | ostrich | + -+-+ Storage engines that permit the column to be placed elsewhere are Aria, MyISAM, MERGE, Spider, TokuDB, BLACKHOLE, FederatedX and Federated. In some storage engines (including the default InnoDB), if the key consists of multiple columns, the AUTO_INCREMENT column must be the first column. It must defined as a key (not necessarily the PRIMARY KEY or UNIQUE key). The automatically generated value can never be lower than 0.Įach table can have only one AUTO_INCREMENT column. This also applies to 0, unless the NO_AUTO_VALUE_ON_ZERO SQL_MODE is enabled.ĪUTO_INCREMENT columns start from 1 by default. When you insert a new record to the table (or upon adding an AUTO_INCREMENT attribute with the ALTER TABLE statement), and the auto_increment field is NULL or DEFAULT (in the case of an INSERT), the value will automatically be incremented. The AUTO_INCREMENT attribute can be used to generate a unique identity for new rows. Generating Auto_Increment Values When Adding the Attribute.CHECK Constraints, DEFAULT Values and Virtual Columns.Setting or Changing the Auto_Increment Value.For this to happen, declare the Id column as either ROWID or INTEGER PRIMARY KEY. when working normally with the database, you can ommit the Id in INSERTs and SQLite will generate one for you.your original IDs will be preserved when the database is being rebuilt by using INSERT commands and explicitly specifying the Id!.that you don't need to specify AUTOINCREMENT.It also means that the Id is only generated automatically if you don't supply one at INSERT. It only determines whether IDs of deleted rows will be reused or not. This means that AUTOINCREMENT is not required for rows to be auto-incremented. In other words, the purpose of AUTOINCREMENT is to prevent the reuse of ROWIDs from previously deleted rows. If the AUTOINCREMENT keyword appears after INTEGER PRIMARY KEY, that changes the automatic ROWID assignment algorithm to prevent the reuse of ROWIDs over the lifetime of the database. This is true regardless of whether or not the AUTOINCREMENT keyword is used. On an INSERT, if the ROWID or INTEGER PRIMARY KEY column is not explicitly given a value, then it will be filled automatically with an unused integer, usually one more than the largest ROWID currently in use.

The documentation for SQLite Autoincrement says:
