SQLite 插入顺序与查询顺序?
查询返回的行顺序是否与 SQLite 数据库表中行插入的顺序相同?
如果是,这种行为是否一致?
如果否,可以强制执行吗?
我需要存储大约 500 行数据,并且需要不时进行排序/排序。插入之前,数据的顺序正确。
Will the order of rows returned by a query will be the same as the order in which the rows were inserted into the table, of SQLite database?
If Yes, Is this behaviour consistent?
If No, Can this be enforced?
I have a requirement of storing approx 500 rows of data, and which requires sorting/ordering from time to time. The data is in proper order, before the insertion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
鉴于表中的行数较少,这可能就是您所需要的:
有关 ROWID 的更多信息,请参阅以下两个链接:
SQLite 自动增量 和 ROWID和整数主键
Given the small number of rows in your table, this is probably what you need:
For more information on ROWID, see these two links:
SQLite Autoincrement and ROWIDs and the INTEGER PRIMARY KEY
即使顺序在一种情况下可能是一致的,但也不能保证。
这就是 SQL 具有 ORDER BY 运算符的原因:
Even if the order may be consistent in one scenario, there is afaik no guarantee.
That is why SQL has the
ORDER BY
operator:不,你不能指望这一点。所有查询优化器在加速查询方面都有很大的自由度。他们可以自由做的一件事是以最快的顺序返回行。即使特定的 dbms 支持聚集索引也是如此。 (聚集索引对行施加物理排序。)
只有一种方法可以保证 SQL 数据库中返回行的顺序:使用
ORDER BY
子句。No, you can't count on that. All query optimizers have a lot of freedom when it comes to speeding up queries. One thing they're free to do is to return rows in whatever order is the fastest. That's true even if a particular dbms supports clustered indexes. (A clustered index imposes a physical ordering on the rows.)
There's only one way to guarantee the order of returned rows in a SQL database: use an
ORDER BY
clause.