SQLite query to find primary keys

2019-04-22 17:59发布

In SQLite I can run the following query to get a list of columns in a table:

PRAGMA table_info(myTable)

This gives me the columns but no information about what the primary keys may be. Additionally, I can run the following two queries for finding indexes and foreign keys:

PRAGMA index_list(myTable)
PRAGMA foreign_key_list(myTable)

But I cannot seem to figure out how to view the primary keys. Does anyone know how I can go about doing this?

Note: I also know that I can do:

select * from sqlite_master where type = 'table' and name ='myTable';

And it will give the the create table statement which shows the primary keys. But I am looking for a way to do this without parsing the create statement.

1条回答
狗以群分
2楼-- · 2019-04-22 18:27

The table_info DOES give you a column named pk (last one) indicating if it is a primary key (if so the index of it in the key) or not (zero).

To clarify, from the documentation:

The "pk" column in the result set is zero for columns that are not part of the primary key, and is the index of the column in the primary key for columns that are part of the primary key.

查看更多
登录 后发表回答