Unless your table is WITHOUT ROWID
, the primary key in your table is just a separate unique index.
So you just have to create your own index after the table is created.
Is an index needed for a primary key in SQLite?
Look at the sqlite3 example - sqlite just creates a separate sqlite_autoindex_t_with_pk_1
for you along with manually created indices.
sqlite> create table t_with_pk(id1 integer not null, id2 integer not null, primary key(id1, id2 desc));
sqlite> create table t_no_pk(id1 integer not null, id2 integer not null);
sqlite> create unique index t_uniq_idx on t_no_pk(id1, id2 desc);
sqlite> select * from sqlite_master;
table|t_with_pk|t_with_pk|2|CREATE TABLE t_with_pk(id1 integer not null, id2 integer not null, primary key(id1, id2 desc))
index|sqlite_autoindex_t_with_pk_1|t_with_pk|3|
table|t_no_pk|t_no_pk|4|CREATE TABLE t_no_pk(id1 integer not null, id2 integer not null)
index|t_uniq_idx|t_no_pk|5|CREATE UNIQUE INDEX t_uniq_idx on t_no_pk(id1, id2 desc)
The behavior is also identical
sqlite> explain query plan select * from t_with_pk order by id1, id2 desc;
QUERY PLAN
`--SCAN TABLE t_with_pk USING COVERING INDEX sqlite_autoindex_t_with_pk_1
sqlite> explain query plan select * from t_no_pk order by id1, id2 desc;
QUERY PLAN
`--SCAN TABLE t_no_pk USING COVERING INDEX t_uniq_idx
Note also (regarding @Developer's answer) that just inverting the order in a SELECT
statements requires sqlite to create a temp index
sqlite> explain query plan select * from t_no_pk order by id1, id2;
QUERY PLAN
|--SCAN TABLE t_no_pk USING COVERING INDEX t_uniq_idx
`--USE TEMP B-TREE FOR RIGHT PART OF ORDER BY