SQLite3操作(精简)
SQL语句 SQL语句的特点 不区分大小写(比如数据库认为user和UsEr是一样的) 每条语句都必须以分号 ;结尾
SQL中的常用关键字有 select、insert、update、delete、from、create、where、desc、order、by、group、table、alter、view、index等等
数据库中不可以使用关键字来命名表、字段
SQL语句的种类 数据定义语句(DDL:Data Definition Language) 包括create和drop等操作 在数据库中创建新表或删除表(create table或drop table)
数据操作语句(DML:Data Manipulation Language) 包括insert、update、delete等操作 上面的3种操作分别用于添加、修改、删除表中的数据
数据查询语句(DQL:Data Query Language) 可以用于查询获得表中的数据 关键字select是DQL(也是所有SQL)用得最多的操作 其他DQL常用的关键字有where,order by,group by和having
DDL
创表 格式 create table 表名 (字段名1 字段类型1,字段名2 字段类型2,…) ; create table if not exists表名 (字段名1 字段类型1,…) ; 示例 create table t_student (idinteger,name text,ageinetger,score real);
删表 格式 drop table 表名 ; drop table if exists 表名 ; 示例 drop table t_student ;
DML
插入数据 格式 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 表名 ; 示例 delete from t_student ; 注意 上面的示例会将t_student表中所有记录都删掉
条件语句
如果只想更新或者删除某些固定的记录,那就必须在DML语句后加上一些条件 条件语句的常见格式 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的记录,年龄都改为 5 update t_student set age= 5 where age > 10 and name!= ‘jack’ ;
删除t_student表中年龄小于等于10 或者 年龄大于30的记录 delete from t_studentwhere age <= 10 or age> 30 ;
猜猜下面语句的作用 update t_student set score= age where name =‘jack’ ; 将t_student表中名字等于haha的记录,height字段的值 都改为 age字段的值
如何用Xcode瞬间随机生成100条记录
然后双击/Users/aplle/Desktop/student.sql
DQL 语句 格式 select 字段1,…from 表名 ; select * from 表名; // 查询所有的字段 示例 select name,agefrom t_student ; select * from t_student ; select * from t_student where age > 10 ; // 条件查询
计算记录数量 格式 selectcount (字段) from 表名 ; selectcount ( * ) from 表名 ; 示例 select count (age)from t_student ; select count ( * )from t_student where score>=60;
排序
查询出来的结果可以用order by进行排序 select* from t_student orderby 字段 ; select*from t_studentorder byage ;
默认是按照升序排序(由小到大),也可以变为降序(由大到小) select* from t_student orderby age desc ; //降序 select*from t_studentorder byageasc ;// 升序(默认)
也可以用多个字段进行排序 select * from t_studentorder by age asc,heightdesc ; 先按照年龄排序(升序),年龄相等就按照身高排序(降序)
Limit
使用limit可以精确地控制查询结果的数量,比如每次只查询10条数据 格式 select * from 表名 limit 数值1,数值2 ; 示例 select * from t_studentlimit 4,8 ; 可以理解为:跳过最前面4条语句,然后取8条记录
limit常用来做分页查询,比如每页固定显示5条数据,那么应该这样取数据 第1页:limit 0,5 第2页:limit 5,5 第3页: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 (idinteger,name text not null unique,age integernot null default 1) ; name字段不能为null,并且唯一 age字段不能为null,并且默认为1
主键设计原则 主键应当是对用户没有意义的
永远也不要更新主键
主键不应包含动态变化的数据
主键应当由计算机自动生成
主键的声明 在创表的时候用primary key声明一个主键 create table t_student (idinteger primary key,nametext,age integer) ; integer类型的id作为t_student表的主键 主键字段 只要声明为primary key,就说明是一个主键字段 主键字段默认就包含了not null 和 unique 两个约束 如果想要让主键自动增长(必须是integer类型),应该增加autoincrement create table t_student (idinteger primary key autoincrement,age integer) ;
外键的约束 利用外键约束可以用来建立表与表之间的联系 外键的一般情况是:一张表的某个字段,引用着另一张表的主键字段 新建一个外键 create table t_student (idinteger primary key autoincrement,age integer,class_idinteger,constraintfk_t_student_class_id_t_class_idforeignkey (class_id)(id)) ; references t_class t_student表中有一个叫做fk_t_student_class_id_t_class_id的外键 这个外键的作用是用t_student表中的class_id字段引用t_class表的id字段
表连接查询
什么是表连接查询 需要联合多张表才能查到想要的数据 表连接的类型 内连接:inner join 或者 join (显示的是左右表都有完整字段值的记录) 左外连接:left outer join (保证左表数据的完整性) 示例 查询0316iOS班的所有学生 select s.name,s.agefrom t_student s,t_class c where s.class_id = c.id and c.name = ‘0316iOS’;
的萨芬 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |