sqlite的sqlite_master与sqlite_temp_master

sqlite的内置表sqlite_master

概述

每一个sqlite数据库都有一个名为sqlite_master的系统表。sqlite_master表中存储当前数据库中所有的表的的相关信息,表的名称、用于创建此表的sql语句、索引、索引所属的表、创建索引的sql语句等。

sqlite_master表是只读的,只能对他进行读操作,不能对它使用 update、insert或 delete。 它会被 creat table、creat index、drop table和 dorp index 等命令自动更新。

sqlite_master表的结构:

  CREATE TABLE sqlite_master (   
    type TEXT,   
    name TEXT,   
    tbl_name TEXT,   
    rootpage INTEGER,   
    sql TEXT   
    );   

对于表来说,type 字段永远是 ‘table’,name 字段永远是表的名字。所以,要获得数据库中所有表的列表, 使用下列select语句:

select name from sqlite_master 
where type=’tableorder by name; 

对于索引,type 等于 ‘index’, name 则是索引的名字,tbl_name 是该索引所属的表的名字。 不管是表还是索引,sql 字段是原先用 creat table或 creat index语句创建它们时的命令文本。对于自动创建的索引(用来实现 PRIMARY KEY 或 UNIQUE 约束),sql字段为NULL。

如果我们想知道当前数据库一共有多少表格和索引可以使用如下语句

select count(*) from sqlite_master

sqlite_temp_master

临时表格不存在于sqlite_master 中,而是存在于sqlite_temp_master中

sqlite_temp_master 表的结构跟sqlite_master一样,这里就不多讲了!

有不同见解的同志,欢迎留言交流~

猜你喜欢

转载自blog.csdn.net/breakpoints_/article/details/80420890