SQLite中有自动增量吗?
我正在尝试在 Sqlite3主键的表一个>。我不确定这是否真的可能,但我希望只需要指定其他字段。
例如:
CREATE TABLE people (
id integer primary key auto increment,
first_name varchar(20),
last_name varchar(20)
);
然后,当我添加一个值时,我希望只需要做:
INSERT INTO people VALUES ("John", "Smith");
这可能吗?
我在 Windows 7 中的 cygwin
下运行 sqlite3
。
I am trying to create a table with an auto-incrementing primary key
in Sqlite3. I am not sure if this is really possible, but I am hoping to only have to designate the other fields.
For example:
CREATE TABLE people (
id integer primary key auto increment,
first_name varchar(20),
last_name varchar(20)
);
Then, when I add a value, I was hoping to only have to do:
INSERT INTO people VALUES ("John", "Smith");
Is this even possible?
I am running sqlite3
under cygwin
in Windows 7.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您可以免费获得一个,称为 ROWID。无论您是否要求,每个 SQLite 表中都存在这一点。
如果包含 INTEGER PRIMARY KEY 类型的列,则该列指向自动 ROWID 列(是其别名)。
正如您所期望的,每当您插入一行时,ROWID(无论您如何称呼它)都会被分配一个值。如果您在 INSERT 上显式分配非 NULL 值,它将获取指定值而不是自动增量。如果在 INSERT 上显式指定 NULL 值,它将获得下一个自动递增值。
另外,您应该尽量避免:
并
改为使用。第一个版本非常脆弱 - 如果您在表定义中添加、移动或删除列,则 INSERT 将失败或生成不正确的数据(值位于错误的列中)。
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:
and use
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).
是的,这是可能的。根据 SQLite 常见问题解答:
然后,您必须在插入时在该列上传递
NULL
。示例:输出:
在 Ubuntu 23.04、sqlite 3.40.1 上测试。
Yes, this is possible. According to the SQLite FAQ:
You then have to pass
NULL
on that column when inserting. Example:outputs:
Tested on Ubuntu 23.04, sqlite 3.40.1.
截至今天 - 2018 年 6 月,
以下是 官方 SQLite 文档 关于该主题的内容(粗体和;斜体是我的):
As of today — June 2018
Here is what official SQLite documentation has to say on the subject (bold & italic are mine):
SQLite AUTOINCRMENT 是一个关键字,用于自动递增表中字段的值。我们可以在创建具有特定列名的表时使用 AUTOINCRMENT 关键字自动递增字段值,以自动递增字段值。
关键字 AUTOINCRMENT 只能与 INTEGER 字段一起使用。
语法:
AUTOINCREMENT 关键字的基本用法如下:
示例如下:
考虑按如下方式创建 COMPANY 表:
现在,将以下记录插入表 TB_COMPANY_INFO:
现在选择记录
SQLite AUTOINCREMENT is a keyword used for auto incrementing a value of a field in the table. We can auto increment a field value by using AUTOINCREMENT keyword when creating a table with specific column name to auto incrementing it.
The keyword AUTOINCREMENT can be used with INTEGER field only.
Syntax:
The basic usage of AUTOINCREMENT keyword is as follows:
For Example See Below:
Consider COMPANY table to be created as follows:
Now, insert following records into table TB_COMPANY_INFO:
Now Select the record
你读过这篇文章吗? 如何创建自动增量字段。
始终插入 NULL 作为 id。
Have you read this? How do I create an AUTOINCREMENT field.
Always insert NULL as the id.
不应在
PRIMARY KEY
附近指定AUTOINCRMENT
关键字。创建自动增量主键并插入的示例:
将给出:
One should not specify
AUTOINCREMENT
keyword nearPRIMARY KEY
.Example of creating autoincrement primary key and inserting:
will give:
除了 rowid 之外,您还可以定义自己的自增字段,但不建议这样做。当我们使用自动增加的 rowid 时,这总是一个更好的解决方案。
请阅读此处了解详细信息。
Beside rowid, you can define your own auto increment field but it is not recommended. It is always be better solution when we use rowid that is automatically increased.
Read here for detailed information.
你所做的是正确的,但是“自动增量”的正确语法应该没有空格:(
另请注意,我将你的 varchar 更改为字符串。那是因为 SQLite 在内部将 varchar 转换为字符串,所以为什么要麻烦呢?)
然后你的insert 应该在 SQL 语言中尽可能标准:
虽然确实,如果您省略 id,它会自动递增和分配,但我个人不喜欢依赖将来可能会改变的自动机制。
关于自动增量的注释:虽然,正如许多人指出的那样,SQLite 人员不推荐它,但我不喜欢在不使用自动增量的情况下自动重用已删除记录的 id。换句话说,我希望已删除记录的 ID 永远不会再次出现。
华泰
What you do is correct, but the correct syntax for 'auto increment' should be without space:
(Please also note that I changed your varchars to strings. That's because SQLite internally transforms a varchar into a string, so why bother?)
then your insert should be, in SQL language as standard as possible:
while it is true that if you omit id it will automatically incremented and assigned, I personally prefer not to rely on automatic mechanisms which could change in the future.
A note on autoincrement: although, as many pointed out, it is not recommended by SQLite people, I do not like the automatic reuse of ids of deleted records if autoincrement is not used. In other words, I like that the id of a deleted record will never, ever appear again.
HTH
我知道这个答案有点晚了。
我这个答案的目的是供大家参考,如果他们现在或将来在 SQLite 上遇到这种类型的挑战并且遇到困难时。
现在,回头看看你的查询,它应该是这样的。
它对我有效。像这样,
如果您正在使用 SQLite,我建议您查看 数据库浏览器对于 SQLite。也适用于不同的平台。
I know this answer is a bit late.
My purpose for this answer is for everyone's reference should they encounter this type of challenge with SQLite now or in the future and they're having a hard time with it.
Now, looking back at your query, it should be something like this.
It works on my end. Like so,
Just in case you are working with SQLite, I suggest for you to check out DB Browser for SQLite. Works on different platforms as well.
在 SQLite 中,您可以使用或不使用 AUTOINCRMENT 进行自动增量,如下所示。 *
AUTOINCRMENT
必须与 INTEGER PRIMARY KEY 一起使用,否则会出现是错误的:并且,
AUTOINCRMENT
可以避免重用已删除的数字,除非明确指定它们,而没有AUTOINCRMENT
则不能根据文档。 *我的问题和我的回答解释AUTOINCRMENT
和没有AUTOINCRMENT
之间的区别。In SQLite, you can auto-increment with and without AUTOINCREMENT as shown below. *
AUTOINCREMENT
must be used with INTEGER PRIMARY KEY otherwise there is error:And,
AUTOINCREMENT
can avoid to reuse the deleted numbers unless explicitly specifying them while noAUTOINCREMENT
cannot according to the doc. *My question and my answer explain the difference betweenAUTOINCREMENT
and noAUTOINCREMENT
.在SQLite中,您可以在定义表时使用INTEGER PRIMARY KEY AUTOINCRMENT关键字创建自动递增主键。
示例:
如果没有“AUTOINCRMENT”,SQLite仍会自动递增INTEGER PRIMARY KEY,但它可能会重用已删除行的 ID,这可能会导致问题。
In SQLite, you can create an auto-incrementing primary key by using the INTEGER PRIMARY KEY AUTOINCREMENT keyword when defining a table.
Example:
Without "AUTOINCREMENT", SQLite will still increment the INTEGER PRIMARY KEY automatically, but it could reuse IDs of deleted rows which can cause problems.