版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/villa_mou/article/details/88077380
运算符 描述
AND ->AND 运算符允许在一个 SQL 语句的 WHERE 子句中的多个条件的存在。
OR ->OR 运算符用于结合一个 SQL 语句的 WHERE 子句中的多个条件。
BETWEEN ->BETWEEN 运算符用于在给定最小值和最大值范围内的一系列值中搜索值(包括最大值和最小值)。
EXISTS ->EXISTS 运算符用于在满足一定条件的指定表中搜索行的存在。
IN ->IN 运算符用于把某个值与一系列指定列表的值进行比较(值是否存在于列表里面)。
NOT IN ->IN 运算符的对立面,用于把某个值与不在一系列指定列表的值进行比较。
LIKE ->LIKE 运算符用于把某个值与使用通配符运算符的相似值进行比较。(%代表0或者很多任意字符,_代表一个任意字符)
GLOB ->GLOB 运算符用于把某个值与使用通配符运算符的相似值进行比较。GLOB 与 LIKE 不同之处在于,它是大小写敏感的。(*代表0或者多个任意字符,?代表一个任意字符)
NOT ->NOT 运算符是所用的逻辑运算符的对立面。比如 NOT EXISTS、NOT BETWEEN、NOT IN,等等。它是否定运算符。
IS NULL ->NULL 运算符用于把某个值与 NULL 值进行比较。
IS ->IS 运算符与 = 相似。
IS NOT ->IS NOT 运算符与 != 相似。
|| ->连接两个不同的字符串,得到一个新的字符串。
UNIQUE ->UNIQUE 运算符搜索指定表中的每一行,确保唯一性(无重复)。
例子
user表
id name age city job
---------- ---------- ---------- ---------- ----------
0 张三 24 四川成都 程序员
1 李四 25 四川成都 java开发
2 王五 30 四川泸州 android开发
3 钩子 20 四川绵阳 android开发
4 腰子 35 四川广元 android开发
5 李伟 15 四川广元 android开发
6 JIM 28 Canada
7 Jim 28 Canada
select *from user where age > 30 and job like '%开发';
id name age city job
---------- ---------- ---------- ---------- ----------
4 腰子 35 四川广元 android开发
select *from user where age > 30 or age <20;
id name age city job
---------- ---------- ---------- ---------- ----------
4 腰子 35 四川广元 android开发
5 李伟 15 四川广元 android开发
select *from user where age between 25 and 30;
id name age city job
---------- ---------- ---------- ---------- ----------
1 李四 25 四川成都 java开发
2 王五 30 四川泸州 android开发
6 JIM 28 Canada
7 Jim 28 Canada
select * from user where exists(select age from user where age =25);
查出全部数据
select * from user where age in (15,28);
id name age city job
---------- ---------- ---------- ---------- ----------
5 李伟 15 四川广元 android开发
6 JIM 28 Canada
7 Jim 28 Canada
select * from user where age not in (15,28,30,35);
id name age city job
---------- ---------- ---------- ---------- ----------
0 张三 24 四川成都 程序员
1 李四 25 四川成都 java开发
3 钩子 20 四川绵阳 android开发
select * from user where name like '%m';
id name age city job
---------- ---------- ---------- ---------- ----------
6 JIM 28 Canada
7 Jim 28 Canada
select * from user where name like '_im';
id name age city job
---------- ---------- ---------- ---------- ----------
6 JIM 28 Canada
7 Jim 28 Canada
select * from user where name glob '*m';
id name age city job
---------- ---------- ---------- ---------- ----------
7 Jim 28 Canada
select * from user where job is null;
id name age city job
---------- ---------- ---------- ---------- ----------
6 JIM 28 Canada
7 Jim 28 Canada
更新
update user set name ='liwei' where name ='张三';
id name age city job
---------- ---------- ---------- ---------- ----------
0 liwei 24 四川成都 程序员
更新表里面的所有内容
update user set age=25,job= 'coder';
id name age city job
---------- ---------- ---------- ---------- ----------
0 liwei 25 四川成都 coder
1 李四 25 四川成都 coder
2 王五 25 四川泸州 coder
3 钩子 25 四川绵阳 coder
4 腰子 25 四川广元 coder
5 李伟 25 四川广元 coder
6 JIM 25 Canada coder
7 Jim 25 Canada coder
删除
delete from user where name ='JIM';
id name age city job
---------- ---------- ---------- ---------- ----------
0 liwei 25 四川成都 coder
1 李四 25 四川成都 coder
2 王五 25 四川泸州 coder
3 钩子 25 四川绵阳 coder
4 腰子 25 四川广元 coder
5 李伟 25 四川广元 coder
7 Jim 25 Canada coder
delete from user;
删除所有内容
limit(条数)和offset(偏移条数);
select * from user limit 2 offset 2;从第三条开始查2条
id name age city job
---------- ---------- ---------- ---------- ----------
2 王五 25 四川泸州 coder
3 钩子 25 四川绵阳 coder
order by 排序 ,asc升序,desc降序;
id name age salary
---------- ---------- ---------- ----------
0 tom 23 2000.0
1 jom 25 2500.0
2 tony 15 1500.0
3 kon 35 5500.0
升序
select * from worker order by salary asc;
id name age salary
---------- ---------- ---------- ----------
2 tony 15 1500.0
0 tom 23 2000.0
1 jom 25 2500.0
3 kon 35 5500.0
降序
select * from worker order by salary desc;
id name age salary
---------- ---------- ---------- ----------
3 kon 35 5500.0
1 jom 25 2500.0
0 tom 23 2000.0
2 tony 15 1500.0
group by
SQLite 的 GROUP BY 子句用于与 SELECT 语句一起使用,来对相同的数据进行分组。
GROUP BY 子句必须放在 WHERE 子句中的条件之后,必须放在 ORDER BY 子句之前。
id name age salary
---------- ---------- ---------- ----------
0 tom 23 2000.0
1 jom 25 2500.0
2 tony 15 1500.0
3 kon 35 5500.0
4 tom 23 4000.0
5 jom 33 5000.0
根据名字进行分组,查询每个名字下面薪水总和
select name,sum(salary) from worker group by name;
name sum(salary)
---------- -----------
jom 7500.0
kon 5500.0
tom 6000.0
tony 1500.0
根据名字进行分组,查询每个名字下面薪水总和,并且对名字进行降序排列
select name, sum(salary) from worker group by name order by name desc;
name sum(salary)
---------- -----------
tony 1500.0
kon 5500.0
jom 7500.0
aom 6000.0
HAVING
指定条件来过滤将出现在最终结果中的分组结果。
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY
查询薪水总和>5000的相同姓名进行分组;
select * from worker group by name having sum(salary)>5000;
id name age salary
---------- ---------- ---------- ----------
0 aom 23 2000.0
1 jom 25 2500.0
3 kon 35 5500.0
Distinct 去重
select distinct name from worker;
对名字去重;
name
----------
aom
jom
tony
kon
别名
as 可以暂时把表或列重命名为另一个名字,这被称为别名
select c.name,c.age from worker as c;
name age
---------- ----------
aom 23
jom 25
tony 15
kon 35
aom 23
jom 33
worker
id name age salary
---------- ---------- ---------- ----------
0 aom 23 2000.0
1 jom 25 2500.0
2 tony 15 1500.0
3 kon 35 5500.0
4 aom 23 4000.0
5 jom 33 5000.0
company
ID NAME AGE CITY
---------- ---------- ---------- ----------
0 张三 23 四川成都
1 张三 23 四川成都
2 张三 23 四川成都
4 李四 35 广东深圳
select c.name,c.age from worker as c,COMPANY as d where c.id = d.id;
name age
---------- ----------
aom 23
jom 25
tony 15
aom 23
Trigger
触发器(Trigger)是数据库的回调函数,它会在指定的数据库事件发生时自动执行/调用。
首先创建一张note表,记录触发时间;;
create table note(note_id int not null,time text not null);
然后创建触发器;
create trigger note_trigger after insert on worker
begin
insert into note(note_id,time) values(new.id,datetime('now'));
end;
最后插入
insert into worker(id,name,age) values(6,'villa',28);
查看note表数据
note_id time
---------- -------------------
6 2019-03-02 10:09:40
完成
列出所有触发器的名字
SELECT name FROM sqlite_master WHERE type = 'trigger';
删除触发器
DROP TRIGGER trigger_name;
Index
索引(Index)是一种特殊的查找表,数据库搜索引擎用来加快数据检索。简单地说,索引是一个指向表中数据的指针。一个数据库中的索引与一本书后边的索引是非常相似的。
索引有助于加快 SELECT 查询和 WHERE 子句,但它会减慢使用 UPDATE 和 INSERT 语句时的数据输入。索引可以创建或删除,但不会影响数据。
什么情况下要避免使用索引?
虽然索引的目的在于提高数据库的性能,但这里有几个情况需要避免使用索引。使用索引时,应重新考虑下列准则:
索引不应该使用在较小的表上。
索引不应该使用在有频繁的大批量的更新或插入操作的表上。
索引不应该使用在含有大量的 NULL 值的列上。
索引不应该使用在频繁操作的列上。
创建索引
create index salary_index on worker(salary);
删除索引
DROP INDEX index_name;
Alter
重命名表和在已有的表中添加列
alter table worker add sex char(1);
id name age salary sex
---------- ---------- ---------- ---------- ----------
0 aom 23 2000.0
1 jom 25 2500.0
2 tony 15 1500.0
3 kon 35 5500.0
4 aom 23 4000.0
5 jom 33 5000.0
6 villa 28
View
只不过是通过相关的名称存储在数据库中的一个 SQLite 语句。视图(View)实际上是一个以预定义的 SQLite 查询形式存在的表的组合。并不是真实存在的表,只是一个虚拟的表,其实就是一个sql,为了方便查询;
创建
create view user_worker_view as select id,name from worker;
查询
select * from user_worker_view;
id name
---------- ----------
0 aom
1 jom
2 tony
3 kon
4 aom
5 jom
6 villa
日期 & 时间
1 date(timestring, modifier, modifier, ...) 以 YYYY-MM-DD 格式返回日期。
2 time(timestring, modifier, modifier, ...) 以 HH:MM:SS 格式返回时间。
3 datetime(timestring, modifier, modifier, ...) 以 YYYY-MM-DD HH:MM:SS 格式返回。
常用函数
SQLite COUNT 函数
SQLite COUNT 聚集函数是用来计算一个数据库表中的行数。
2 SQLite MAX 函数
SQLite MAX 聚合函数允许我们选择某列的最大值。
3 SQLite MIN 函数
SQLite MIN 聚合函数允许我们选择某列的最小值。
4 SQLite AVG 函数
SQLite AVG 聚合函数计算某列的平均值。
5 SQLite SUM 函数
SQLite SUM 聚合函数允许为一个数值列计算总和。
6 SQLite RANDOM 函数
SQLite RANDOM 函数返回一个介于 -9223372036854775808 和 +9223372036854775807 之间的伪随机整数。
7 SQLite ABS 函数
SQLite ABS 函数返回数值参数的绝对值。
8 SQLite UPPER 函数
SQLite UPPER 函数把字符串转换为大写字母。
9 SQLite LOWER 函数
SQLite LOWER 函数把字符串转换为小写字母。
10 SQLite LENGTH 函数
SQLite LENGTH 函数返回字符串的长度。
11 SQLite sqlite_version 函数
SQLite sqlite_version 函数返回 SQLite 库的版本。