How to create a self-increment field in SQLite

Source: Internet
Author: User
Tags sqlite

 SQLiteSimple answer: A field declared as an INTEGER PRIMARY KEY is automatically incremented.

Here is the detailed answer: starting with the 2.3.4 version of SQLite, if you declare a field in a table to be an INTEGER PRIMARY KEY, then whenever you insert a null value into that field of the table, the null value is automatically replaced with the most An integer that has a large value of 1, and if the table is empty, it is replaced with 1. For example, suppose you have a data sheet like this:

CREATE TABLE T1 (
A INTEGER PRIMARY KEY,
b INTEGER
);

In this data table, declare

INSERT into T1 VALUES (null,123);

In a logical sense equivalent to:

INSERT into T1 VALUES ((SELECT Max (a) from T1) +1,123);

A new API function Sqlite3_last_insert_rowid () returns the Shaping key for the most recent insert operation

Note that this integer key is always 1 larger than the last key in the previously inserted table. The new key is unique relative to the existing key in the table, but it may overlap with the key values that were previously removed from the table. To always get the key that is unique throughout the table, add the keyword AutoIncrement before the declaration of the integer PRIMARY key. The selected key will always be 1 larger than the largest key already present in the table. If the maximum possible key already exists in the table, the insert operation fails and returns a sqlite_full error code.

How to create a self-increment field in SQLite

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.