The AUTOINCREMENT keyword imposes extra CPU, memory, disk space, and disk I/O overhead and should be avoided if not strictly needed. It is usually not needed.
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. This is true regardless of whether or not the AUTOINCREMENT keyword is used.
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. In other words, the purpose of AUTOINCREMENT is to prevent the reuse of ROWIDs from previously deleted rows.
R-23612-42210:[You can access the ROWID of an SQLite table using one of the special column names ROWID, _ROWID_, or OID. Except if you declare an ordinary table column to use one of those special names, then the use of that name will refer to the declared column not to the internal ROWID. ]
R-38485-20010:[If a table contains a column of type INTEGER PRIMARY KEY, then that column becomes an alias for the ROWID. ] R-05038-25064:[You can then access the ROWID using any of four different names, the original three names described above or the name given to the INTEGER PRIMARY KEY column. ] R-37283-61388:[All these names are aliases for one another and work equally well in any context. ]
R-54260-17937:[When a new row is inserted into an SQLite table, the ROWID can either be specified as part of the INSERT statement or it can be assigned automatically by the database engine. ] R-51090-01319:[To specify a ROWID manually, just include it in the list of values to be inserted. For example:
CREATE TABLE test1(a INT, b TEXT); INSERT INTO test1(rowid, a, b) VALUES(123, 5, 'hello');
R-12104-35971:[If no ROWID is specified on the insert, or if the specified ROWID has a value of NULL, then an appropriate ROWID is created automatically. ] R-29538-34987:[The usual algorithm is to give the newly created row a ROWID that is one larger than the largest ROWID in the table prior to the insert. ] R-61914-48074:[If the table is initially empty, then a ROWID of 1 is used. ] R-07677-41881:[If the largest ROWID is equal to the largest possible integer (9223372036854775807) then the database engine starts picking positive candidate ROWIDs at random until it finds one that is not previously used. ] R-38219-53002:[If no unused ROWID can be found after a reasonable number of attempts, the insert operation fails with an SQLITE_FULL error. ] R-40812-03570:[If no negative ROWID values are inserted explicitly, then automatically generated ROWID values will always be greater than zero. ]
R-60470-29837:[The normal ROWID selection algorithm described above will generate monotonically increasing unique ROWIDs as long as you never use the maximum ROWID value and you never delete the entry in the table with the largest ROWID. ] R-03793-19254:[If you ever delete rows or if you ever create a row with the maximum possible ROWID, then ROWIDs from previously deleted rows might be reused when creating new rows and newly created ROWIDs might not be in strictly ascending order. ]
R-43328-56724:[If a column has the type INTEGER PRIMARY KEY AUTOINCREMENT then a slightly different ROWID selection algorithm is used. ] R-18968-56672:[The ROWID chosen for the new row is at least one larger than the largest ROWID that has ever before existed in that same table. ] R-15283-49521:[If the table has never before contained any data, then a ROWID of 1 is used. ] R-17817-00630:[If the largest possible ROWID has previously been inserted, then new INSERTs are not allowed and any attempt to insert a new row will fail with an SQLITE_FULL error. ] R-17684-03275:[Only ROWID values from previous transactions that were committed are considered. ROWID values that were rolled back are ignored and can be reused. ]
R-09328-38790:[SQLite keeps track of the largest ROWID using an internal table named "sqlite_sequence". ] R-43700-31930:[The sqlite_sequence table is created and initialized automatically whenever a normal table that contains an AUTOINCREMENT column is created. ] R-20486-33667:[The content of the sqlite_sequence table can be modified using ordinary UPDATE, INSERT, and DELETE statements. ] R-26490-64257:[But making modifications to this table will likely perturb the AUTOINCREMENT key generation algorithm. ] Make sure you know what you are doing before you undertake such changes. The sqlite_sequence table does not track ROWID changes associated with UPDATE statement, only INSERT statements.
The behavior implemented by the AUTOINCREMENT keyword is subtly different from the default behavior. R-57773-31134:[With AUTOINCREMENT, rows with automatically selected ROWIDs are guaranteed to have ROWIDs that have never been used before by the same table in the same database. ] R-47208-15314:[And the automatically generated ROWIDs are guaranteed to be monotonically increasing. ] These are important properties in certain applications. But if your application does not need these properties, you should probably stay with the default behavior since the use of AUTOINCREMENT requires additional work to be done as each row is inserted and thus causes INSERTs to run a little slower.
R-56805-28429:[Note that "monotonically increasing" does not imply that the ROWID always increases by exactly one. ] R-50658-17415:[One is the usual increment. ] R-15078-25200:[However, if an insert fails due to (for example) a uniqueness constraint, the ROWID of the failed insertion attempt might not be reused on subsequent inserts, resulting in gaps in the ROWID sequence. ] R-24433-42760:[AUTOINCREMENT guarantees that automatically chosen ROWIDs will be increasing but not that they will be sequential. ]
Because AUTOINCREMENT keyword changes the behavior of the ROWID selection algorithm, AUTOINCREMENT is not allowed on WITHOUT ROWID tables or on any table column other than INTEGER PRIMARY KEY. R-46912-28798:[Any attempt to use AUTOINCREMENT on a WITHOUT ROWID table or on a column other than the INTEGER PRIMARY KEY column results in an error. ]