我刚刚开始学习SQLite。如果能够看到表的详细信息就太好了,比如MySQL的DESCRIBE [table]。PRAGMA table_info [table]不够好,因为它只有基本信息(例如,它没有显示一个列是否是某种类型的字段)。SQLite有办法做到这一点吗?
您正在寻找用于生成表的SQL吗?为此,你可以查询sqlite_schema表:
sqlite> CREATE TABLE foo (bar INT, quux TEXT);
sqlite> SELECT * FROM sqlite_schema;
table|foo|foo|2|CREATE TABLE foo (bar INT, quux TEXT)
sqlite> SELECT sql FROM sqlite_schema WHERE name = 'foo';
CREATE TABLE foo (bar INT, quux TEXT)
选择的名字 模式表总是可以使用名称sqlite_schema来引用,特别是在使用模式名(如main)限定的情况下。Sqlite_schema或temp.sqlite_schema。但为了历史兼容,一些替代名称也被认可,包括: sqlite_master sqlite_temp_schema sqlite_temp_master 替代方案(2)和(3)仅适用于与每个数据库连接关联的TEMP数据库,但替代方案(1)适用于任何地方。
为了防止人们被一些评论误导到其他答案:
如果.schema或sqlite_master的查询没有给出任何输出,则表示表名不存在,例如,这也可能是由;.schema, .tables,…或者只是因为这个表根本不存在。 schema不能正常工作是不太可能的,然后应该在sqlite项目中提交错误报告。
... .模式只能在命令行中使用;上面的命令>可以通过库(Python, c#等)作为查询运行。——Mark Rushakoff 7月25日21:09
'can only be used from a command line' may mislead people. Almost any (likely every?) programming language can call other programs/commands. Therefore the quoted comment is unlucky as calling another program, in this case sqlite, is more likely to be supported than that the language provides a wrapper/library for every program (which not only is prone to incompleteness by the very nature of the masses of programs out there, but also is counter acting single-source principle, complicating maintenance, furthering the chaos of data in the world).
如果你使用图形工具。它在表名旁边显示模式。对于DB Browser For Sqlite,单击打开数据库(右上角),导航并打开您的数据库,您将看到填充在表中的信息如下所示。
右键单击record/table_name,单击copy create statement,你就有了它。
希望它能帮助到一些无法使用命令行的初学者。
".schema可以比PRAGMA显示更多的表细节,包括表约束。
下面的命令显示所有表的详细信息:
.schema
下面的命令以良好的格式显示了所有表的详细信息:
.schema --indent
下面的命令显示了一个表的详细信息:
.schema <table_name>
下面这些命令以一种格式良好的方式显示一个表的详细信息:
.schema --indent <table_name>
Or:
.schema <table_name> --indent
此外,下面这些命令显示了关于".schema"的详细信息:
.help .schema
Or:
.help schema
然后,它是这样的:
sqlite> .help .schema
.schema ?PATTERN? Show the CREATE statements matching PATTERN
Options:
--indent Try to pretty-print the schema
--nosys Omit objects whose names start with "sqlite_"
推荐文章
- 如何停止mysqld
- 检查MySQL表是否存在而不使用“select from”语法?
- 从NOW() -1天选择记录
- 我看到VARCHAR(255)如此频繁地使用(而不是其他长度),有什么好的原因吗?
- 使用pgadmin连接到heroku数据库
- 从表中选择1是什么意思?
- Delete_all vs destroy_all
- 我如何移动一个redis数据库从一个服务器到另一个?
- 如何首次配置postgresql ?
- 数据库性能调优有哪些资源?
- 如何更改表的默认排序规则?
- 如何在PostgreSQL中自动更新时间戳
- MySQL foreign_key_checks是否影响整个数据库?
- 设置NOW()为datetime数据类型的默认值?
- 在MySQL中Datetime等于或大于今天