数据库学习—SQLite
了解最轻巧的数据库SQLite
SQLite,是一款轻型的数据库,占用资源非常低,能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,比如 Tcl、C#、PHP、Java等,还有ODBC接口,同样比起Mysql、PostgreSQL这两款开源的世界著名数据库管理系统来讲,它的处理速度比他们都快。SQLite的最新版本是 SQLite 3。
(1)在Windows上安装SQLite
- 需要访问下载页面 - http://www.sqlite.org/download.html 并下载预编译的Windows二进制文件。
- 您需要下载 sqlite-tools-win32-.zip 和 sqlite-dll-win64-.zip 压缩文件。
- 创建一个文件夹:D:/sqlite并放置这些文件。它将如下所示:
- 进入D:/sqlite目录并打开sqlite3命令。它将如下所示:
上述方法有助于永久创建数据库,附加数据库和分离数据库。
(2)SQLite创建数据库
在SQLite中,sqlite3命令用于创建新的数据库。
语法:
sqlite3 DatabaseName.db
示例:
创建一个名称为“test.db”的数据库:
sqlite3 test.db,它将如下所示:
执行上面命令后,就创建了数据库。 可以使用“.databases”命令检查创建的数据库。它将如下所示:
也可以在SQLite根文件夹中看到创建的数据库。
(3)SQLite创建表
在SQLite中,CREATE TABLE语句用于创建新表。 在创建表时,需要为表指定一个名称并定义表的每列和数据类型。
语法:
CREATE TABLE database_name.table_name(
column1 datatype PRIMARY KEY(one or more columns),
column2 datatype,
column3 datatype,
.....
columnN datatype,
);
示例:
创建一个SQLite数据库中的表student:
使用SQLite的.tables命令查看表是否已成功创建。
在SQLite中,DROP TABLE语句用于删除表定义以及与该表关联的所有关联数据,索引,触发器,约束和权限规范。
语法:
DROP TABLE database_name.table_name;
SQLite图形化管理工具(SQLite Expert Professional )
SQLite Expert Professional是SQLite 的可视化管理工具,不仅可以浏览SQLite内存储的资料,同时也可以进行新增、修改、删除等操作。
需要到官网下载SQLite Expert Professional免费版,网址为:-http://www.sqliteexpert.com/download.html
用图形化管理工具打开一个SQLite数据库文件,查看其中的表。
打开数据库之后,可以看到创建的student表,点击Data可以查看表。
SQLite最基本的SQL语句学习
一、SQLite常见的数据类型
SQLite只支持常见的5种存储类:NULL、INTEGER(整型)、REAL(浮点型)、TEXT(文本)、BLOB(大二进制对象)。
二、基本的数据操作
1,建立表
Create table student(
username text,
age integer);
2,插入数据
insert into 表名(字段列表)values(值列表);
insert into student values(‘song’,25);
3,查询
elect 字段名 from 表名;
select * from student;
select distinct field from table_name;(distinct去掉重复项,将列表中各字段值的单个列出)
4,删除数据
Delete from 表名 where 条件子句;
delete from student from where username=‘song’;
5,修改
update 表名 set 字段名=值 where 条件子句;
update student set username=‘zhang’,age=24 where username=‘song’and age=25;
6,按条件分组
select * from 表名 where 条件子句 group by 分组子句 having ...order by排子句
select * from student;
select * from student order by id desc(降序)|asc(升序);
select uesranme from student group by username having count(*)>1;
7,多条件查询语句
select 字段名 from 表名 where 子句1 and 子句2;
select * from student where username=‘song’ and age=24;
select * from table_name where field in (‘vall’,‘val2’,‘val3’);
select * from table_name where field between vall and val2;
select * from student limit 5;(限制输出数据记录数量)
8,多条件排序
select 字段名 from 表名 order by 字段1(desc),字段2(desc);
select * from student order by t1,t2 desc;
9,索引
建立复合索引:create index idxT1 on student(username,age);
各自建立索引:create index idxUsername on student(username);
create index idxAge on student(age);
10,分页
select * from account limit 5 offset 3;
或者 select * from account limit 5,3;
11,模糊查询
select 字段 from 表 where 某字段 like 条件;
(1)%表示任意0个或多个字符
(2)_表示任意单个字符,匹配单个任意字符,常用来限制表达式的字符长度语
(3)[]表示括号内所列字符中的一个
select * from student where username like ‘[张李王]三’;
表示搜索的是“张三”,“李三”或“王三”
(4)[^]表示不在括号所列之类的单个字符。
12,删除表|索引
drop table[IF EXISTS]student;
drop index index_name;
13,查询记录数目
select count(*)from table_name;