一、創(chuàng)建數(shù)據(jù)表:
該命令的語法規(guī)則和使用方式與大多數(shù)關(guān)系型數(shù)據(jù)庫基本相同,因此我們還是以示例的方式來演示SQLite中創(chuàng)建表的各種規(guī)則。但是對(duì)于一些SQLite特有的規(guī)則,我們會(huì)給予額外的說明。注:以下所有示例均是在sqlite自帶命令行工具中完成的。
1). 最簡(jiǎn)單的數(shù)據(jù)表:
sqlite> CREATE TABLE testtable (first_col integer);
這里需要說明的是,對(duì)于自定義數(shù)據(jù)表表名,如testtable,不能以sqlite_開頭,因?yàn)橐栽撉熬Y定義的表名都用于sqlite內(nèi)部。
2). 創(chuàng)建帶有缺省值的數(shù)據(jù)表:
sqlite> CREATE TABLE testtable (first_col integer DEFAULT 0, second_col varchar DEFAULT 'hello');
3). 在指定數(shù)據(jù)庫創(chuàng)建表:
sqlite> ATTACH DATABASE 'd:/mydb.db' AS mydb;
sqlite> CREATE TABLE mydb.testtable (first_col integer);
這里先通過ATTACH DATABASE命令將一個(gè)已經(jīng)存在的數(shù)據(jù)庫文件attach到當(dāng)前的連接中,之后再通過指定數(shù)據(jù)庫名的方式在目標(biāo)數(shù)據(jù)庫中創(chuàng)建數(shù)據(jù)表,如mydb.testtable。關(guān)于該規(guī)則還需要給出一些額外的說明,如果我們?cè)趧?chuàng)建數(shù)據(jù)表時(shí)沒有指定數(shù)據(jù)庫名,那么將會(huì)在當(dāng)前連接的main數(shù)據(jù)庫中創(chuàng)建該表,在一個(gè)連接中只能有一個(gè)main數(shù)據(jù)庫。如果需要?jiǎng)?chuàng)建臨時(shí)表,就無需指定數(shù)據(jù)庫名,見如下示例:
--創(chuàng)建兩個(gè)表,一個(gè)臨時(shí)表和普通表。
sqlite> CREATE TEMP TABLE temptable(first_col integer);
sqlite> CREATE TABLE testtable (first_col integer);
--將當(dāng)前連接中的緩存數(shù)據(jù)導(dǎo)出到本地文件,同時(shí)退出當(dāng)前連接。
sqlite> .backup d:/mydb.db
sqlite> .exit
--重新建立sqlite的連接,并將剛剛導(dǎo)出的數(shù)據(jù)庫作為主庫重新導(dǎo)入。
sqlite> .restore d:/mydb.db
--查看該數(shù)據(jù)庫中的表信息,通過結(jié)果可以看出臨時(shí)表并沒有被持久化到數(shù)據(jù)庫文件中。
sqlite> .tables
testtable
4). "IF NOT EXISTS"從句:
如果當(dāng)前創(chuàng)建的數(shù)據(jù)表名已經(jīng)存在,即與已經(jīng)存在的表名、視圖名和索引名沖突,那么本次創(chuàng)建操作將失敗并報(bào)錯(cuò)。然而如果在創(chuàng)建表時(shí)加上"IF NOT EXISTS"從句,那么本次創(chuàng)建操作將不會(huì)有任何影響,即不會(huì)有錯(cuò)誤拋出,除非當(dāng)前的表名和某一索引名沖突。
sqlite> CREATE TABLE testtable (first_col integer);
Error: table testtable already exists
sqlite> CREATE TABLE IF NOT EXISTS testtable (first_col integer);
5). CREATE TABLE ... AS SELECT:
通過該方式創(chuàng)建的數(shù)據(jù)表將與SELECT查詢返回的結(jié)果集具有相同的Schema信息,但是不包含缺省值和主鍵等約束信息。然而新創(chuàng)建的表將會(huì)包含結(jié)果集返回的所有數(shù)據(jù)。
sqlite> CREATE TABLE testtable2 AS SELECT * FROM testtable;
sqlite> .schema testtable2
CREATE TABLE testtable2(first_col INT);
.schema命令是sqlite3命令行工具的內(nèi)置命令,用于顯示當(dāng)前數(shù)據(jù)表的CREATE TABLE語句。
6). 主鍵約束:
--直接在字段的定義上指定主鍵。
sqlite> CREATE TABLE testtable (first_col integer PRIMARY KEY ASC);
--在所有字段已經(jīng)定義完畢后,再定義表的數(shù)約束,這里定義的是基于first_col和second_col的聯(lián)合主鍵。
sqlite> CREATE TABLE testtable2 (
...> first_col integer,
...> second_col integer,
...> PRIMARY KEY (first_col,second_col)
...> );
和其他關(guān)系型數(shù)據(jù)庫一樣,主鍵必須是唯一的。
7). 唯一性約束:
--直接在字段的定義上指定唯一性約束。
sqlite> CREATE TABLE testtable (first_col integer UNIQUE);
--在所有字段已經(jīng)定義完畢后,在定義表的唯一性約束,這里定義的是基于兩個(gè)列的唯一性約束。
sqlite> CREATE TABLE testtable2 (
...> first_col integer,
...> second_col integer,
...> UNIQUE (first_col,second_col)
...> );
在SQLite中,NULL值被視為和其他任何值都是不同的,這樣包括和其他的NULL值,如下例:
sqlite> DELETE FROM testtable;
sqlite> SELECT count(*) FROM testtable;
count(*)
----------
0
sqlite> INSERT INTO testtable VALUES(NULL);
sqlite> INSERT INTO testtable VALUES(NULL);
sqlite> SELECT count(*) FROM testtable;
count(*)
----------
2
由此可見,兩次插入的NULL值均插入成功。
8). 為空(NOT NULL)約束:
sqlite> CREATE TABLE testtable(first_col integer NOT NULL);
sqlite> INSERT INTO testtable VALUES(NULL);
Error: testtable.first_col may not be NULL
從輸出結(jié)果可以看出,first_col已經(jīng)被定義了非空約束,因此不能在插入NULL值了。
9). 檢查性約束:
sqlite> CREATE TABLE testtable (first_col integer CHECK (first_col < 5));
sqlite> INSERT INTO testtable VALUES(4);
sqlite> INSERT INTO testtable VALUES(20); -- 20違反了字段first_col的檢查性約束(first_col < 5)
Error: constraint failed
--和之前的其它約束一樣,檢查性約束也是可以基于表中的多個(gè)列來定義的。
sqlite> CREATE TABLE testtable2 (
...> first_col integer,
...> second_col integer,
...> CHECK (first_col > 0 AND second_col < 0)
...> );
二、表的修改:
SQLite對(duì)ALTER TABLE命令支持的非常有限,僅僅是修改表名和添加新字段。其它的功能,如重命名字段、刪除字段和添加刪除約束等均為提供支持。
1). 修改表名:
需要先說明的是,SQLite中表名的修改只能在同一個(gè)數(shù)據(jù)庫中,不能將其移動(dòng)到Attached數(shù)據(jù)庫中。再有就是一旦表名被修改后,該表已存在的索引將不會(huì)受到影響,然而依賴該表的視圖和觸發(fā)器將不得不重新修改其定義。
sqlite> CREATE TABLE testtable (first_col integer);
sqlite> ALTER TABLE testtable RENAME TO testtable2;
sqlite> .tables
testtable2
通過.tables命令的輸出可以看出,表testtable已經(jīng)被修改為testtable2。
2). 新增字段:
sqlite> CREATE TABLE testtable (first_col integer);
sqlite> ALTER TABLE testtable ADD COLUMN second_col integer;
sqlite> .schema testtable
CREATE TABLE "testtable" (first_col integer, second_col integer);
通過.schema命令的輸出可以看出,表testtable的定義中已經(jīng)包含了新增字段。
關(guān)于ALTER TABLE最后需要說明的是,在SQLite中該命令的執(zhí)行時(shí)間是不會(huì)受到當(dāng)前表行數(shù)的影響,也就是說,修改有一千萬行數(shù)據(jù)的表和修改只有一條數(shù)據(jù)的表所需的時(shí)間幾乎是相等的。
三、表的刪除:
在SQLite中如果某個(gè)表被刪除了,那么與之相關(guān)的索引和觸發(fā)器也會(huì)被隨之刪除。在很多其他的關(guān)系型數(shù)據(jù)庫中是不可以這樣的,如果必須要?jiǎng)h除相關(guān)對(duì)象,只能在刪除表語句中加入WITH CASCADE從句。見如下示例:
sqlite> CREATE TABLE testtable (first_col integer);
sqlite> DROP TABLE testtable;
sqlite> DROP TABLE testtable;
Error: no such table: testtable
sqlite> DROP TABLE IF EXISTS testtable;
從上面的示例中可以看出,如果刪除的表不存在,SQLite將會(huì)報(bào)錯(cuò)并輸出錯(cuò)誤信息。如果希望在執(zhí)行時(shí)不拋出異常,我們可以添加IF EXISTS從句,該從句的語義和CREATE TABLE中的完全相同。
四、創(chuàng)建視圖:
我們這里只是給出簡(jiǎn)單的SQL命令示例,具體的含義和技術(shù)細(xì)節(jié)可以參照上面的創(chuàng)建數(shù)據(jù)表部分,如臨時(shí)視圖、"IF NOT EXISTS"從句等。
1). 最簡(jiǎn)單的視圖:
sqlite> CREATE VIEW testview AS SELECT * FROM testtable WHERE first_col > 100;
2). 創(chuàng)建臨時(shí)視圖:
sqlite> CREATE TEMP VIEW tempview AS SELECT * FROM testtable WHERE first_col > 100;
3). "IF NOT EXISTS"從句:
sqlite> CREATE VIEW testview AS SELECT * FROM testtable WHERE first_col > 100;
Error: table testview already exists
sqlite> CREATE VIEW IF NOT EXISTS testview AS SELECT * FROM testtable WHERE first_col > 100;
五、刪除視圖:
該操作的語法和刪除表基本相同,因此這里只是給出示例:
sqlite> DROP VIEW testview;
sqlite> DROP VIEW testview;
Error: no such view: testview
sqlite> DROP VIEW IF EXISTS testview;