本文使用"发现"二字,是表示我作为一个用过mysql oracle hive以及各种nosql数据库的男人,竟然发现有一个如此常识的数据库我竟然不知道。
在配置airflow的时候,我想当然的认为airflow的元数据应该储存在像是mysql类型的数据库中,我从来没有安装过sqlite,但是我直接初始化的airflow的时候,竟然初始化成功了,
并产生了airflow.db这个文件。
SQLite 是一个被大家低估的数据库,但有些人认为它是一个不适合生产环境使用的玩具数据库。事实上,SQLite 是一个非常可靠的数据库,它可以处理 TB 级的数据,但它没有网络层。
它“只是”一个库,它不是传统意义上的服务器。
那我开始了解一下,sqlite是最轻量级的数据库,或者可能连数据库都算不上。但是它又具备数据库的很多功能比如增删改查等等
small, fast, self-contained, high-reliability, full-featured
那我又有疑问sqlite和mysql等数据库的应用环境差异在哪里,其实官网早有解释
SQLite is not directly comparable to client/server SQL database engines such as MySQL, Oracle, PostgreSQL, or SQL Server since SQLite is trying to solve a different problem. Client/server SQL database engines strive to implement a shared repository of enterprise data. They emphasize scalability, concurrency, centralization, and control. SQLite strives to provide local data storage for individual applications and devices. SQLite emphasizes economy, efficiency, reliability, independence, and simplicity. SQLite does not compete with client/server databases. SQLite competes with fopen()
这一页官网有一句话还是单独拿出来以证明sqllte并不小:
SQLite数据库的大小限制为140 TB(2 47字节,128 TB)。即使可以处理更大的数据库,SQLite也会将整个数据库存储在单个磁盘文件中,而许多文件系统将文件的最大大小限制为小于此大小。
1.SQlite 通过文件来保存数据库,一个文件就是一个数据库。
2.数据库里又包含数个表格;
3.每个表格里面包含了多个记录;
4.每个记录由多个字段组成;
5.每个字段都有其对应的值;
6.每个值都可以指定类型,并且指定约束。
共有15种:
建表
create table 表名(参数名1 类型 修饰条件,参数名2,类型 修饰参数,···)
create table class(num integer PRIMARY KEY,name text NOT NULL DEFAULT "1班",count integer CHECK(count>10))
删表
删除一张表适用下面的语句:
drop table class
drop table 表名
添加数据
使用下面的语句来进行数据行的添加操作:
insert into class(num,name,count) values(2,"三年2班",58)
上面的语句代码可以简化成如下格式:
insert into 表名(键1,键2,···) values(值1,值2,···)
使用下面的语句进行数据列的添加,即添加一个新的键:
alter table class add new text
alter table 表名 add 键名 键类型
修改数据
使用如下语句来进行改操作:
update class set num=3,name="新的班级" where num=1
update 表名 set 键1=值1,键2=值2 where 条件
where后面添加修改数据的条件,例如上面代码修改num为1的班级的名字和mun值。
删除数据
delete from class where num=1
delete from 表名 where 条件
上面代码删除num为1的一条数据。
查询操作
查询操作是数据库的核心功能,sqlite的许多查询命令可以快捷的完成复杂的查询功能。
查询表中某些键值:
select num from class
select 键名,键名··· from 表名
查询全部键值数据:
select * from class
select * from 表名
*是一个全通配符,代表不限个数任意字符
查询排序:
select * from class order by count asc
select 键名,键名,··· from 表名 order by 键名 排序方式
order by 后面写要进行排序的键名,排序方式有 asc升序 desc降序
查找数据条数与查找位置限制:
select * from class limit 2 offset 0
select 键名 from 表名 limit 最大条数 offset 查询起始位置
条件查询:
select * from class where num>2
select 键名 from 表名 where 条件
查询数据条数:
select count(*) from class
select count(键名) from 表名
去重查询:
select
distinct
num
from
class
select distinct 键名 from 表名
原文:https://www.cnblogs.com/wqbin/p/11938592.html