SQLite SELECT default order with PRIMARY KEY ASC

前端 未结 2 1697
日久生厌
日久生厌 2021-01-13 05:57

I\'ve created a SQLite table using:

CREATE TABLE T1 (
  CN INTEGER PRIMARY KEY ASC,
  Name TEXT
);

If I do:

SELECT * FROM T         


        
相关标签:
2条回答
  • 2021-01-13 06:46

    The second question might be useful to others.

    From the SQLite documentation:

    Except for WITHOUT ROWID tables, all rows within SQLite tables have a 64-bit signed integer key that uniquely identifies the row within its table. This integer is usually called the "rowid".
    ... if a rowid table has a primary key that consists of a single column and the declared type of that column is "INTEGER" in any mixture of upper and lower case, then the column becomes an alias for the rowid.

    This also holds for columns that are declared of type "INTEGER PRIMARY KEY ASC", so in your table CN is an alias for "rowid"

    Further information can be found here: http://www.sqlite.org/lang_createtable.html#rowid

    0 讨论(0)
  • 2021-01-13 06:54

    There is no such thing as a default order, if you need your results ordered add an explicit order by clause.

    The dbms is simply optimised to look for the best way to quickly get the required data based on the query. In this case it's the primary key on CN, but that's only because your example is so simple. Never ever rely on the dbms choosing the order you want.

    0 讨论(0)
提交回复
热议问题