SQL SQLite:自动增量主键问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8519936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQLite: autoincrement primary key questions
提问by Caner
I have the following SQLite query:
我有以下 SQLite 查询:
CREATE TABLE Logs ( Id integer IDENTITY (1, 1) not null CONSTRAINT PKLogId PRIMARY KEY, ...
IDENTITY (1, 1)
-> What does this mean?PKLogId
what is this? This doesn't seem to be defined anywhere- I want
Id
to beinteger primary key
withautoincrement
. I would like to be able to insert into thisLogs
table omittingId
column in my query. I wantId
to be automatically added and incremented. Is this possible? How can I do this?
IDENTITY (1, 1)
-> 这是什么意思?PKLogId
这是什么?这似乎没有在任何地方定义- 我想
Id
是integer primary key
有autoincrement
。我希望能够插入到这个Logs
表中,而Id
在我的查询中省略列。我想Id
自动添加和递增。这可能吗?我怎样才能做到这一点?
At the moment when I try to insert without Id
I get:
当我尝试在没有Id
我的情况下插入时:
Error while executing query: Logs.Id may not be NULL
回答by Bruno
I'm not sure whether you're actually using SQLite according to the syntax of your example.
根据示例的语法,我不确定您是否真的在使用 SQLite。
If you are, you may be interested in SQLite FAQ #1: How do I create an AUTOINCREMENT field?:
如果您是,您可能对SQLite 常见问题 #1:如何创建 AUTOINCREMENT 字段感兴趣?:
Short answer: A column declared INTEGER PRIMARY KEY will autoincrement.
简短回答:声明为 INTEGER PRIMARY KEY 的列将自动递增。
回答by Micha? Powaga
Change it to:
将其更改为:
CREATE TABLE Logs ( Id integer PRIMARY KEY,....
回答by Doc Maynord
If you are, you may be interested in SQLite FAQ #1: How do I create an AUTOINCREMENT field?:
如果是,您可能对 SQLite 常见问题 #1:如何创建 AUTOINCREMENT 字段感兴趣?:
Short answer: A column declared INTEGER PRIMARY KEY will auto increment.
简短回答:声明为 INTEGER PRIMARY KEY 的列将自动递增。
This in fact is not entirely accurate. An integer primary key will indeed increment, however if the table drops all rows, it starts from the beginning again, It is important if you want to have all associated records tied correctly to use the autoincrement description after the primary key declaration on the integer field.
这实际上并不完全准确。整数主键确实会增加,但是如果表删除所有行,它会再次从头开始,如果您希望在整数字段上的主键声明之后正确绑定所有关联记录以使用自动增量描述,这一点很重要.