SQLite 介紹
- 是一種結(jié)構(gòu)化查詢語句
- 對關(guān)系型數(shù)據(jù)庫中的數(shù)據(jù)進行定義和操作的語言
- 語言簡潔冈钦、語法簡單糖耸、好學好用
SQLite 語句
特點
- 不區(qū)分大小寫
- 每條語句都必須以分號 扎拣; 結(jié)尾
常用關(guān)鍵字
select桃焕、insert瓶盛、update惯疙、delete钱床、from荚斯、create、where、desc鲸拥、 order拐格、by、group刑赶、table捏浊、alter、view撞叨、index
- 數(shù)據(jù)庫中不允許使用關(guān)鍵字來命名表金踪、字段
種類
- 數(shù)據(jù)定義語句(DDL)
- create 和 drop 等草族
- 在數(shù)據(jù)庫中創(chuàng)建新表或刪除表 - 數(shù)據(jù)操作語句(DML)
- insert、update牵敷、delete 等操作
- 分別用于添加胡岔、修改、刪除表中的數(shù)據(jù) - 數(shù)據(jù)查詢語句(DQL)
- 查詢獲取表中的數(shù)據(jù)
- select枷餐、where靶瘸、order by、group by毛肋、having
創(chuàng)建表
- 格式:
- create table 表名(字段名1 字段類型1怨咪,字段名2 字段類型2)
- create table if not exists 表名(字段名1 字段類型1,字段名2 字段類型2) - 示例
- create table t_student(id integer润匙,name text诗眨, age integer,score real)孕讳;
字段類型
integer : 整型
real :浮點值
text : 文本字符串
blob : 二進制數(shù)據(jù)(文件)
實際上SQL是無類型的
就算聲明為 integer 類型牵舱,還是能存儲字符串文本(主鍵除外)
建表時聲明啥類型或不聲明類型都可以
- create table t_student(name锹引,age)
- 為了保持編程規(guī)范击罪,方便程序員之間的交流痒给,編寫建表語句時最好加上每個字段的具體類型
刪表
- 格式:
- drop table 表名;
- drop table if exists 表名璃饱; - 示例
- drop table t_student与斤;
插入數(shù)據(jù)
- 格式:
- insert into 表名(字段1,字段2帜平,...)values(字段1的值, 字段2的值, …); - 示例
- insert into t_student(name梅鹦,age)values(‘sajiner’裆甩, 10); - 注意: 數(shù)據(jù)庫中的字符串內(nèi)容應(yīng)該用單引號‘括住
更新數(shù)據(jù)
- 格式:
- update 表名 set 字段1 = 字段1的值, 字段2 = 字段2的值, … ; - 示例
- update t_student set name = ‘jack’齐唆, age = 20嗤栓; - 注意: 上面的示例會將t_student表中所有記錄的name都改為jack,age改為20
刪除數(shù)據(jù)
- 格式:
- delete from 表名; - 示例
- delete from t_student茉帅; - 注意: 上面的示例會講表中所有記錄都刪除
條件語句
- 如果只想更新或刪除某些固定的記錄叨叙,需要在DML后加入一些條件
- 常見格式
- where 字段 = 某個值;
- where 字段 is 某個值堪澎;
- where 字段 != 某個值擂错;
- where 字段 is not 某個值;
- where 字段 > 某個值樱蛤;
- where 字段1 = 某個值 and 字段2 > 某個值钮呀;
- where 字段1 = 某個值 or 字段2 = 某個值; - 示例1
- 將t_student表中年齡大于10并且姓名不等于jack的記錄昨凡,年齡都改為5
- update t_student set age = 5 where age > 10 and name != 'jack'; - 示例 2
- 刪除表中年齡小于等于10 或者年齡大于30的記錄
- delete from t_student where age <= 10 or age > 30; - 示例 3
- 將t_student表中名字等于jack的記錄爽醋,score字段的值 都改為 age字段的值
- update t_student set score = age where name = 'jack';
DQL 語句
- 格式:
- select 字段1,字段2便脊,...from 表名蚂四;
- select * from 表名; // 查詢所有的字段 - 示例
- select name哪痰,age from t_student遂赠;
- select * from t_student ;
- select * from t_student where age > 10 ; // 條件查詢
起別名
- 格式:
- select 字段1 別名 , 字段2 別名 , … from 表名 別名 ;
- select 字段1 別名, 字段2 as 別名, … from 表名 as 別名 ;
- select 別名.字段1, 別名.字段2, … from 表名 別名 ; - 示例1
- select name myname, age myage from t_student ;
- 給name起個叫做myname的別名,給age起個叫做myage的別名 - 示例2
- select s.name, s.age from t_student s ;
- 給t_student表起個別名叫做s妒御,利用s來引用表中的字段
計算記錄的數(shù)量
- 格式:
- 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 ; - 默認是按照升序排序(由小到大)解愤,也可以變?yōu)榻敌颍ㄓ纱蟮叫。?br>
- 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
- 使用limit可以精確地控制查詢結(jié)果的數(shù)量送讲,比如每次只查詢10條數(shù)據(jù)
- 格式:
- select * from 表名 limit 數(shù)值1, 數(shù)值2 ; - 示例
- select * from t_student limit 4, 8 ;
- 可以理解為:跳過最前面4條語句,然后取8條記錄
limit常用來做分頁查詢惋啃,
- 比如每頁固定顯示5條數(shù)據(jù)哼鬓,那么應(yīng)該這樣取數(shù)據(jù)
- 第1頁:limit 0, 5
- 第2頁:limit 5, 5
- 第n頁:limit 5*(n-1), 5 - 示例
- select * from t_student limit 7 ;
- 相當于select * from t_student limit 0, 7 ;
- 表示取最前面的7條記錄
簡單約束
- 建表時可以給特定的字段設(shè)置一些約束條件,常見的約束有
- not null :規(guī)定字段的值不能為null
- unique :規(guī)定字段的值必須唯一
- default :指定字段的默認值- 建議:盡量給字段設(shè)定嚴格的約束边灭,以保證數(shù)據(jù)的規(guī)范性
- 示例
- create table t_student (id integer, name text not null unique, age integer not null default 1) ;
- name字段不能為null异希,并且唯一;age字段不能為null绒瘦,并且默認為1
主鍵約束
- 每張表都必須有一個主鍵称簿,用來標識記錄的唯一性
- 什么是主鍵
- 主鍵(Primary Key,簡稱PK)用來唯一地標識某一條記錄
- 例如t_student可以增加一個id字段作為主鍵惰帽,相當于人的身份證
- 主鍵可以是一個字段或多個字段 - 設(shè)計原則
- 主鍵應(yīng)當是對用戶沒有意義的
- 永遠也不要更新主鍵
- 主鍵不應(yīng)包含動態(tài)變化的數(shù)據(jù)
- 主鍵應(yīng)當由計算機自動生成 - 聲明
- 在創(chuàng)表的時候用primary key聲明一個主鍵
- create table t_student (id integer primary key, name text, age integer) ; - 主鍵字段
- 只要聲明為primary key憨降,就說明是一個主鍵字段
- 主鍵字段默認就包含了not null 和 unique 兩個約束 - 如果想要讓主鍵自動增長(必須是integer類型),應(yīng)該增加autoincrement
- create table t_student (id integer primary key autoincrement, name text, age integer) ;
外鍵約束
- 利用外鍵約束可以用來建立表與表之間的聯(lián)系
- 外鍵的一般情況是:一張表的某個字段该酗,引用著另一張表的主鍵字段
- 新建一個外鍵
- create table t_student (id integer primary key autoincrement, name text, age integer, class_id integer, constraint fk_t_student_class_id_t_class_id foreign key (class_id) references t_class (id)) ;
- t_student表中有一個叫做fk_t_student_class_id_t_class_id的外鍵
這個外鍵的作用是用t_student表中的class_id字段引用t_class表的id字段
表連接查詢
- 什么是表連接查詢
- 需要聯(lián)合多張表才能查到想要的數(shù)據(jù) - 表連接的類型
- 內(nèi)連接:inner join 或者 join (顯示的是左右表都有完整字段值的記錄)
- 左外連接:left outer join (保證左表數(shù)據(jù)的完整性) - 示例
- 查詢0316iOS班的所有學生
- select s.name,s.age from t_student s, t_class c where s.class_id = c.id and c.name = ‘0316iOS’;