sqlite笔记(akaedu)
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)的更多相关文章
- 编写SQL语句操作数据库(慕课SQLite笔记)
安卓常用数据存储方式之一SQLite学习及操作笔记 0.视频地址:http://www.imooc.com/video/3382 1.每个程序都有自己的数据库 默认情况下是各自互不干扰 1)创建一个数 ...
- Android中使用sqlite笔记
1.实现SQLiteHelper来在android中使用SQLite.代码如下,来自android官网. public class FeedReaderDbHelper extends SQLiteO ...
- SQLite笔记
一.SQLite下载: http://www.sqlite.org/download.html (或在NuGet下载安装) 二.SQLite操作: 1.添加引用System.Data.SQLite,如 ...
- python之SQLite笔记
sqlite3 打开文件并创建游标 conn = sqlite3.connect('adressbook.db')c = conn.cursor() 连接对象:sqlite3.connect('数据文 ...
- Ionic2学习笔记(8):Local Storage& SQLite
作者:Grey 原文地址: http://www.cnblogs.com/greyzeng/p/5557947.html Ionic2可以有两种方式来存储数据,Local S ...
- SQLite学习笔记(七)&&事务处理
说到事务一定会提到ACID,所谓事务的原子性,一致性,隔离性和持久性.对于一个数据库而言,通常通过并发控制和故障恢复手段来保证事务在正常和异常情况下的ACID特性.sqlite也不例外,虽然简单,依然 ...
- Sqlite学习笔记(四)&&SQLite-WAL原理
Sqlite学习笔记(三)&&WAL性能测试中列出了几种典型场景下WAL的性能数据,了解到WAL确实有性能优势,这篇文章将会详细分析WAL的原理,做到知其然,更要知其所以然. WAL是 ...
- Sqlite学习笔记(一)&&编译安装
Sqlite简介 sqlite是一个开源的嵌入式文件数据库,sqlite以动态链接库的方式供应用程序调用,所有的数据库对象都存储在同一个文件中. sqlite动态库非常小,最新的3.8.11版本也只有 ...
- 安卓第四天笔记-Sqlite
安卓第四天笔记-Sqlite 1.数据库的创建运行与更新 1.1.创建一个类继承SqliteOpenHelper 1.2.创建构造方法 /** * 数据库创建类 * @author 刘楠 * * 20 ...
随机推荐
- [Big Data] Week 5 A (Advance)
Question 1 Consider the diagonal matrix M = 1 0 0 0 2 0 0 0 0 . Compute its Moore-Penrose pseudoinve ...
- VS中的代码拖放
平时数据操作中,我们经常在选择文件,由于数据太多.数据目录太深,选择数据文件到TextBox中非常不方便,于是就想让控件支持拖放数据,很多软件都支持这个功能,非常实用. 在ArcGIS10.x开发平台 ...
- 用mapreduce来操作hbase的优化
(1)scan.setCacheBlocks(false); 初始化map任务 TableMapReduceUtil.initTableMapperJob 本次mr任务scan的所有数据不放在缓 ...
- HDU 1565 1569 方格取数(最大点权独立集)
HDU 1565 1569 方格取数(最大点权独立集) 题目链接 题意:中文题 思路:最大点权独立集 = 总权值 - 最小割 = 总权值 - 最大流 那么原图周围不能连边,那么就能够分成黑白棋盘.源点 ...
- 彻底告别加解密模块代码拷贝-JCE核心Cpiher详解
前提 javax.crypto.Cipher,翻译为密码,其实叫做密码器更加合适.Cipher是JCA(Java Cryptographic Extension,Java加密扩展)的核心,提供基于多种 ...
- this详解与面向对象编程
原文链接:http://www.cnblogs.com/kongxy/p/4581223.html JS中的this对象详解 声明:文章转载自上面地址,版权归阿孔所有,这里仅供本人学习笔记使用 t ...
- 使用Nginx负载均衡搭建高性能.NETweb应用程序(转)
一.遇到的问题 当我们用IIS服务器部署了一个web应用以后,当很多用户高并发访问的时候,客户端响应就会很慢,客户的体验就会很差,由于IIS接受到客户端请求的时候,就会创建一个线程,当线程达到几千个时 ...
- Hibernate基于注解方式的各种映射全面总结
1. 使用Hibernate Annotation来做对象关系映射 1) 添加必须包: hibernate-jpa-2.0-api-1.0.0.Final.jar 2) 在实体类中添加JPA的标准注解 ...
- 【CI3.1】CI框架简单使用方法
CI框架简单使用方法 1.回忆MVC 1.1.M:模型,提供数据,保存数据 1.2.V:视图,只负责显示,表单form 1.3.C:控制器,协调模型和视图 1.4.action:动作,是控制器中的方法 ...
- PHP-流的概念与详细用法
Stream是PHP开发里最容易被忽视的函数系列(SPL系列,Stream系列,pack函数,封装协议)之一,但其是个很有用也很重要的函数.Stream可以翻译为“流”,在Java里,流是一个很重要的 ...