Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to create a self-increasing field in SQLite
How to create a self-increasing field in SQLite
Starting from version 2.3.4 of SQLite, if you declare a field in the table as an integer primary key, then every time you insert a null value in this field of the table, this null value will be automatically replaced by an integer that is 1 larger than the maximum value of all rows in this field in the table; If the table is empty, replace it with 1 For example, suppose you have a data table like this:

Create table t 1 (

Integer primary key,

B integer

);

It is pointed out in the data sheet

Insert t 1 value (NULL,123);

Logically equivalent to:

Insert the value of t 1 (choose max(a)+ 1,123 from t 1);

A new API function sqlite3_last_insert_rowid () returns the plastic key of the latest insert operation.

Note that this integer key is always larger than the last key inserted in the table before 1. The new key is unique relative to the existing key in the table, but it may overlap with the key value previously deleted from the table. To always get a unique key in the whole table, add the keyword AUTOINCREMENT before declaring the integer primary key. In this way, the selected key will always be larger than the existing largest key in the table 1. If the largest possible key already exists in the table, the insert operation will fail and an SQLITE_FULL error code will be returned.