SQLite increment primary key (Autoincrement)

Translation: https: //www.sqlite.org/autoinc.html
 
Overview
1 increment primary key (TheAUTOINCREMENT keyword) take up additional CPU, memory, disk space and increase disk I / O overhead (disk I / O overhead), so if not necessary, it should be disabled. Under normal circumstances it is not necessary.
2 SQLite, specify where a property is INTEGERPRIMARY KEY, and designated as the ROWID, the effect is the same (an alias for the ROWID), ( except when creating the table, specified WITHOUT_ROWID), the storage type 64-bit symbol integer.
3 in the insertion operation, or if the ROWID INTEGERPRIMARY KEY column value is not specified, it will automatically fill SQLITE an unused integer, typically greater than any ROWID value have been used. Regardless of whether the AUTOINCREMENT keyword has been specified, the effect is still the same.
4 If INTEGEPRIMARY KEY AUTOINCREMENT keyword followed by a keyword, will change the allocation algorithm ROWID no longer use ROWID values have been used, in other words, to prevent AUTOINCREMENT ROWID reuse, although it has been with a row ROWID deletion of data, while not being used.
 
BACKGROUND
For SQLite, each row of each table there is a 64-bit signed integer ROWID, the ROWID uniquely expressed in the table (table WITHOUT_ROWID specified exceptions)
 
you can special column names (e.g. ROWID, _rowid, or) SQLITE value ROWID access table OID. Unless you define a column name with the same name in the above table, if it is defined, the name will only apply to a defined columns instead of the built ROWID.
 
If you have a table defined as INTEGER PRIMARY KEY attribute columns, and will produce the same effect as ROWID (an alias for), I want to translate into an alias, but perhaps no one understands. This time, can be accessed through four different ways ROWID, before three ways to refer to the previous paragraph, the last one way is to specify the nature of that column INTEGER PRIMARY KEY attributes! ! They are only aliases.
 
When a new row is inserted into SQLITE table, ROWID can be specified in the insert statement, or let the database engine automatically assigned. Manually example specifies ROWID follows:
the CREATE TABLE test1 (A the INT, B the TEXT);
the INSERT the INTO test1 (ROWID, A, B) the VALUES (123,. 5, 'Hello');
 
if the insert statement is not specified ROWID, or ROWID specifies a NULL value, the database engine will automatically set specify an appropriate ROWID value. Universal algorithm will create a new ROWID, this value is greater than any previously assigned ROWID value, reflecting the growth of the situation. If the table is empty, ROWID will be designated as the 1.If the largest ROWID is equal to the largest possibleinteger (9223372036854775807) then the database engine starts picking positivecandidate ROWIDs at random until it finds one that is not previously used.
If you do not find a ROWID values have not used, the insertion operation will end in failure, and returns an error code SQLITE_FULL! If ROWID not specified in the insert statement displayed, automatically generated ROWID value is always larger than zero.
 
ROWID normal selection algorithm would generate monotonically increasing and the only ROWID, as long as you do not use the maximum value of ROWID, and never delete the largest ROWID data columns. If you delete some rows, or you have to create one of the largest ROWID value, before it is deleted ROWID line used, it may be reused newly inserted row.
 
Increment primary key
if a column specifies the attribute INTEGER PRIMARY KEYAUTOINCREMENT properties, ROWID slightly different selection algorithms. ROWID produced are larger than the existing records in the table of ROWID. If before the table is empty, then the value is allocated ROWID 1. If the user specifies ROWID value is the maximum, then it will not allow any of insertion, at the same time, the insertion operation will return any error codes SQLITE_FULL . Only ROWID values frompreviously Transactions that were committed are Considered. ROWID values thatwere rolled the Back are ignored and CAN BE the Reused.
 
SQLite internally maintains a name "sqlite_sequence" table, records the largest ROWID a table already in use. Regardless of a table there is no specified property value AUTOINCREMENT column, the table will be automatically created and initialized. The table can also be edited using ordinary UPDATE, INSERT, and DELETE statements. But modifying the table will affect the AUTOINCREMENT generation algorithm. When you change the time, make sure you know what you're doing! !
 
And to achieve the specified keyword AUTOINCREMENT default implementation is different. For AUTOINCREMENT, ROWID value obtained for each column of the same table the same database, it is never used. Generated ROWID monotonically increasing. For some applications it is very important attributes. But if your application does not require these attributes, you can just use the default mode, instead of specifying AUTOINCREMENT. After all the AUTOINCREMENT during insertion need to do some extra work, resulting in the insertion of a slower slow.
 
Please note that does not mean ROWID always monotonically increasing plus 1 (increases by exactly one). Gain 1 is the most common. However, if it fails in the insertion process will not be assigned the next ROWID inserted into the data reuse, resulting in a gap in the sequence ROWID. AUTOINCREMENT ensures that the ROWID is automatically incremented, but there is no guarantee continuous increasing.
 
Because AUTOINCREMENT keyword changed the ROWID selection algorithm, it can not be applied in AUTOINCREMENT WITHOUT_ROWID specified table or specify the other as INTEGER PRIMARYKEY. Any attempt to apply the AUTOINCREMENT keyword in the above two cases will result in an error.
Note:
1) specifies WITHOUT_ROWID property, you can not use AUTOINCREMENT
2) If you already have a list of specified INTEGER PRIMARY KEY attribute, AUTOINCREMENT can not modify any other column of the table.

The following are reading:
Excerpt from: http: //www.jb51.net/article/50049.htm
for use in user-defined internal self-increment primary keys, or system maintenance ROWID, has been described above is very clear, and also links It has been provided a method of operating ROWID.
Now focus on: in large-scale distributed applications, auto-increment primary key, of course, SQLite data can only be a sparrow, for us, there is no concept of any of the large amount of data, for the author mentioned a It uses the concept of scale as well as two tables, have a profound curiosity, but currently do not have any operating environment.
Excerpt from the article: Use self-growth field as the primary key, there are many problems, such as maintenance or solve the conflict in the primary key applications such as large-scale distribution.
In some large-scale distributed applications, the primary key is generally used guid, which can effectively prevent primary key conflicts, reduce the primary key maintenance projects.
Of course, for the benefit of small and medium sized applications, from the fields grow some more, simple and fast.
    Reasons increment primary keys, and under what circumstances, need to create a primary key, especially in distributed database! !


Guess you like

Origin blog.51cto.com/fengyuzaitu/2429474