有没有办法按顺序对 sqlite PRAGMA foreign_key_list() 查询的结果进行排序,table 查询中的列出现?

Is there a way to order the result of a sqlite PRAGMA foreign_key_list() query by the order, the columns in the table the query is taken on, appear?

如果行数超过设置的限制大小,我想以编程方式向 sqlite table 添加索引。我想将索引添加到所有具有外键的列,为了得到这些,我使用

PRAGMA foreign_key_list(myTable)

我使用 from 列从我的 table 中获取具有外键的列。 由于具有多个列的索引的顺序很重要,我想按照它们在 table 中出现的顺序对它们进行排序。有没有办法在这个 PRAGMA 查询中找到订单(以及如何),或者我是否必须进行另一个查询 (PRAGMA table_info(myTable)) 并以编程方式找到订单?

编辑:使用 .db3 文件。

经过有限的测试,如果外键是在列级别定义的,那么 id 列是较早定义的外键的更高值.

您可以使用 return 导致 SELECT 语句的 pragma,例如并根据其中一列排序(如果使用了合适的列命名约定,则使用了 id,您可以排序根据from栏目(注意需要括起来))

SELECT * FROM pragma_foreign_key_list('mytable') ORDER BY id ASC;

例如考虑以下内容:-

DROP TABLE IF EXISTS mytable;


CREATE TABLE IF NOT EXISTS mytable (id INTEGER PRIMARY KEY,
  _otherd_building_id_reference INTEGER REFERENCES _building(_id),
    _building_id_reference INTEGER REFERENCES _building(_id),
    _room_id_reference INTEGER REFERENCES _room(_id),
    _campus_id_reference INTEGER REFERENCES _campus(_id),
    _othera_campus_reference INTEGER REFERENCES _campus(_id),
    _otherz_room_id_reference INTEGER REFERENCES _room(_id),
    _otherc_building_id_reference INTEGER REFERENCES _building(_id)
    );


SELECT * FROM pragma_foreign_key_list('mytable') ORDER BY id DESC;
  • 请注意,最初添加的列没有像 *other? 这样的前缀,othera ,c 和 z 然后在最后添加,然后最后添加 _otherd_building_id_reference.

结果,当运行以上一致时:-