码迷,mamicode.com
首页 > 数据库 > 详细

sqlite中的自增主键

时间:2016-09-23 15:01:39      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

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中的自增主键

标签:

原文地址:http://www.cnblogs.com/chucklu/p/5899878.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!