內(nèi)容來(lái)自《The Definitive Guide to SQLite》
.echo on 在屏幕上打印執(zhí)行的SQL語(yǔ)句
.mode column 以列模式顯示結(jié)果
.headers on 包含列名稱(chēng)
.nullvalue NULL 將nulls打印成NULL
.width 設(shè)置輸出結(jié)果中各個(gè)列的寬度
SQL不區(qū)分關(guān)鍵字和標(biāo)示符大小寫(xiě)的,默認(rèn)的字符常量值是大小寫(xiě)敏感的,例如,‘Mike' 和 ’mike' 是不同的。
注釋:-- 或者 /**/
創(chuàng)建表
可以用如下定義的create table命令創(chuàng)建表:
create [temp] table table_name (column_definitions [, constrains]);
temp或temporary關(guān)鍵字表示聲明的表是臨時(shí)表。臨時(shí)表只存活于當(dāng)前回話(huà),一段斷開(kāi),就會(huì)就自動(dòng)銷(xiāo)毀。 table_name表示表名 column_definitions由用逗號(hào)分隔的字段列表組成,每個(gè)字段定義包括一個(gè)名詞、一個(gè)域和一個(gè)逗號(hào)分隔的字段約束。 例如:
[sql] view plaincopy print?
sqlite>?create ?table ?contacts?(id?integer ?primary ?key ,?? ???...>?name ?text?not ?null ?collate ?nocase,?? ???...>?phone?text?not ?null ?default ?'UNKNOWN' ,?? ???...>?unique ?(name ,phone));??
sqlite> create table contacts (id integer primary key,...> name text not null collate nocase,...> phone text not null default 'UNKNOWN',...> unique (name,phone));
字段id聲明為integer型,限制為主鍵,該字段是自增長(zhǎng)字段 字段name聲明為text類(lèi)型,約束不能為null,并且排序不區(qū)分大小寫(xiě)。 字段phone是text類(lèi)型 表一級(jí)的約束是unique,定義在字段name和phone上。 修改表
alter table命令改變表的部分結(jié)構(gòu)。alter table命令既可以改變表名,也可以增加字段。格式為:
alter table table {rename to name | add column column_def}
{}表示必須從各選項(xiàng)中選擇一個(gè):
alter table table rename... 重命名表 alter table table add column... 添加列
[sql] view plaincopy print?
sqlite>?alter ?table ?contacts?? ???...>?add ?column ?email?text?not ?null ?default ?'' ?collate ?nocase;?? sqlite>?.schema ?contacts?? CREATE ?TABLE ?contacts?(id?integer ?primary ?key ,??name ?text?not ?null ?collate ?nocase,??phone?text?not ?null ?default ?'UNKNOWN' ,?email?text?not ?null ?default ?'' ?collate ?nocase,?? unique ?(name ,phone));??
sqlite> alter table contacts...> add column email text not null default '' collate nocase;
sqlite> .schema contacts
CREATE TABLE contacts (id integer primary key,
name text not null collate nocase,
phone text not null default 'UNKNOWN', email text not null default '' collate nocase,
unique (name,phone));.schema 會(huì)打印出整個(gè)數(shù)據(jù)庫(kù)的模式。
數(shù)據(jù)庫(kù)查詢(xún)
select命令的通用形式如下:
[sql] view plaincopy print?
select ?[distinct ]?heading??from ?tables??where ?predicate??group ?by ?columns??having ?predicate??order ?by ?columns??limit?count ,?offset;??
select [distinct] heading
from tables
where predicate
group by columns
having predicate
order by columns
limit count, offset;
最常見(jiàn)的select命令形式如下:
[sql] view plaincopy print?
select ?heading?from ?tables?where ?predicate;??
select heading from tables where predicate;
[sql] view plaincopy print?
sqlite>?select ?id,name ?from ?food_types;??
sqlite> select id,name from food_types;
[sql] view plaincopy print?
sqlite>?select ?*?from ?food_types;??
sqlite> select * from food_types;星號(hào)(*)表示所有的列。
過(guò)濾where
[sql] view plaincopy print?
sqlite>?select ?*?from ?foods?where ?name ?=?'JujyFruit' ?and ?type_id?=?9;?? id??????????type_id?????name ???????? ?? 244?????????9???????????JujyFruit???
sqlite> select * from foods where name = 'JujyFruit' and type_id = 9;
id type_id name
---------- ---------- ----------
244 9 JujyFruit
LIKE與GLOB操作符
LIKE的作用與相等(=)類(lèi)似,都是通過(guò)一個(gè)模式來(lái)進(jìn)行字符串匹配的。%可以任意0個(gè)或多個(gè)字符匹配,下劃線(xiàn)(_)可以與任何單個(gè)字符匹配。
[sql] view plaincopy print?
sqlite>?select ?id,?name ?from ?foods?where ?name ?like ?'%ac%P%' and ?name ?not ?like ?'%Sch%' ;?? id??????????name ?????????????????? ?? 38??????????Pie?(Blackberry)?Pie?? 127?????????Guacamole?Dip????????? 198?????????Macinaw?peaches???????
sqlite> select id, name from foods where name like '%ac%P%'and name not like '%Sch%';
id name
---------- --------------------
38 Pie (Blackberry) Pie
127 Guacamole Dip
198 Macinaw peaches
GLOB操作符在行為上與LIKE操作符非常相似。它會(huì)使用文件名替換相關(guān)的通配符,例如*和_,并且是大小寫(xiě)敏感的。例如:
[sql] view plaincopy print?
sqlite>?select ?id,?name ?from ?foods??? ???...>?where ?name ?glob?'Pine*' ;?? id??????????name ???????? ?? 205?????????Pineapple??? 258?????????Pineapple???
sqlite> select id, name from foods ...> where name glob 'Pine*';
id name
---------- ----------
205 Pineapple
258 Pineapple
限定和排序
limit指定返回記錄的最大數(shù)量,offset指定偏移的記錄數(shù)。order by排序,以逗號(hào)分隔的一系列字段,每個(gè)字段項(xiàng)都可能配合排序方向——asc(默認(rèn)的升序)或desc(降序),例如:
[sql] view plaincopy print?
sqlite>?select ?*?from ?foods?where ?name ?like ?'B%' ?? ???...>?order ?by ?type_id?desc ,?name ?limit?10;?? id??????????type_id?????name ????????? ?? 382?????????15??????????Baked?Beans?? 383?????????15??????????Baked?Potat?? 384?????????15??????????Big?Salad???? 385?????????15??????????Brocolli????? 362?????????14??????????Bouillabais?? 328?????????12??????????BLT?????????? 327?????????12??????????Bacon?Club??? 326?????????12??????????Bologna?????? 329?????????12??????????Brisket?San?? 274?????????10??????????Bacon???
sqlite> select * from foods where name like 'B%'...> order by type_id desc, name limit 10;
id type_id name
---------- ---------- -----------
382 15 Baked Beans
383 15 Baked Potat
384 15 Big Salad
385 15 Brocolli
362 14 Bouillabais
328 12 BLT
327 12 Bacon Club
326 12 Bologna
329 12 Brisket San
274 10 Bacon
limit和offset一起使用時(shí),可以用逗號(hào)替代offset關(guān)鍵字,例如:
[sql] view plaincopy print?
sqlite>?select ?*?from ?foods?where ?name ?like ?'B%' order ?by ?type_id?desc ,?name ?limit?1?offset?2;??
sqlite> select * from foods where name like 'B%'order by type_id desc, name limit 1 offset 2;等同于:
[sql] view plaincopy print?
sqlite>?select ?*?from ?foods?where ?name ?like ?'B%' order ?by ?type_id?desc ,?name ?limit?2,1;??
sqlite> select * from foods where name like 'B%'order by type_id desc, name limit 2,1;
函數(shù)(Function)和聚合(Aggressive)
abs()計(jì)算絕對(duì)值,upper()將字符串的值轉(zhuǎn)化為大寫(xiě),lower()將字符串的值轉(zhuǎn)化為小寫(xiě),length()計(jì)算長(zhǎng)度。
[sql] view plaincopy print?
sqlite>?select ?upper ('hello?newman' ),length('hello?newman' ),abs (-12);?? upper ('hello?newman' )??length('hello?newman' )??abs (-12)?????? HELLO?NEWMAN???????????12??????????????????????12??????????
sqlite> select upper('hello newman'),length('hello newman'),abs(-12);
upper('hello newman') length('hello newman') abs(-12)
--------------------- ---------------------- ----------
HELLO NEWMAN 12 12
聚合是一類(lèi)特殊的函數(shù),它從一組記錄中計(jì)算聚合值。標(biāo)準(zhǔn)的聚合函數(shù)包括sum(),avg(),count(),min()和max().例如,要得到type_id = 1的數(shù)量,可以使用如下語(yǔ)句:
[sql] view plaincopy print?
sqlite>?select ?count (*)?from ?foods?where ?type_id?=?1;?? count (*)?????? 47??????????
sqlite> select count(*) from foods where type_id = 1;
count(*)
----------
47
分組(Grouping)
聚合的主要部分是分組。聚合不只是能夠計(jì)算整個(gè)結(jié)果集的聚合值,還可以把結(jié)果集分成多個(gè)組。使用group by子句,例如,獲取每個(gè)type_id組的記錄數(shù)目:
[sql] view plaincopy print?
sqlite>?select ?type_id,count (*)?from ?foods?group ?by ?type_id;?? type_id?????count (*)???? ?? 1???????????47?????????? 2???????????15?????????? 3???????????23?????????? 4???????????22?????????? 5???????????17?????????? 6???????????4??????????? 7???????????60?????????? 8???????????23?????????? 9???????????61?????????? 10??????????36?????????? 11??????????16?????????? 12??????????23?????????? 13??????????14?????????? 14??????????19?????????? 15??????????32??????????
sqlite> select type_id,count(*) from foods group by type_id;
type_id count(*)
---------- ----------
1 47
2 15
3 23
4 22
5 17
6 4
7 60
8 23
9 61
10 36
11 16
12 23
13 14
14 19
15 32
having是一個(gè)可以應(yīng)用到group by的斷言,它從group by中過(guò)濾組的方式與where子句從from子句中過(guò)濾行的方式相同。where子句的預(yù)測(cè)是針對(duì)單個(gè)行的,而having的斷言是針對(duì)聚合值的。例如:
[sql] view plaincopy print?
sqlite>?select ?type_id,count (*)?from ?foods?group ?by ?type_id?having ?count (*)?<?20;?? type_id?????count (*)???? ?? 2???????????15?????????? 5???????????17?????????? 6???????????4??????????? 11??????????16?????????? 13??????????14?????????? 14??????????19??????????
sqlite> select type_id,count(*) from foods group by type_id having count(*) < 20;
type_id count(*)
---------- ----------
2 15
5 17
6 4
11 16
13 14
14 19 group by和having一起工作可以對(duì)group by有約束。
去掉重復(fù)
distinct處理select的結(jié)果并過(guò)濾其中重復(fù)的行,例如,取得所有不同的type_id的值:
[sql] view plaincopy print?
sqlite>?select ?distinct ?type_id?from ?foods;??
sqlite> select distinct type_id from foods;
多表連接
id是food_types的主鍵,foods.type_id由于這種關(guān)系,稱(chēng)為外鍵,它引用另一個(gè)表的主鍵,這種關(guān)系稱(chēng)為外鍵關(guān)系。
[sql] view plaincopy print?
sqlite>?select ?foods.name ,?food_types.name ?? ???...>?from ?foods,?food_types?? ???...>?where ?foods.type_id?=?food_types.id?limit?10;?? name ????????name ?????????? Bagels??????Bakery?????? Bagels,?ra??Bakery?????? Bavarian?C??Bakery?????? Bear?Claws??Bakery?????? Black?and ???Bakery?????? Bread?(wit??Bakery?????? Butterfing??Bakery?????? Carrot?Cak??Bakery?????? Chips?Ahoy??Bakery?????? Chocolate???Bakery??????
sqlite> select foods.name, food_types.name...> from foods, food_types...> where foods.type_id = food_types.id limit 10;
name name
---------- ----------
Bagels Bakery
Bagels, ra Bakery
Bavarian C Bakery
Bear Claws Bakery
Black and Bakery
Bread (wit Bakery
Butterfing Bakery
Carrot Cak Bakery
Chips Ahoy Bakery
Chocolate Bakery
內(nèi)連接
內(nèi)連接就是通過(guò)表中的兩個(gè)字段進(jìn)行連接。內(nèi)連接使用關(guān)系代數(shù)的另一種集合操作,稱(chēng)為交叉。
[sql] view plaincopy print?
sqlite>?select ?*?from ?foods?inner ?join ?food_types?on ?foods.id?=?food_types.id;?? id??????????type_id?????name ????????id??????????name ???????? ?? 1???????????1???????????Bagels??????1???????????Bakery?????? 2???????????1???????????Bagels,?ra??2???????????Cereal?????? 3???????????1???????????Bavarian?C??3???????????Chicken/Fo?? 4???????????1???????????Bear?Claws??4???????????Condiments?? 5???????????1???????????Black?and ???5???????????Dairy??????? 6???????????1???????????Bread?(wit??6???????????Dip????????? 7???????????1???????????Butterfing??7???????????Drinks??????
sqlite> select * from foods inner join food_types on foods.id = food_types.id;
id type_id name id name
---------- ---------- ---------- ---------- ----------
1 1 Bagels 1 Bakery
2 1 Bagels, ra 2 Cereal
3 1 Bavarian C 3 Chicken/Fo
4 1 Bear Claws 4 Condiments
5 1 Black and 5 Dairy
6 1 Bread (wit 6 Dip
7 1 Butterfing 7 Drinks
交叉連接
[sql] view plaincopy print?
sqlite>?select ?*?from ?foods,?food_types;?? id??????????type_id?????name ????????id??????????name ???????? ?? 1???????????1???????????Bagels??????1???????????Bakery?????? 1???????????1???????????Bagels??????2???????????Cereal?????? 1???????????1???????????Bagels??????3???????????Chicken/Fo?? 1???????????1???????????Bagels??????4???????????Condiments?? 1???????????1???????????Bagels??????5???????????Dairy??????? 1???????????1???????????Bagels??????6???????????Dip????????? 1???????????1???????????Bagels??????7???????????Drinks?????? 1???????????1???????????Bagels??????8???????????Fruit????<span?style="font-family:?Arial,?Helvetica,?sans-serif;" >?</span>??
sqlite> select * from foods, food_types;
id type_id name id name
---------- ---------- ---------- ---------- ----------
1 1 Bagels 1 Bakery
1 1 Bagels 2 Cereal
1 1 Bagels 3 Chicken/Fo
1 1 Bagels 4 Condiments
1 1 Bagels 5 Dairy
1 1 Bagels 6 Dip
1 1 Bagels 7 Drinks
1 1 Bagels 8 Fruit ?<span style="font-family: Arial, Helvetica, sans-serif;"> </span>
外連接
例如:
[sql] view plaincopy print?
sqlite>?select ?*?from ?foods?left ?outer ?join ?foods_episodes?on ?foods.id?=?foods_episodes.food_id;?? id??????????type_id?????name ????????food_id?????episode_id?? ?? 1???????????1???????????Bagels??????1???????????165????????? 2???????????1???????????Bagels,?ra??2???????????154????????? 2???????????1???????????Bagels,?ra??2???????????165????????? 3???????????1???????????Bavarian?C??3???????????115????????? 4???????????1???????????Bear?Claws??4???????????67??????
sqlite> select * from foods left outer join foods_episodes on foods.id = foods_episodes.food_id;
id type_id name food_id episode_id
---------- ---------- ---------- ---------- ----------
1 1 Bagels 1 165
2 1 Bagels, ra 2 154
2 1 Bagels, ra 2 165
3 1 Bavarian C 3 115
4 1 Bear Claws 4 67 foods是其中的左表,左外連接試圖將foods中的所有行與foods_episodes的所有行進(jìn)行連接關(guān)系(foods.id = foods_episodes.food_id)的匹配,所有匹配的行都包含在結(jié)果集中。
名稱(chēng)和別名
[sql] view plaincopy print?
sqlite>?select ?foods.name ,?food_types.name ?? ???...>?from ?foods,?food_types?? ???...>?where ?foods.type_id?=?food_types.id?? ???...>?limit?10;??
sqlite> select foods.name, food_types.name...> from foods, food_types...> where foods.type_id = food_types.id...> limit 10;
等價(jià)于
[sql] view plaincopy print?
sqlite>?select ?f.name ,?t.name ?? ???...>?from ?foods?f,?food_types?t?? ???...>?where ?f.type_id?=?t.id?? ???...>?limit?10;??
sqlite> select f.name, t.name...> from foods f, food_types t...> where f.type_id = t.id...> limit 10;
子查詢(xún)
子查詢(xún)最常用的地方是where子句,特別是在in操作符中。
[sql] view plaincopy print?
sqlite>?select ?count (*)?? ???...>?from ?foods?? ???...>?where ?type_id??in ??? ???...>?(select ?id?? ???...>?from ?food_types?? ???...>?where ?name ?=?'Bakery' ?or ?name ?=?'Cereal' );??
sqlite> select count(*)...> from foods...> where type_id in ...> (select id...> from food_types...> where name = 'Bakery' or name = 'Cereal');
總結(jié)
以上是生活随笔 為你收集整理的SQLite中的SQL 的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。