获得数据库的 Schema 信息
1.通过.tables
获取所有表和视图的列表,以上一节为例查询:
sqlite> .tables
输出:schema test
可以看到我们创建的表 test 和视图 schema。
注:视图是一张虚拟的表,由一个预定义的查询select语句组成,可以将一系列复杂的selcet语句转换成一个视图,避免每次输入一大堆的查询语句。
要删除视图则:drop view [视图名]
;
2.同样的,要显示一个表的索引,可以键入命 令.indices [table name]
:
sqlite> .indices test
输出:test_idx
3.使用.schema [tablename]
可以得到一 个表或视图的定义(DDL)语句:
sqlite> .schema test
输出:
CREATE TABLE test(id integer primary key, value text);
CREATE INDEX test_idx on test(value);
如果没提供表名,则返回所有数据库对象(包括 table、indexe、 view 和 index)的定义语句:
sqlite> .schema
输出:
CREATE TABLE test(id integer primary key, value text);
CREATE INDEX test_idx on test(value);
CREATE VIEW schema as select * from sqlite_master;
编号 | 字段 | 说明 |
---|---|---|
1 | type | 值为"table"、 "index"、 "trigger"或"view"之一。 |
2 | name | 对象名称,值为字符串。 |
3 | tbl_name | 如果是表或视图对象,此字段值与字段 2 相同。如果是索引或触 发器对象,此字段值为与其相关的表名。 |
4 | rootpage | 对触发器或视图对象,此字段值为 0。对表或索引对象,此字段 值为其根页的编号。 |
5 | SQL | 字符串,创建此对象时所使用的 SQL 语句。 |
4.更详细的 schema 信息可以通过 SQLite 唯一的一个系统视图 sqlite_master得到,这个视图是一个系统目录,它的结构如下表所示:
编号 | 字段 | 说明 |
---|---|---|
1 | type | 值为"table"、 "index"、 "trigger"或"view"之一。 |
2 | name | 对象名称,值为字符串。 |
3 | tbl_name | 如果是表或视图对象,此字段值与字段 2 相同。如果是索引或触 发器对象,此字段值为与其相关的表名。 |
4 | rootpage | 对触发器或视图对象,此字段值为 0。对表或索引对象,此字段 值为其根页的编号。 |
5 | SQL | 字符串,创建此对象时所使用的 SQL 语句。 |
查询当前数据库的 sqlite_master 表:
sqlite> .mode col
sqlite> .headers on
sqlite> select * from sqlite_master order by type;
结果如下:

网友评论