Sqlite   发布时间:2022-05-22  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了SQLite Autoincrement大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

sqlite autoincrement

In sqlite,every row of every table has an 64-bit signed Integer ROWID. The ROWID for each row is unique among all rows in the same table.

You can access the ROWID of an sqlite table using one 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.

If a table contains a column of type INTEGER PRIMARY KEY,then that column becomes an alias for the ROWID. 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. All these names are aliases for one another and work equally well in any context.

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. To specify a ROWID manually,just include it in the List of values to be inserted. For example:

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. 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. If the table is initially empty,then a ROWID of 1 is used. 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. If no unused ROWID can be found after a reasonable number of attempts,the insert operation fails with an @R_897_5607@FULL error. If no negative ROWID values are inserted explicitly,then automatically generated ROWID values will always be greater than zero.

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. 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.

The autoINCREMENT Keyword

If a column has the type INTEGER PRIMARY KEY autoINCREMENT then a slightly different ROWID SELEction algorithm is used. The ROWID chosen for the new row is @R_197_5410@t one larger than the largest ROWID that has ever before existed in that same table. If the table has never before contained any data,then a ROWID of 1 is used. If the table has prevIoUsly Held a row with the largest possible ROWID,then new INSERTs are not allowed and any attempt to insert a new row will fail with an @R_897_5607@FulL error.

sqlite keeps track of the largest ROWID that a table has ever Held using the special sqlite_sequence table. The sqlite_sequence table is created and initialized automatically whenever a normal table that contains an autoINCREMENT column is created. The content of the sqlite_sequence table can be modifIEd using ordinary updatE,INSERT,and deletE statements. 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 behavior implemented by the autoINCREMENT keyword is subtly different from the default behavior. 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. 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.

Note that "monotonically increasing" does not imply that the ROWID always increases by exactly one. One is the usual increment. 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. autoINCREMENT guarantees that automatically chosen ROWIDs will be increasing but not that they will be sequential.

大佬总结

以上是大佬教程为你收集整理的SQLite Autoincrement全部内容,希望文章能够帮你解决SQLite Autoincrement所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: