简体   繁体   English

sqlite delete命令的结果与启用了限制的结果不一致

[英]Inconsistent result with sqlite delete command with limit enabled

Here are my sqlite db details, 这是我的sqlite db详细信息,

sqlite> .table
url_db
sqlite> .schema url_db
CREATE TABLE url_db(URL TEXT UNIQUE);
sqlite> select * from url_db;
play.googleapis.com
notifications.google.com
contacts.skype.com
edge.skype.com
people.skype.com

I wanted to execute the command delete from url_db limit 1 . 我想执行命令delete from url_db limit 1

So I've downloaded the full source code (sqlite-src-3240000.zip) from the official download page . 因此,我已经从官方下载页面下载了完整的源代码(sqlite-src-3240000.zip)。

Compiled the source code with the option 'SQLITE_ENABLE_UPDATE_DELETE_LIMIT=1' 使用选项“ SQLITE_ENABLE_UPDATE_DELETE_LIMIT = 1”编译源代码

When I execute that command (command executed), sometimes it deletes the random entry but not the first entry. 当我执行该命令(执行命令)时,有时会删除随机条目,但不会删除第一个条目。 I wanted to delete play.googleapis.com , instead that command deleted contacts.skype.com . 我想删除play.googleapis.com ,相反,该命令删除了contacts.skype.com

sqlite> select * from url_db;
play.googleapis.com
notifications.google.com
edge.skype.com
people.skype.com

What's the cause for this behavior? 这是什么原因造成的? I am implementing a FIFO list in which when the entries reach 500, I need to delete the first entry. 我正在实现一个FIFO列表,当条目达到500时,我需要删除第一个条目。

sometimes it deletes the random entry but not the first entry. 有时会删除随机条目,但不会删除第一个条目。

You misunderstand relational databases. 您误解了关系数据库。 A table represents an unordered set. 表格代表无序集。 There is no "first" row in a table, unless you explicitly define the ordering. 除非您明确定义顺序,否则表中没有“第一”行。

Because this problem occurs so often, SQLite has a built-in work-around, the rowid "column". 由于此问题经常发生,因此SQLite具有内置的变通方法,即rowid “ column”。 You can use this as a regular column, resulting in: 您可以将其用作常规列,从而得到:

delete from url_db
    order by rowid
    limit 1;

Personally, I prefer an explicitly declared auto-increment column, but SQLite builds in this functions (unlike other databases). 就个人而言,我更喜欢显式声明的自动增量列,但是SQLite内置于此函数中(与其他数据库不同)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM