SQLite 查询以查找主键 [英] SQLite query to find primary keys

查看:75
本文介绍了SQLite 查询以查找主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 SQLite 中,我可以运行以下查询来获取表中的列列表:

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';

它会给出显示主键的创建表语句.但我正在寻找一种无需解析 create 语句即可执行此操作的方法.

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.

推荐答案

table_info 确实为您提供了一个名为 pk(最后一个)的列,指示它是否是主键(如果是,则它在键中)或不(零).

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).

从文档中澄清:

结果集中的pk"列对于不存在的列为零主键的一部分,是主键中列的索引属于主键的列的键.

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.

这篇关于SQLite 查询以查找主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆