您好,欢迎访问一九零五行业门户网

SQLite入门之四表的增删攺查

您现在的位置:首页>教程>编程开发>mssql数据库 > sqlite入门之四表的增删攺查 sqlite入门之四表的增删攺查 感谢 3lian8 的投递 时间:2014-03-13 来源:三联教程 4.1 sqlite 存储类型 sqlite 存储类型 存储类型描述 null值是一个 null 值。 integer值是一个
您现在的位置:首页 > 教程 > 编程开发 > mssql数据库 > sqlite入门之四表的增删攺查
sqlite入门之四表的增删攺查
感谢 3lian8 的投递 时间:2014-03-13 来源:三联教程
4.1 sqlite 存储类型
sqlite 存储类型存储类型描述
null值是一个 null 值。
integer值是一个带符号的整数,根据值的大小存储在 1、2、3、4、6 或 8 字节中。
real值是一个浮点值,存储为 8 字节的 ieee 浮点数字。
text值是一个文本字符串,使用数据库编码(utf-8、utf-16be 或 utf-16le)存储。
blob值是一个 blob 数据,,完全根据它的输入存储。
4.2 创建数据库和创建表创建了一个 company 表,id 作为主键,not null 的约束表示在表中创建纪录时这些字段不能为 null
?
1
2
3
4
5
bixiaopeng@bixiaopeng db$ sqlite3 wireless.db
sqlite version 3.7.13 2012-07-17 17:46:21
enter .help for instructions
enter sql statements terminated with a ;
sqlite> create table company(id int primary key not null,name text not null,age int not null,address char(50),salary real);
create table 是告诉数据库系统创建一个新表的关键字。create table 语句后跟着表的唯一的名称或标识。您也可以选择指定带有 table_name 的 database_name。
查看表是否创建成功
?
1
2
sqlite> .tables
company
查看表的完整信息
?
1
2
sqlite> .schema company
create table company(id int primary key not null,name text not null,age int not null,address char(50),salary real);
4.3 插入数据插入数据,方法一:插入对应的列的值
?
1
2
sqlite> insert into company (id,name,age,address,salary)
   ...> values (1, 'paul', 32, 'california', 20000.00 );
查询是否插入成功
?
1
2
sqlite> select * from company;
1|paul|32|california|20000.0
 插入数据,方法二:给所有列插入值
?
1
2
3
4
sqlite> insert into company values (7, 'james', 24, 'houston', 10000.00 );
sqlite> select * from company;
1|paul|32|california|20000.0
7|james|24|houston|10000.0
用第二种方法多插入几个数据:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
sqlite> insert into company values (2, 'allen', 25, 'texas', 15000.00 );
sqlite> insert into company values (3, 'teddy', 23, 'norway', 20000.00 );
sqlite> insert into company values (4, 'mark', 25, 'rich-mond ', 65000.00 );
sqlite> insert into company values (5, 'david', 27, 'texas', 85000.00 );
sqlite> insert into company values (6, 'kim', 22, 'south-hall', 45000.00 );
sqlite> select * from company;
1|paul|32|california|20000.0
7|james|24|houston|10000.0
2|allen|25|texas|15000.0
3|teddy|23|norway|20000.0
4|mark|25|rich-mond |65000.0
5|david|27|texas|85000.0
6|kim|22|south-hall|45000.0
4.4 更新数据
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//先插入一条数据
sqlite> insert into company values (8, 'wirelessqa', 28, 'hz', 20000.00 );
sqlite> select * from company;
id  name    age address salary
1   paul    32  california  20000.0
7   james   24  houston 10000.0
2   allen   25  texas   15000.0
3   teddy   23  norway  20000.0
4   mark    25  rich-mond   65000.0
5   david   27  texas   85000.0
6   kim 22  south-hall  45000.0
8   wirelessqa  28  hz  20000.0
//更新name为wirelessqa的地址为nanjing
sqlite> update company set address = 'nanjing' where name = 'wirelessqa';
8   wirelessqa  28  nanjing 20000.0
//查看更新后的数据
sqlite> select * from company where name = 'wirelessqa';
id  name    age address salary
8   wirelessqa  28  nanjing 20000.0
4.5 删除数据
?
1
2
3
4
//删除address为nanjing的这条数据
sqlite> delete from company where address = 'nanjing';
sqlite> select * from company where address = 'nanjing';
sqlite>
4.6 数据查询4.6.1. sqlite 算术运算符运算符: + - * / %
?
1
2
3
4
5
6
7
8
9
10
sqlite> select 4 + 2;
6
sqlite> select 4 - 2;
2
sqlite> select 4 * 2;
8
sqlite> select 4 / 2;
2
sqlite> select 4 % 2;
0
4.6.2. sqlite 算术运算符运算符描述实例
==检查两个操作数的值是否相等,如果相等则条件为真。(a == b) 不为真。
=检查两个操作数的值是否相等,如果相等则条件为真(a = b) 不为真。
!=检查两个操作数的值是否相等,如果不相等则条件为真(a != b) 为真。
检查两个操作数的值是否相等,如果不相等则条件为真(a b) 为真。
>检查左操作数的值是否大于右操作数的值,如果是则条件为真。(a > b) 不为真。
>=检查左操作数的值是否大于等于右操作数的值,如果是则条件为真(a >= b) 不为真。
看一下表里现有的数据:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
sqlite> .headers on
sqlite> .mode tabs
sqlite> select * from company;
id  name    age address salary
1   paul    32  california  20000.0
7   james   24  houston 10000.0
2   allen   25  texas   15000.0
3   teddy   23  norway  20000.0
4   mark    25  rich-mond   65000.0
5   david   27  texas   85000.0
6   kim 22  south-hall  45000.0
sqlite> select * from company where age = 32;
id  name    age address salary
1   paul    32  california  20000.0
sqlite> select * from company where age == 32 ;
id  name    age address salary
1   paul    32  california  20000.0
sqlite> select * from company where age
id  name    age address salary
7   james   24  houston 10000.0
2   allen   25  texas   15000.0
3   teddy   23  norway  20000.0
4   mark    25  rich-mond   65000.0
5   david   27  texas   85000.0
6   kim 22  south-hall  45000.0
sqlite> select * from company where age != 32;
id  name    age address salary
7   james   24  houston 10000.0
2   allen   25  texas   15000.0
3   teddy   23  norway  20000.0
4   mark    25  rich-mond   65000.0
5   david   27  texas   85000.0
6   kim 22  south-hall  45000.0
sqlite> select * from company where age
id  name    age address salary
1   paul    32  california  20000.0
7   james   24  houston 10000.0
2   allen   25  texas   15000.0
3   teddy   23  norway  20000.0
4   mark    25  rich-mond   65000.0
5   david   27  texas   85000.0
6   kim 22  south-hall  45000.0
sqlite> select * from company where age >= 32;
id  name    age address salary
1   paul    32  california  20000.0
sqlite> select * from company where age > 32;
4.6.3. sqlite 逻辑运算符运算符描述
andand 运算符允许在一个 sql 语句的 where 子句中的多个条件的存在。
betweenbetween 运算符用于在给定最小值和最大值范围内的一系列值中搜索值。
existsexists 运算符用于在满足一定条件的指定表中搜索行的存在。
inin 运算符用于把某个值与一系列指定列表的值进行比较。
not inin 运算符的对立面,用于把某个值与不在一系列指定列表的值进行比较。
likelike 运算符用于把某个值与使用通配符运算符的相似值进行比较。
globglob 运算符用于把某个值与使用通配符运算符的相似值进行比较。glob 与 like 不同之处在于,它是大小写敏感的。
notnot 运算符是所用的逻辑运算符的对立面。比如 not exists、not between、not in,等等。它是否定运算符。
oror 运算符用于结合一个 sql 语句的 where 子句中的多个条件。
is nullnull 运算符用于把某个值与 null 值进行比较。
isis 运算符与 = 相似。
is notis not 运算符与 != 相似。
uniqueunique 运算符搜索指定表中的每一行,确保唯一性(无重复)。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//and 运算符允许在一个 sql 语句的 where 子句中的多个条件的存在。
sqlite> select * from company where age 15000.0;
id  name    age address salary
3   teddy   23  norway  20000.0
6   kim 22  south-hall  45000.0
//or 运算符用于结合一个 sql 语句的 where 子句中的多个条件。
sqlite> select * from company where age 15000.0;
id  name    age address salary
1   paul    32  california  20000.0
7   james   24  houston 10000.0
3   teddy   23  norway  20000.0
4   mark    25  rich-mond   65000.0
5   david   27  texas   85000.0
6   kim 22  south-hall  45000.0
//between 运算符用于在给定最小值和最大值范围内的一系列值中搜索值。
sqlite> select * from company where age between 25 and 32;
id  name    age address salary
1   paul    32  california  20000.0
2   allen   25  texas   15000.0
4   mark    25  rich-mond   65000.0
5   david   27  texas   85000.0
//exists 运算符用于在满足一定条件的指定表中搜索行的存在。
sqlite> select age from company where exists (select age from company where salary > 65000);
age
32
24
25
23
25
27
22
//age 不为 null 的所有记录
sqlite> select * from company where age is not null;
id  name    age address salary
1   paul    32  california  20000.0
7   james   24  houston 10000.0
2   allen   25  texas   15000.0
3   teddy   23  norway  20000.0
4   mark    25  rich-mond   65000.0
5   david   27  texas   85000.0
6   kim 22  south-hall  45000.0
//like 运算符用于把某个值与使用通配符运算符的相似值进行比较。
sqlite> select * from company where name like 'ki%';
id  name    age address salary
6   kim 22  south-hall  45000.0
//glob 运算符用于把某个值与使用通配符运算符的相似值进行比较。glob 与 like 不同之处在于,它是大小写敏感的。
sqlite> select * from company where name glob 'ki*';
id  name    age address salary
6   kim 22  south-hall  45000.0
//in 运算符用于把某个值与一系列指定列表的值进行比较。
sqlite> select * from company where age in ( 25, 27 );
id  name    age address salary
2   allen   25  texas   15000.0
4   mark    25  rich-mond   65000.0
5   david   27  texas   85000.0
//in 运算符的对立面,用于把某个值与不在一系列指定列表的值进行比较。
sqlite> select * from company where age not in ( 25, 27 );
id  name    age address salary
1   paul    32  california  20000.0
7   james   24  houston 10000.0
3   teddy   23  norway  20000.0
6   kim 22  south-hall  45000.0
//
sqlite> select * from company where age > (select age from company where salary > 65000);
id  name    age address salary
1   paul    32  california  20000.0
sqlite> select * from company where age 65000);
id  name    age address salary
7   james   24  houston 10000.0
2   allen   25  texas   15000.0
3   teddy   23  norway  20000.0
4   mark    25  rich-mond   65000.0
6   kim 22  south-hall  45000.0
4.6.4 排序、分组、去重、时间
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//通过内置函数查看一共有多少条数据
sqlite> select count(*) as records from company;
records
7
//显示前4条
sqlite> select * from company limit 4;
id  name    age address salary
1   paul    32  california  20000.0
7   james   24  houston 10000.0
2   allen   25  texas   15000.0
3   teddy   23  norway  20000.0
//按salary降序排序
sqlite> select * from company order by salary asc;
id  name    age address salary
7   james   24  houston 10000.0
2   allen   25  texas   15000.0
1   paul    32  california  20000.0
3   teddy   23  norway  20000.0
6   kim 22  south-hall  45000.0
4   mark    25  rich-mond   65000.0
5   david   27  texas   85000.0
//按salary升序排序
sqlite> select * from company order by salary desc;
id  name    age address salary
5   david   27  texas   85000.0
4   mark    25  rich-mond   65000.0
6   kim 22  south-hall  45000.0
1   paul    32  california  20000.0
3   teddy   23  norway  20000.0
2   allen   25  texas   15000.0
7   james   24  houston 10000.0
//按name和salary升序排序
sqlite> select * from company order by age,salary desc;
id  name    age address salary
6   kim 22  south-hall  45000.0
3   teddy   23  norway  20000.0
7   james   24  houston 10000.0
4   mark    25  rich-mond   65000.0
2   allen   25  texas   15000.0
5   david   27  texas   85000.0
1   paul    32  california  20000.0
// group by 子句用于与 select 语句一起使用,来对相同的数据进行分组。
// 查询某个人的工资总数
sqlite>  select name, sum(salary) from company group by name;
name    sum(salary)
allen   15000.0
david   85000.0
james   10000.0
kim 45000.0
mark    65000.0
paul    20000.0
teddy   20000.0
// group by 和 order by一起用
sqlite> select name, sum(salary) from company group by name order by name desc;
name    sum(salary)
teddy   20000.0
paul    20000.0
mark    65000.0
kim 45000.0
james   10000.0
david   85000.0
allen   15000.0
//having 子句允许指定条件来过滤将出现在最终结果中的分组结果。
//where 子句在所选列上设置条件,而 having 子句则在由 group by 子句创建的分组上设置条件。
//在一个查询中,having 子句必须放在 group by 子句之后,必须放在 order by 子句之前
//查询所有数据
qlite> select * from company;
id  name    age address salary
1   paul    32  california  20000.0
7   james   24  houston 10000.0
2   allen   25  texas   15000.0
3   teddy   23  norway  20000.0
4   mark    25  rich-mond   65000.0
5   david   27  texas   85000.0
6   kim 22  south-hall  45000.0
//查询age,并去重
sqlite> select distinct age from company;
age
32
24
25
23
27
22
日期 & 时间
//把header关掉了
sqlite> . header off
sqlite> select date('now');
2014-02-27
sqlite> select datetime(1092941466, 'unixepoch');
2004-08-19 18:51:06
sqlite> select time('now');
07:47:25?
4.6.5. 常用函数
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//表行数
sqlite> select count(*) from company;
7
//最大值
sqlite> select max(salary) from company;
85000.0
//最小值
sqlite> select min(salary) from company;
10000.0
//平均值
sqlite> select avg(salary) from company;
37142.8571428572
sqlite> select sum(salary) from company;
260000.0
//转大写
sqlite> select upper(name) from company;
paul
james
allen
teddy
mark
david
kim
//转小写
sqlite> select lower(name) from company;
paul
james
allen
teddy
mark
david
kim
//长度
sqlite> select name, length(name) from company;
paul    4
james   5
allen   5
teddy   5
mark    4
david   5
kim 3
sqlite>
4.7 删除表
?
1
2
sqlite> drop table company;
sqlite> .tables
4.8 删除数据库
?
1
直接rm 删除掉db文件就可以了
相关文章
标签:
[返回三联首页] [返回mssql数据库栏目] / [加入三联文集]
其它类似信息

推荐信息