SQLite的System Catalogs

-- TOC --

System Catalog就是数据库系统自己记录的数据,每个数据库里面都有。SQLite数据库中,所有以sqlite_开头的表,都是这一类。

这些table基本都是read-only,可以用select读,如果你遇到一个不知道内部结构的数据库,查看system catalog表是个不错的开始。

所有非临时的SQLite数据库,都有sqlite_master表,它记录了所有数据库中的对象,它也是最重要的。

sqlite_master

如果有 AUTOINCREMENT 列,sqlite_master中被自动创建一个 sqlite_sequence 表,来记录一些需要的数据:

sqlite> create table aa(id integer primary key);
sqlite> select * from sqlite_master;
table|aa|aa|2|CREATE TABLE aa(id integer primary key)
sqlite> .mode column
sqlite> select * from sqlite_master;
type   name  tbl_name  rootpage  sql
-----  ----  --------  --------  ---------------------------------------
table  aa    aa        2         CREATE TABLE aa(id integer primary key)
sqlite>
sqlite> create table bb(id integer primary key autoincrement);
sqlite> select * from sqlite_master;
type   name             tbl_name         rootpage  sql
-----  ---------------  ---------------  --------  -----------------------------------------------------
table  aa               aa               2         CREATE TABLE aa(id integer primary key)
table  bb               bb               3         CREATE TABLE bb(id integer primary key autoincrement)
table  sqlite_sequence  sqlite_sequence  4         CREATE TABLE sqlite_sequence(name,seq)

如果用其它方式创建主键,默认会产生一个index,通过sqlite_master就能够清晰的看到:

sqlite> create table t3(id int primary key not null);
sqlite> select * from sqlite_master;
table|t3|t3|2|CREATE TABLE t3(id int primary key not null)
index|sqlite_autoindex_t3_1|t3|3|
sqlite> .mode column
sqlite> select * from sqlite_master;
type   name                   tbl_name  rootpage  sql
-----  ---------------------  --------  --------  --------------------------------------------
table  t3                     t3        2         CREATE TABLE t3(id int primary key not null)
index  sqlite_autoindex_t3_1  t3        3

如果用过 ANALYZE 语句,还会产生 sqlite_stat# 表格。

临时的table不在sqlite_master中,他们在sqlite_temp_master中。

本文链接:https://cs.pynote.net/sf/sqlite/202203203/

-- EOF --

-- MORE --