SQLite 列出数据库中的所有外键
有没有办法列出 SQLite 数据库中的所有外键?
它们似乎没有存储在 sqlite_master 中,并且 PRAGMAforeign_key_list('table')
一次只列出一个。
或者,有没有办法列出哪些外键引用了表?
Is there a way of listing ALL foreign keys in a SQLite database?
They don't seem to be stored in sqlite_master and PRAGMA foreign_key_list('table')
only lists one at a time.
Alternatively, is there a way of listing what foreign keys reference a table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
似乎所有(或许多)
PRAGMA
命令都可以通过一些小技巧以编程方式选择;通常它们的调用方式如下:
但这也可以完成:
并且模式也可以(或多或少)获得:
因此,要获得
sqlite_master 和
pragma_foreign_key_list
可以解决这个问题:请注意,
pragma_foreign_key_list
的某些字段如table
、from
、 ...必须被引用;3rd party edit
上面对 sqlite 示例数据库的查询结果来自 dbeaver
看起来像这样
< /a>
It seems that all (or many) of the
PRAGMA
commands can be programatically selected with a little trick;Usually the are called like:
But this can also be done:
And the schema can also be (more or less) obtained:
So, to get all the fks a JOIN between
sqlite_master
andpragma_foreign_key_list
can do the trick:Just take care, that some fields of
pragma_foreign_key_list
liketable
,from
, ... must be quoted;3rd party edit
The result of the query above for the sqlite sample database from dbeaver
looks like this
在 SQLite shell 中,使用
.schema
指令,并使用 GREP 过滤包含REFERENCES
的行。从 SQLite 存储库中的
shell.c
中,主干中的今天版本,两个查询:和
第二个可能是您正在寻找的内容。
With the SQLite shell, use the
.schema
instruction, and use GREP to filter lines containingREFERENCES
.From
shell.c
in the SQLite repository, today's version in the trunk, two queries:and
The second one is probably what you are looking for.