本文共 3641 字,大约阅读时间需要 12 分钟。
SQL(structured query language):结构化查询语句,是一种对关系型数据库中的数据进行定义和操作的语言。
特点:不区分大小写(比如数据库认为user和UsEr是一样的),每条语句都必须以分号;结尾 SQL中的常用关键字有:select、insert、update、delete、from、create、where、desc、order、by、group、table、alter、view、index等等,数据库中不可以用关键字来命名表、字段。create table 表名(字段名1 字段类型1,字段名2 字段类型2 , . . .);
create table if not exists 表名(字段名1 字段类型1,字段名2 字段类型2, . . . ); 示例create table if not exists t_student(id integer, name text, age integer, score real);
字段类型:SQLite将数据划分为以下几种存储类型:
integer:整型值 real:浮点值 text: 文本字符串 blob:二进制数据(比如文件) 注意:实际上SQLite是无类型的,就算声明为integer类型,还是能存储字符串文本(主键除外) 建表时声明啥类型或者不声明类型都可以,也就意味着创表语句可以这么写:create table if not exists t_student(name,age); 注意:为了保持良好的编程规范,方便程序员之间的交流,编写建表语句的时候最好加上每个字段的具体类型。drop table 表名;
drop table if exists 表名 示例:drop table if exists t_student;
insert into 表名(字段1,字段2, . . . ) values(字段1的值,字段2的值, . . .);
示例:insert into t_student(name,age) values(‘mj’,10);
注意:数据库中的字符串内容应该用单引号’ 括住
update 表名 set 字段1=字段1的值,字段2=字段2的值,. . . ;
示例:update t_student set name=‘jack’,age=20;
注意:上面的示例会将t_student表中所有记录的name都改成jack,age都改成20
delete from t_student;
注意:上面的示例会将t_student表中所有记录都删掉
如果只想更新或者删除某些固定的记录,那就必须在语句后加上一些条件。条件语句的常用格式
where 字段 = 某个值 ; //不能用两个= where 字段 is 某个值 ;//is相当于= where 字段 !=某个值; where 字段is not某个值; //is not 相当于!= where 字段>某个值; where 字段1 = 某个值 and 字段2>某个值;//and相当于C语言中的&& where 字段1 = 某个值 or 字段2=某个值; //or 相当于C语言中的|| 示例:将t_student表中年龄大于10并且姓名不等于jack的记录,年龄都改为5update t_student set age=5 where age>10 and name!=‘jack’;
删除t_student表中年龄小于等于20或者年龄大于30的记录
delete from t_student where age<=20 or age>30;
猜猜下面语句的作用
update t_student set score =age where name=‘jack’;
将数据表t_student中名字等于jack的记录,score字段的值 都改为age字段的值
select 字段1, 字段2, . . .from 表名;
select *from 表名; //查询所有的字段 示例select name, age from t_student;
select *from t_student; select *from t_student where age>10;
格式(字段和表都可以起别名)
select name myname,age myage from t_student;
给name起个叫做myname的别名,给age取个叫做myage 的别名
select ,s.age from t_student s;
给t_student表起个别名叫做s,利用s来引用表中的字段
select count(字段) from 别名;
select count (*)from 表名; 示例select count(age) from t_student;
select count(*) from t_student where score >=60;
查询出来的结果可以用order by进行排序
select *from t_student order by 字段;select * from t_student order by age;
默认是按照升序排序(由小到大),也可以变为降序(由大变小)
select * from t_student order by age desc; //降序
select *from t_student order by age asc; //升序(默认)
也可以用多个字段进行排序
select *from t_student order by age asc,height desc;
先按照年龄排序(升序),年龄相等就按照身高排序(降序)
使用limit可以精确地控制查询结果的数量,比如每次只查询10条数据
格式: select* from 表名 limit 数值1,数值2; //从数值1开始取,取的记录条数为数值2; 数值从0开始记 示例:select * from t_student limit 4,8;
可以理解为:跳过最前面4条语句,然后取8条记录
limit常用来做分页查询,比如每页固定显示5条数据,那么应该这样取数据。select *from t_student limit 0,5;
select * from t_student limit 5,5; select * from t_student limit 10,5; …
第n页:limit 5*(n-1),5
select *from t_student limit 7;
这条语句的作用相当于select *from t_student limit 0,7;表示取最前面的7条数据;
建表时可以给特定的字段设置一些约束条件,常见的约束有
not null: 规定字段的值不能为null; unique: 规定字段的值必须唯一; default: 指定字段的默认值; (尽量给字段设定严格的约束,以保证数据的规范性) 示例:create table t_student (id integer, name text not null unique, age integer not null default 1);
name 字段不能为null,并且唯一,
age 字段不能为null,并且默认为1每张表都必须有一个主键(primary key),用来标识记录的唯一性。
例如t_student 可以增加一个id字段作为主键,相当于人的身份证; (1)主键的设计原则:create table if not exists t_student (id integer primary key, name text, age integer);
integer 类型的id作为t_student表的主键;
主键字段默认就包含了not null,和unique两个约束。 说明:如果想让主键自动增长(必须是integer类型),应该增加autoincrementcreate table t_student (id integer primary key atoincrement,name text , age integer);
转载地址:http://upwin.baihongyu.com/