SQLite 插入顺序与查询顺序?

发布于 2024-11-01 11:23:49 字数 135 浏览 0 评论 0原文

查询返回的行顺序是否与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

债姬 2024-11-08 11:23:49

鉴于表中的行数较少,这可能就是您所需要的:

SELECT * FROM yourtable ORDER BY ROWID

有关 ROWID 的更多信息,请参阅以下两个链接:
SQLite 自动增量ROWID和整数主键

Given the small number of rows in your table, this is probably what you need:

SELECT * FROM yourtable ORDER BY ROWID

For more information on ROWID, see these two links:
SQLite Autoincrement and ROWIDs and the INTEGER PRIMARY KEY

不爱素颜 2024-11-08 11:23:49

即使顺序在一种情况下可能是一致的,但也不能保证。

这就是 SQL 具有 ORDER BY 运算符的原因:

SELECT foo,bar FROM Table FOO WHERE frobnitz LIKE 'foo%' ORDER BY baz ASC;

Even if the order may be consistent in one scenario, there is afaik no guarantee.

That is why SQL has the ORDER BY operator:

SELECT foo,bar FROM Table FOO WHERE frobnitz LIKE 'foo%' ORDER BY baz ASC;
落墨 2024-11-08 11:23:49

a 返回的行的顺序
查询将与中的顺序相同
行被插入到
SQLite 数据库的表?

不,你不能指望这一点。所有查询优化器在加速查询方面都有很大的自由度。他们可以自由做的一件事是以最快的顺序返回行。即使特定的 dbms 支持聚集索引也是如此。 (聚集索引对行施加物理排序。)

只有一种方法可以保证 SQL 数据库中返回行的顺序:使用 ORDER BY 子句。

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?

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文