SQLite学习笔记(一)
发布时间:2020-12-12 19:51:33 所属栏目:百科 来源:网络整理
导读:SQLite学习笔记(一) 今天学习过程中发现数据库并没有想象中那么容易,于是决定一些笔记来巩固一下知识 介绍一些简单SQL语句,同时也是最为重要的基础 SQL (structured query language ):结构化查询语言 该语言虽不区分大小写,但是在阅读方便上操作与变
SQLite学习笔记(一)
今天学习过程中发现数据库并没有想象中那么容易,于是决定一些笔记来巩固一下知识 介绍一些简单SQL语句,同时也是最为重要的基础 SQL (structured query language ):结构化查询语言 该语言虽不区分大小写,但是在阅读方便上操作与变量区分开比较好 我拿一个名为t_student 的表为例 种类:DDL:(data definition language) 包括creat ,drop等操作 创建表: creat table 表名(字段1 字段1类型,字段2 字段2类型。。。。);例如:creat table t_student (id integer,name text,age integer,score real); integer:整型 real:浮点型 text:文本字符串 blob:二进制数据 实际上SQLite是无类型的,即使声明为integer类型也可以作为字符串存储(主键除外) 所以create table t_student(name ,age);也可以,但是不推荐这么写 一般声名格式为:creat table if not exists t_student(id integer,name text,。。。); if not exists :正如字面意思,如果检测到该表不存在就创建该表。。。 倘若你要将id设置为主键;就在id integer后面加上 primary key autoincrement 其中 autoincrement 是自动赋值,就像1 ,2,3,4.。。。( autoincrement 修饰的主键必须为 integer类型),因为主键不能有重复的出现,加上 autoincrement 就省去手动赋值了 主键: 主键应当是对用户没有意义的; 永远不要更新主键; 主键不应该包含动态变化的数据; 主键应当由计算机自动生成; create table t_student(id integer primary key autoincrement,name text not null unique, age integer not null default 1); id为主键自动生成;name不为空且唯一(重复会报错);age不为空且默认值为1(没有值的时候) not null:规定字段不能为null unique:指定字段必须唯一 default:指定字段默认值 删除表:drop table if exists表名; 这个没什么好说的if exists 写上比较好 DML:(data manipulation language) 包括 insert update delete等 插入表: insert into t_student (id,name,age。。。)values(1,‘jack’,20,100。。。); 成员与成员的值应该相符合 更新表: update t_student set id = 10,name = ‘hb’,。。。; 没什么好说的,就一个set,想怎么改就怎么改 删除表: delete from 表名 ; 一删除表就全都没了 delete from t_student where age<=10 or age>30;//描述:删除age小于等于10或大于30的成员 不得不介绍条件语句: where 字段 = 某值; 等同于 where is 某值; where != 某值; 等同于 where is not 某值; where > 某值; where 字段 > 某值 and 字段 > 某值; where or 字段 > 某值; DQL:(data query language )包括select 我要说select分量最重!也使用的最多的 select 字段1 , 字段2,。。。 from 表名;//表示从该表中查询字段1,字段2,。。。 select *from表名;//这里的*表示查询所有字段 select *from t_student where age >10;//查询age>10的成员 select age sage,name sname from t_student where age>10 and….; 看不懂 age sage的人我告诉你 ,这个其实是age as sage的简写,只是为了输出age这个列的值的时候,显示的不是age,而是别名sage (表明也可以起别名 同样可以简写,不要因为这种简写就看不懂了) select count(*) from t_student;//查询表中的所有的成员数量 *也可以改为 age,意思是查有age的成员数量 select count(*) from t_student where age>10;//同样可以加条件 select *from t_student where age >10 order by name asc; 这句我得讲清楚;name作为字符串类型同样可以作为排序的依据; order by 也是条件约束的一种,它与 asc配合,表示根据name的顺序从低到高升序排序;反之,想要降序排列就将 asc改为 desc 建议自己动手练一下 select *from t_student limit 4,8;//跳过最前面的4个成员,查找后面的8个成员 select *from t_student limit 4;//取前4个成员 相当于 limit 0,4; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |