码迷,mamicode.com
首页 > 数据库 > 详细

sqlite笔记(akaedu)

时间:2015-12-17 22:14:10      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:

1.创建sql表
create table student(id integer primary key, name text, score integer);

2.插入一条记录
insert into student(score, id, name) values(100, 1, ‘XiaoMing‘);
insert into student values(2, "XiaoZhang", 90);
//主键没有的给的话,会自动分配一个给你的记录,其他没有not null约束的字段你没有提供的话,默认是可以为空(null)的
insert into student(name) values("XiaoLiu");

3.简单的查询语句
select id, name from student;
select * from student;

4.修改一条记录(where子句很重要,没有where则修改所有记录)
update student set score=80, name="XiaoWu" where id=3;

5.删除一条记录
delete from student; //没有where子句删除所有记录
delete from student where id=3;

6.数据的批量导入
这不是SQL语句,是sqlite3工具的一条命令
.import 1.txt student

7.修改表的结构
alter table student add score2 integer;
可以使用命令.schema student查看student表结构。
alter table student rename to newstudent;修改表名
但是不支持修改某一个现有字段。(没有modify操作)

8.备份一张表的内容(备份表内容,但是表结构可能有差异)
备份student表的所有内容到新的表newstudent
create table newstudent as select * from student;
备份student表的头三列到新的表newstudent中
create table newstudent as select id, name, score from student;

9.删除表
drop table student;删除student表

10.复杂的查询语句
select * from student where score>80;查询成绩大于80分的同学
select * from student where score>87 and score<100;
select * from student where score between 87 and 100;
where score between 87 and 100;
等价于 where score>=87 and score<=100;

模糊查询
select * from student where score like "9%";
select * from student where name like "%g";
select * from student where score like "87";等价于select * from student where score=87;

排序输出
select * from student order by score desc; 降序
select * from student order by score asc;升序
order by默认是升序排列

找80分以上成绩最低的两位学员:
select * from student where score>=80 order by score asc limit 2;

找班上成绩第三名的同学:
select * from student order by score desc limit 1 offset 2;

找班上成绩最高的一位或几位同学:
select * from student where score=(select score from student order by score desc limit 1);

group by子句(having是group by的条件子句)
select dep, sum(salory) from employee where salory>4000 group by dep; //按部门列出每个月每个部门所发薪水总和
select name from employee group by name, salory, dep having count(*)>1;//求出出现重复录入的数据的人的姓名

连接两张表的内容:
sqlite> select * from student;
1|XiaoMing|21
2|XiaoZhang|22
3|XiaoWu|19
sqlite> select * from score;
1|100
2|96

1.where子句连接两张表
select a.id, a.name, a.age, b.score from student a, score b where a.id=b.id;
1|XiaoMing|21|100
2|XiaoZhang|22|96

2.自然连接(要求两张表中要有相同名字的字段,字段值相同的记录被连接到一起输出)
select id, name, age, score from student natural join score;
1|XiaoMing|21|100
2|XiaoZhang|22|96
如果两张表中没有相同名字的字段(student的id,score的id名字相同),连接不能成功,输出两张表的笛卡尔积
select id, name, age, nid, score from student natural join newscore;
1|XiaoMing|21|1|100
1|XiaoMing|21|2|96
2|XiaoZhang|22|1|100
2|XiaoZhang|22|2|96
3|XiaoWu|19|1|100
3|XiaoWu|19|2|96

左外连接(左边的表中,即使在右边表内没有连接成功的项也会输出。)
select a.id, name, age, score from student a left outer join score b on a.id=b.id;
1|XiaoMing|21|100
2|XiaoZhang|22|96
3|XiaoWu|19|     =>这一项因为左外连接而输出,注意和下面的比较

select a.id, name, age, score from score b left outer join student a on a.id=b.id;
1|XiaoMing|21|100
2|XiaoZhang|22|96

sqlite笔记(akaedu)

标签:

原文地址:http://www.cnblogs.com/embedded-linux/p/5055254.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!