SQLite query to find primary keys SQLite query to find primary keys sqlite sqlite

SQLite query to find primary keys


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.


Hopefully this helps someone:After some research and pain the command that worked for me to find the primary key column name was:

SELECT l.name FROM pragma_table_info("Table_Name") as l WHERE l.pk = 1;