日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

SQLite中的高级SQL

發布時間:2025/3/12 数据库 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SQLite中的高级SQL 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

插入記錄

使用insert插入記錄。insert在表單上工作,使用insert命令可以一次插入一條記錄,使用select命令可以一次插入多條記錄。insert語句的一般格式為:

[sql] view plaincopy print?
  • insert?into?table(column?list)?values(value_list);??
  • insert into table(column list) values(value_list);
    例如:

    [sql] view plaincopy print?
  • insert?into?foods?(name,?type_id)?values?('Cinnamon?Bobka',?1);??
  • insert into foods (name, type_id) values ('Cinnamon Bobka', 1);

    如果在insert語句中為表的所有字段提供值,可以省略字段列表。例如:

    [sql] view plaincopy print?
  • sqlite>?insert?into?foods?values?(NULL,?1,?'Blueberry?Bobka');??
  • sqlite> insert into foods values (NULL, 1, 'Blueberry Bobka');
    插入一組行

    子查詢可以用在insert語句中,既可以作為值列表的一部分,也可以完全替代整個值列表。

    [sql] view plaincopy print?
  • sqlite>?insert?into?foods??
  • ???...>?select?last_insert_rowid()+1,?type_id,?name?from?foods??
  • ???...>?where?name?=?'Chocolate?Bobka';??
  • sqlite>?select?*?from?foods?where?name?like?'%Bobka';??
  • id??????????type_id?????name?????????????
  • ----------??----------??---------------??
  • 10??????????1???????????Chocolate?Bobka??
  • 13??????????1???????????Cinnamon?Bobka???
  • 413?????????1???????????Cinnamon?Bobka???
  • 414?????????1???????????Blueberry?Bobka??
  • 415?????????1???????????Chocolate?Bobka??
  • sqlite> insert into foods...> select last_insert_rowid()+1, type_id, name from foods...> where name = 'Chocolate Bobka'; sqlite> select * from foods where name like '%Bobka'; id type_id name ---------- ---------- --------------- 10 1 Chocolate Bobka 13 1 Cinnamon Bobka 413 1 Cinnamon Bobka 414 1 Blueberry Bobka 415 1 Chocolate Bobka
    插入多行

    使用select形式的insert可以一次插入多行,例如:

    [sql] view plaincopy print?
  • sqlite>?create?table?foods2?(id?int,?type_id?int,?name?text);??
  • sqlite>?insert?into?foods2?select?*?from?foods;??
  • sqlite>?select?count(*)?from?foods2;??
  • count(*)????
  • ----------??
  • 415?????????
  • sqlite> create table foods2 (id int, type_id int, name text); sqlite> insert into foods2 select * from foods; sqlite> select count(*) from foods2; count(*) ---------- 415 上述語句表示創建一個新表foods,并插入表foods的所有記錄。

    還有另一種形式:

    [sql] view plaincopy print?
  • sqlite>?create?table?foods3?as?select?*?from?foods;??
  • sqlite> create table foods3 as select * from foods;
    create table與從foods表選擇數據插入表兩步并為一步。這對創建臨時表特別有用:

    [sql] view plaincopy print?
  • sqlite>?select?f.name?food,?t.name?name,??
  • ???...>?(select?count(episode_id)?from?foods_episodes?where?food_id?=?f.id)?episodes??
  • ???...>?from?foods?f,?food_types?t??
  • ???...>?where?f.type_id?=?t.id;??
  • sqlite> select f.name food, t.name name,...> (select count(episode_id) from foods_episodes where food_id = f.id) episodes...> from foods f, food_types t...> where f.type_id = t.id;
    更新記錄

    update命令用于更新表中的記錄,update語句的一般格式為:

    [sql] view plaincopy print?
  • update?table?set?update_list?where?predicate??
  • update table set update_list where predicate

    例如:

    [sql] view plaincopy print?
  • sqlite>?insert?into?foods?values?(415,?1,?'Chocolate?Bobka');??
  • Error:?UNIQUE?constraint?failed:?foods.id??
  • sqlite>?update?foods?set?name?=?'CHOCOLATE?BOBKA'??
  • ???...>?where?name?=?'Chocolate?Bobka';??
  • sqlite>?select?*?from?foods?where?name?like?'CHOCOLATE%';??
  • id??????????type_id?????name?????????????
  • ----------??----------??---------------??
  • 10??????????1???????????CHOCOLATE?BOBKA??
  • 11??????????1???????????Chocolate?Eclai??
  • 12??????????1???????????Chocolate?Cream??
  • 222?????????9???????????Chocolates,?box??
  • 223?????????9???????????Chocolate?Chip???
  • 224?????????9???????????Chocolate?Cover??
  • 415?????????1???????????CHOCOLATE?BOBKA??
  • sqlite> insert into foods values (415, 1, 'Chocolate Bobka'); Error: UNIQUE constraint failed: foods.id sqlite> update foods set name = 'CHOCOLATE BOBKA'...> where name = 'Chocolate Bobka'; sqlite> select * from foods where name like 'CHOCOLATE%'; id type_id name ---------- ---------- --------------- 10 1 CHOCOLATE BOBKA 11 1 Chocolate Eclai 12 1 Chocolate Cream 222 9 Chocolates, box 223 9 Chocolate Chip 224 9 Chocolate Cover 415 1 CHOCOLATE BOBKA
    刪除記錄

    使用delete命令可以刪除表中的記錄。一般格式為:

    [sql] view plaincopy print?
  • delete?from?table?where?predicate??
  • delete from table where predicate
    實體完整性

    數據庫的所有域都是唯一的、可以定位的。域要可尋址,它相應的行必須是可以尋址的。因此,行必須在某種方式是唯一的。這就是主鍵的功能。

    主鍵由至少帶有unique約束的一個或一組字段組成。

    唯一性約束

    唯一性(unique)約束是主鍵的基礎。

    [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>?insert?into?contacts?(name,phone)?values?('Jerry',?'UNKNOWN');??
  • sqlite>?insert?into?contacts?(name)?values?('Jerry');??
  • Error:?UNIQUE?constraint?failed:?contacts.name,?contacts.phone??
  • sqlite>?insert?into?contacts?(name,?phone)?values?('Jerry',?'555-1212');??
  • sqlite> create table contacts (...> id integer primary key,...> name text not null collate nocase,...> phone text not null default 'UNKNOWN',...> unique (name, phone)); sqlite> insert into contacts (name,phone) values ('Jerry', 'UNKNOWN'); sqlite> insert into contacts (name) values ('Jerry'); Error: UNIQUE constraint failed: contacts.name, contacts.phone sqlite> insert into contacts (name, phone) values ('Jerry', '555-1212'); 在contacts表中,對name和phone的聯合有唯一性約束。字段值的聯合必須是唯一的。

    NULL和UNIQUE:注意NULL不等于任何值,甚至不等于其他NULL.理論上,您可以放入任意多個NULL。

    主鍵約束

    在SQLite中,不管你有沒有定義主鍵,都有一個64-bit整型字段,稱為rowid。它還有兩個別名——_rowid_和oid。SQLite為主鍵字段提供自動增長特性。如果定義字段類型為integer primary key,SQLite將為該字段創建默認值,該默認值確保整數值是唯一的。實際上,該字段就是rowid的別名。

    新創建的rowid不一定是按照嚴格順序增長的。如果想要SQLite使用唯一的自動主鍵值,可以加入關鍵值autoincrement。

    [sql] view plaincopy print?
  • sqlite>?select?rowid,oid,_rowid_,id,name,?phone?from?contacts;??
  • id??????????id??????????id??????????id??????????name????????phone???????
  • ----------??----------??----------??----------??----------??----------??
  • 1???????????1???????????1???????????1???????????Jerry???????UNKNOWN?????
  • 2???????????2???????????2???????????2???????????Jerry???????555-1212????
  • sqlite> select rowid,oid,_rowid_,id,name, phone from contacts; id id id id name phone ---------- ---------- ---------- ---------- ---------- ---------- 1 1 1 1 Jerry UNKNOWN 2 2 2 2 Jerry 555-1212
    域完整性

    默認值

    default為字段提供一個默認值。例如,contacts的name字段默認值為'UNKNOWN'。

    default還可以接受3中預定義格式的ANSI/ISO保留字,用于生產日期和時間值。current_time格式為(HH:MM:SS),current_date格式為(YYYY-MM-DD),current_timestamp會生成一個日期時間的組合,格式為(YYYY-MM-DD HH:MM:SS).例如:

    [sql] view plaincopy print?
  • sqlite>?create?table?times?(id?int,??
  • ???...>?date?not?null?default?current_date,??
  • ???...>?time?not?null?default?current_time,??
  • ???...>?timestamp?not?null?default?current_timestamp);??
  • sqlite>?insert?into?times?(id)?values?(1);??
  • sqlite>?insert?into?times?(id)?values?(2);??
  • sqlite>?select?*?from?times;??
  • id??????????date????????time????????timestamp????????????
  • ----------??----------??----------??-------------------??
  • 1???????????2015-01-25??09:10:01????2015-01-25?09:10:01??
  • 2???????????2015-01-25??09:10:16????2015-01-25?09:10:16??
  • sqlite> create table times (id int,...> date not null default current_date,...> time not null default current_time,...> timestamp not null default current_timestamp); sqlite> insert into times (id) values (1); sqlite> insert into times (id) values (2); sqlite> select * from times; id date time timestamp ---------- ---------- ---------- ------------------- 1 2015-01-25 09:10:01 2015-01-25 09:10:01 2 2015-01-25 09:10:16 2015-01-25 09:10:16
    NOT NULL約束

    NOT NULL約束可以確保字段部位NULL.


    check約束

    check約束允許定義表達式來測試要插入或者更新的字段值。例如,check約束可能確保電話號碼字段的值至少是7位字符長度。

    [sql] view plaincopy print?
  • sqlite>?create?table?contacts2??
  • ???...>?(id?integer?primary?key,??
  • ???...>?name?text?not?null?collate?nocase,??
  • ???...>?phone?text?not?null?default?'UNKNOWN',??
  • ???...>?unique(name,?phone),??
  • ???...>?check?(length(phone)?>=7));??
  • sqlite> create table contacts2...> (id integer primary key,...> name text not null collate nocase,...> phone text not null default 'UNKNOWN',...> unique(name, phone),...> check (length(phone) >=7));
    外鍵約束

    SQLite支持關系理論中的關系完整性概念。關系完整性也叫外鍵,它確保了一個表中的關鍵值必須從另一個表中引用,且該數據必須在另一個表中實際存在。經典的例子就是父子關系、主從關系。SQLite支持在創建表時使用外鍵,可以使用如下語法:

    [sql] view plaincopy print?
  • create?table?table_name??
  • (column_definition?references?foreign_table?(column_name)???
  • on?{delete|update}?integrity_action???
  • [not]?deferrable?[initially?{deferred|immediate},]?...);???
  • create table table_name (column_definition references foreign_table (column_name) on {delete|update} integrity_action [not] deferrable [initially {deferred|immediate},] ...);
    例如,foods表和food_types表當前的定義如下:

    [sql] view plaincopy print?
  • CREATE?TABLE?foods(??
  • ??id?integer?primary?key,??
  • ??type_id?integer,??
  • ??name?text?);??
  • CREATE TABLE foods(id integer primary key,type_id integer,name text ); [sql] view plaincopy print?
  • CREATE?TABLE?food_types(??
  • ??id?integer?primary?key,??
  • ??name?text?);??
  • CREATE TABLE food_types(id integer primary key,name text );
    可以看出foods表使用了type_id字段,引用自表food_types。如果想要引用完整性確保SQLite保護您的數據,確保foods表的type_id值都存在于food_types的id字段,可以使用如下方式定義foods表:

    [sql] view plaincopy print?
  • sqlite>?create?table?foods(??
  • ???...>?id?integer?primary?key,??
  • ???...>?type_id?integer?references?food_types(id)??
  • ???...>?on?delete?restrict??
  • ???...>?deferrable?initially?deferred,??
  • ???...>?name?text);??
  • sqlite> create table foods(...> id integer primary key,...> type_id integer references food_types(id)...> on delete restrict...> deferrable initially deferred,...> name text);
    type_id引用自表food_types的id字段。使用了delete restrict, 該命令告訴SQLite阻止任何這樣的刪除行為,如果從表food_types中刪除某行,將導致foods表的id沒有父id的存在。完整的規則定義如下:

    • set null:如果父值被刪除或者不存在了,剩余的子值將該為null
    • set default:如果父值被刪除或者不存在了,剩余的子值修改為默認值
    • cascade:更新父值時,更新所有匹配的子值。刪除父值時,刪除所有的子值。要特別注意該選項,cascade的刪除功能可能會出現意想不到的效果
    • restrict:更新或者刪除父值可能會出現孤立的子值,從而阻止(終止)事物。
    • no action:使用一種松弛的方法,不干涉操作執行,只是觀察變化。在整個語句(如果約束定義為deferred的事物也一樣)的結尾報出錯誤。
    SQLite支持deferrable子句,該子句控制定義的約束是立即強制實施還是延遲到整個事務結束時。


    排序規則

    排序規則涉及文本值如何比較。三種排序規則:

    • memcmp()逐字比較文本值
    • nocase:非大小寫敏感排序算法
    • reverse:與二進制排序規則相反。reverse更多地用來測試。

    存儲類

    SQLite有5個原始的數據類型。integer,real,text,blob,NULL。SQLite函數typeof()根據值的表示法返回其存儲類。

    [sql] view plaincopy print?
  • sqlite>?select?typeof(3.14),?typeof('3.14'),typeof(314),?typeof(x'3142'),typeof(NULL);??
  • typeof(3.14)??typeof('3.14')??typeof(314)??typeof(x'3142')??typeof(NULL)??
  • ------------??--------------??-----------??---------------??------------??
  • real??????????text????????????integer??????blob?????????????null??????????
  • sqlite> select typeof(3.14), typeof('3.14'),typeof(314), typeof(x'3142'),typeof(NULL); typeof(3.14) typeof('3.14') typeof(314) typeof(x'3142') typeof(NULL) ------------ -------------- ----------- --------------- ------------ real text integer blob null
    視圖

    視圖即虛擬表,也稱為派生表,因為它們的內容都派生自其他表的查詢結果。雖然視圖看起來感覺就像基本表一樣,但是它們不是基本表。基本表的內容是持久的,而視圖的內容是在使用時動態產生的。創建視圖的語法如下:

    [sql] view plaincopy print?
  • create?view?name?as?select-stmt;??
  • create view name as select-stmt; [sql] view plaincopy print?
  • sqlite>?create?view?details?as??
  • ???...>?select?f.name?as?fd,?ft.name?as?tp,?e.name?as?ep,?e.season?as?ssn??
  • ???...>?from?foods?f??
  • ???...>?inner?join?food_types?ft?on?f.type_id?=?ft.id??
  • ???...>?inner?join?foods_episodes?fe?on?f.id?=?fe.food_id??
  • ???...>?inner?join?episodes?e?on?fe.episodes_id?=?e.id;??
  • sqlite> create view details as...> select f.name as fd, ft.name as tp, e.name as ep, e.season as ssn...> from foods f...> inner join food_types ft on f.type_id = ft.id...> inner join foods_episodes fe on f.id = fe.food_id...> inner join episodes e on fe.episodes_id = e.id;
    使用drop view刪除視圖

    [sql] view plaincopy print?
  • drop?view?name;??
  • drop view name;

    索引

    索引是一種用來在某種條件下加速查詢的結構。創建索引的命令如下:

    [sql] view plaincopy print?
  • create?index?[unique]?index_name?on?table_name?(columns)??
  • create index [unique] index_name on table_name (columns)
    如果使用關鍵字unique,將會在索引上添加約束,索引中的所有值必須是唯一的。這不僅適用于索引,也適用于索引所在字段。unqiue關鍵字覆蓋index中的所有字段,不管是聯合值還是單個值:

    [sql] view plaincopy print?
  • sqlite>?create?table?foo?(a?text,?b?text);??
  • sqlite>?create?unique?index?foo_idx?on?foo(a,?b);??
  • sqlite>?insert?into?foo?values?('unique',?'value');??
  • sqlite>?insert?into?foo?values?('unique',?'value');??
  • Error:?UNIQUE?constraint?failed:?foo.a,?foo.b??
  • sqlite> create table foo (a text, b text); sqlite> create unique index foo_idx on foo(a, b); sqlite> insert into foo values ('unique', 'value'); sqlite> insert into foo values ('unique', 'value'); Error: UNIQUE constraint failed: foo.a, foo.b
    若要刪除索引,可使用drop index命令

    [sql] view plaincopy print?
  • drop?index?index_name;??
  • drop index index_name;
    排序規則

    索引中的每個字段都有相關的排序規則。例如,要在foods.name上創建大小寫不敏感的索引,可以使用如下命令:

    [sql] view plaincopy print?
  • create?index?foods_name_idx?on?foods?(name?collate?nocase);??
  • create index foods_name_idx on foods (name collate nocase);
    .indices列出表中索引。也可以使用.schema命令查看更多的信息。


    觸發器

    當具體的表發生特定的數據庫事件時,觸發器執行對應的SQL命令。命令格式如下:

    [sql] view plaincopy print?
  • create?[temp|temporary]?trigger?name?[before|after]?[insert|delete|update|update?of?colums]?on?table?action??
  • create [temp|temporary] trigger name [before|after] [insert|delete|update|update of colums] on table action可以通過關鍵字before或after來制定這些操作是在事件發生前還是發生后執行。事件包括在具體的表中執行的delete、insert和update命令。

    更新觸發器

    與insert和delete觸發器不同,update觸發器可以在表的執行字段上定義,這種觸發器的一般格式如下:

    [sql] view plaincopy print?
  • create?trigger?name?[before|after]?update?of?column?on?table?action??
  • create trigger name [before|after] update of column on table action


    事物

    事務由3個命令控制:begin、commit和rollback.begin開始一個事務,begin之后的所有操作都可以取消,如果連接終止前沒有發出commit,也會被取消。commit提交事物開始后所執行的所有操作。類似地,rollback還原begin之后的所有操作。例如:

    [sql] view plaincopy print?
  • sqlite>?begin;??
  • sqlite>?delete?from?foods;??
  • sqlite>?rollback;??
  • sqlite>?select?count(*)?from?foods;??
  • sqlite> begin; sqlite> delete from foods; sqlite> rollback; sqlite> select count(*) from foods;

    總結

    以上是生活随笔為你收集整理的SQLite中的高级SQL的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。