sqlite中的自增主键

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sqlite中的自增主键相关的知识,希望对你有一定的参考价值。

http://stackoverflow.com/questions/8519936/sqlite-autoincrement-primary-key-questions

I‘m not sure whether you‘re actually using SQLite according to the syntax of your example.

If you are, you may be interested in SQLite FAQ #1: How do I create an AUTOINCREMENT field?:

Short answer: A column declared INTEGER PRIMARY KEY will autoincrement.

 

http://stackoverflow.com/questions/7905859/is-there-an-auto-increment-in-sqlite

You get one for free, called ROWID. This is in every SQLite table whether you ask for it or not.

If you include a column of type INTEGER PRIMARY KEY, that column points at (is an alias for) the automatic ROWID column.

ROWID (by whatever name you call it) is assigned a value whenever you INSERT a row, as you would expect. If you explicitly assign a non-NULL value on INSERT, it will get that specified value instead of the auto-increment. If you explicitly assign a value of NULL on INSERT, it will get the next auto-increment value.

Also, you should try to avoid:

 INSERT INTO people VALUES ("John", "Smith");

and use

 INSERT INTO people (first_name, last_name) VALUES ("John", "Smith");

instead. The first version is very fragile — if you ever add, move, or delete columns in your table definition the INSERT will either fail or produce incorrect data (with the values in the wrong columns).

 

https://www.sqlite.org/autoinc.html

 

以上是关于sqlite中的自增主键的主要内容,如果未能解决你的问题,请参考以下文章

java面试一日一题:mysql中的自增主键

Oracle数据库中的自增主键

mybatis批量插入,怎么返回生成的自增主键

mybatis批量插入,怎么返回生成的自增主键

关于 mysql 事务中的自增 id 的疑问

MySQL中的自增主键 ID自增到最大,使用完id会发生什么,怎么办?