iOS開發(fā) - SQLite學(xué)習(xí)筆記

SQLite 簡(jiǎn)單介紹

SQLite,是一款輕型的數(shù)據(jù)庫(kù),是遵守ACID的關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng),它的設(shè)計(jì)目標(biāo)是嵌入式的,而且目前已經(jīng)在很多嵌入式產(chǎn)品中使用了它,它占用資源非常的低,在嵌入式設(shè)備中,可能只需要幾百K的內(nèi)存就夠了。

它能夠支持Windows/Linux/Unix等等主流的操作系統(tǒng),同時(shí)能夠跟很多程序語(yǔ)言相結(jié)合,比如 Tcl、C#、PHP、Java等,還有ODBC接口,同樣比起Mysql、PostgreSQL這兩款開源的世界著名數(shù)據(jù)庫(kù)管理系統(tǒng)來(lái)講,它的處理速度比他們都快。

SQLite第一個(gè)Alpha版本誕生于2000年5月。 至今已經(jīng)有14個(gè)年頭,SQLite也迎來(lái)了一個(gè)版本 SQLite 3已經(jīng)發(fā)布。


以下是簡(jiǎn)單整理的資料,方便自己和大家查詢:

一、數(shù)據(jù)庫(kù)語(yǔ)句種類

1,數(shù)據(jù)定義語(yǔ)句(DDL:Data Definition Language)
包括create和drop等操作
在數(shù)據(jù)庫(kù)中創(chuàng)建新表或刪除表(create table或 drop table)

2,數(shù)據(jù)操作語(yǔ)句(DML:Data Manipulation Language)
包括insert、update、delete等操作
上面的3種操作分別用于添加、修改、刪除表中的數(shù)據(jù)

3,數(shù)據(jù)查詢語(yǔ)句(DQL:Data Query Language)
可以用于查詢獲得表中的數(shù)據(jù)
關(guān)鍵字select是DQL(也是所有SQL)用得最多的操作
其他DQL常用的關(guān)鍵字有where,order by,group by和having

二、數(shù)據(jù)庫(kù)字段類型

SQLite將數(shù)據(jù)劃分為以下幾種存儲(chǔ)類型:
integer : 整型值
real : 浮點(diǎn)值
text : 文本字符串
blob : 二進(jìn)制數(shù)據(jù)(比如文件)

實(shí)際上SQLite是無(wú)類型的
就算聲明為integer類型,還是能存儲(chǔ)字符串文本(主鍵除外)
建表時(shí)聲明啥類型或者不聲明類型都可以,也就意味著創(chuàng)表語(yǔ)句可以這么寫:
create table t_student(name, age);

為了保持良好的編程規(guī)范、方便程序員之間的交流,編寫建表語(yǔ)句的時(shí)候最好加上每個(gè)字段的具體類型

二、SQL基本語(yǔ)句

**創(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 inetger, score real) ;

刪表

格式
drop table 表名 ;
drop table if exists 表名 ;

格式
insert into 表名 (字段1, 字段2, …) values (字段1的值, 字段2的值, …) ;
示例
insert into t_student (name, age) values (‘mj’, 10) ;

格式
delete from 表名 ;
示例
delete from t_student ;

格式
update 表名 set 字段1 = 字段1的值, 字段2 = 字段2的值, … ;
示例
update t_student set name = ‘jack’, age = 20 ;

格式
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 表名 別名 ;

示例
select name myname, age myage from t_student ;
給name起個(gè)叫做myname的別名,給age起個(gè)叫做myage的別名

select s.name, s.age from t_student s ;
給t_student表起個(gè)別名叫做s,利用s來(lái)引用表中的字段

數(shù)量

格式
select count (字段) from 表名 ;
select count ( * ) from 表名 ;

示例
select count (age) from t_student ;
select count ( * ) from t_student where score >= 60;

排序

查詢出來(lái)的結(jié)果可以用order by進(jìn)行排序
select * from t_student order by 字段 ;
select * from t_student order by age ;

默認(rèn)是按照升序排序(由小到大),也可以變?yōu)榻敌颍ㄓ纱蟮叫。?select * from t_student order by age desc ;  //降序
select * from t_student order by age asc ;   // 升序(默認(rèn))

也可以用多個(gè)字段進(jìn)行排序
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 ;
可以理解為:跳過(guò)最前面4條語(yǔ)句,然后取8條記錄
limit常用來(lái)做分頁(yè)查詢,比如每頁(yè)固定顯示5條數(shù)據(jù),那么應(yīng)該這樣取數(shù)據(jù)
第1頁(yè):limit 0, 5
第2頁(yè):limit 5, 5
第3頁(yè):limit 10, 5
第n頁(yè):limit 5*(n-1), 5

三、SQL其它語(yǔ)句

約束

建表時(shí)可以給特定的字段設(shè)置一些約束條件,常見的約束有

not null :規(guī)定字段的值不能為null
unique :規(guī)定字段的值必須唯一
default :指定字段的默認(rèn)值

建議:盡量給字段設(shè)定嚴(yán)格的約束,以保證數(shù)據(jù)的規(guī)范性

示例
create table t_student (id integer, name text not null unique, age integer not null default 1) ;
name字段不能為null,并且唯一
age字段不能為null,并且默認(rèn)為1

主鍵

在創(chuàng)表的時(shí)候用primary key聲明一個(gè)主鍵
create table t_student (id integer primary key, name text, age integer) ;
integer類型的id作為t_student表的主鍵

----主鍵字段----
只要聲明為primary key,就說(shuō)明是一個(gè)主鍵字段
主鍵字段默認(rèn)就包含了not null 和 unique 兩個(gè)約束
如果想要讓主鍵自動(dòng)增長(zhǎng)(必須是integer類型),應(yīng)該增加autoincrement
create table t_student (id integer primary key autoincrement, name text, age integer) ;

外鍵

利用外鍵約束可以用來(lái)建立表與表之間的聯(lián)系
外鍵的一般情況是:一張表的某個(gè)字段,引用著另一張表的主鍵字段。

----新建一個(gè)外鍵----
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) (id)) ;references t_class

t_student表中有一個(gè)叫做fk_t_student_class_id_t_class_id的外鍵
這個(gè)外鍵的作用是用t_student表中的class_id字段引用t_class表的id字段

表連接查詢

----什么是表連接查詢----
需要聯(lián)合多張表才能查到想要的數(shù)據(jù)

----表連接的類型----
內(nèi)連接:inner join 或者 join  (顯示的是左右表都有完整字段值的記錄)
左外連接:left outer join (保證左表數(shù)據(jù)的完整性)

----示例----
查詢0316iOS班的所有學(xué)生
select s.name,s.age from t_student s, t_class c where s.class_id = c.id and c.name = ‘0316iOS’;

謝謝,以上整理只為方便自己和大家查詢。


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容