sqlite安裝html
DDL(數據定義語言):create、alter、dropsql
DML(數據操做語言):insert、update、delete數據庫
DQL(數據查詢語言):selectexpress
.header on .mode [csv/column/html/insert/line/list/tabs/tcl] .databases .exit(.quit) .help .import FILE TABLE .schema ?TABEL? .dump ?TABLE? .show
sqlite3 語法:測試
不區分大小寫(除GLOB與glob)ui
數據類型:spa
null、integer、real、text(文本字符串)、blob日誌
--使用 SQLite .dump 點命令來導出完整的數據庫在一個文本文件中 $sqlite3 testDB.db .dump > testDB.sql --恢復 $sqlite3 testDB.db < testDB.sql
建立、附加、分離數據庫:code
1 --建立數據庫 2 $sqlite3 DatabaseName.db
1 --附加數據庫 2 ATTACH DATABASE 'DatabaseName' As 'Alias-Name'; 3 4 --如: 5 sqlite> ATTACH DATABASE 'testDB.db' as 'TEST'; 6 7 8 --分離數據庫 9 DETACH DATABASE 'Alias-Name'; 10 11 --如: 12 sqlite> DETACH DATABASE 'TEST';
建立、刪除(drop)、更改表(基於'TABLE');插入、查詢、更新、刪除(delete)表項(基於'FROM')
1 sqlite> CREATE TABLE COMPANY( 2 ID INT PRIMARY KEY NOT NULL, 3 NAME TEXT NOT NULL, 4 AGE INT NOT NULL, 5 ADDRESS CHAR(50), 6 SALARY REAL 7 ); 8 9 sqlite>.tables 10 11 sqlite>.schema company 12 13 14 --//DROP TABLE database_name.table_name; 15 16 sqlite>DROP TABLE COMPANY; 17 18 19 /* 20 INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)] 21 VALUES (value1, value2, value3,...valueN); 22 INSERT INTO TABLE_NAME VALUES (value1,value2,value3,...valueN); 23 */ 24 sqlite>INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) 25 VALUES (1, 'Paul', 32, 'California', 20000.00 ); 26 sqlite>INSERT INTO COMPANY VALUES (7, 'James', 24, 'Houston', 10000.00 ); 27 28 --SELECT column1, column2, columnN FROM table_name; 29 30 31 UPDATE table_name 32 SET column1 = value1, column2 = value2....columnN=valueN 33 [ WHERE CONDITION ]; 34 35 36 //ALTER 37 //重命名已有表 38 ALTER TABLE database_name.table_name RENAME TO new_table_name; 39 40 41 //在已有表中添加一個新的列 42 ALTER TABLE database_name.table_name ADD COLUMN column_def...;
關鍵字:DISTINCT GROUP BY HAVING ORDER BY LIMIT OFFSET
1 //LIMIT OFFSET 2 SELECT column1, column2, columnN 3 FROM table_name 4 LIMIT [num of rows] OFFSET [row num]; 5 6 7 8 9 10 //在 SELECT 語句中,GROUP BY 子句放在 WHERE 子句以後,放在 ORDER BY 子句以前。 11 SELECT column-list 12 FROM table_name 13 WHERE [ conditions ] 14 [GROUP BY column1, column2....columnN] 15 [ORDER BY column1, column2, .. columnN] [ASC | DESC]; 16 17 sqlite> SELECT NAME, SUM(SALARY) FROM COMPANY GROUP BY NAME ORDER BY NAME DESC;; 18 19 20 SELECT 21 FROM 22 WHERE 23 GROUP BY 24 HAVING 25 ORDER BY 26 27 28 //HAVING 子句必須放在 GROUP BY 子句以後,必須放在 ORDER BY 子句以前 29 SELECT column1, column2 30 FROM table1, table2 31 WHERE [ conditions ] 32 GROUP BY column1, column2 33 HAVING [ conditions ] 34 ORDER BY column1, column2 35 36 sqlite > SELECT * FROM COMPANY GROUP BY name HAVING count(name) > 2; 37 38 39 // DISTINCT 40 SELECT DISTINCT column1, column2,.....columnN 41 FROM table_name 42 WHERE [condition]
sqlite3約束:
SQLite 支持 ALTER TABLE 的有限子集。在 SQLite 中,ALTER TABLE 命令容許用戶重命名錶,或向現有表添加一個新的列。重命名列,刪除一列,或從一個表中添加或刪除約束都是不可能的。
JOIN[INNER/OUTER/NATURAL] UNION UNION ALL 表、列別名
1 //ON、USING 用在join中 2 //內鏈接(INNER JOIN)是最多見的鏈接類型,是默認鏈接類型。INNER 關鍵字是可選的。 3 //外連接 OUTER 關鍵字必選 4 SELECT ... FROM table1 LEFT [OUTER/INNER] JOIN table2 ON conditional_expression ... 5 6 SELECT ... FROM table1 LEFT [OUTER/INNER] JOIN table2 USING ( column1 ,... ) ... 7 8 //天然鏈接(NATURAL JOIN)相似於 JOIN...USING,只是它會自動測試存在兩個表中的每一列的值之間相等值: 9 SELECT ... FROM table1 NATURAL JOIN table2... 10 11 //UNION 子句/運算符用於合併兩個或多個 SELECT 語句的結果,不返回任何重複的行。 12 SELECT column1 [, column2 ] 13 FROM table1 [, table2 ] 14 [WHERE condition] 15 16 UNION 17 18 SELECT column1 [, column2 ] 19 FROM table1 [, table2 ] 20 [WHERE condition] 21 22 //UNION ALL 運算符用於結合兩個 SELECT 語句的結果,包括重複行。 23 SELECT column1 [, column2 ] 24 FROM table1 [, table2 ] 25 [WHERE condition] 26 27 UNION ALL 28 29 SELECT column1 [, column2 ] 30 FROM table1 [, table2 ] 31 [WHERE condition] 32 33 34 //表 別名 35 SELECT column1, column2.... 36 FROM table_name AS alias_name 37 WHERE [condition]; 38 39 sqlite> SELECT C.ID, C.NAME, C.AGE, D.DEPT 40 FROM COMPANY AS C, DEPARTMENT AS D 41 WHERE C.ID = D.EMP_ID; 42 43 //列 別名 44 SELECT column_name AS alias_name 45 FROM table_name 46 WHERE [condition]; 47 48 sqlite> SELECT C.ID AS COMPANY_ID, C.NAME AS COMPANY_NAME, C.AGE, D.DEPT 49 FROM COMPANY AS C, DEPARTMENT AS D 50 WHERE C.ID = D.EMP_ID;
SQLite 觸發器(Trigger)是數據庫的回調函數,它會在指定的數據庫事件發生時自動執行/調用。如下是關於 SQLite 的觸發器(Trigger)的要點:
建立 觸發器(Trigger) 的基本語法以下:
1 CREATE TRIGGER trigger_name [BEFORE|AFTER] event_name 2 ON table_name 3 BEGIN 4 -- Trigger logic goes here.... 5 END; 6 /*event_name 能夠是在所提到的表 table_name 上的 INSERT、DELETE 和 UPDATE 數據庫操做。您能夠在表名後選擇指定 FOR EACH ROW */ 7 CREATE TRIGGER database_name.trigger_name [BEFORE|AFTER] INSERT ON table_name FOR EACH ROW 8 BEGIN 9 stmt1; 10 stmt2; 11 .... 12 END; 13 14 //如下是在 UPDATE 操做上在表的一個或多個指定列上建立觸發器(Trigger)的語法: 15 CREATE TRIGGER trigger_name [BEFORE|AFTER] UPDATE OF column_name ON table_name 16 BEGIN 17 -- Trigger logic goes here.... 18 END; 19 20 21 //如:將建立一個名爲 AUDIT 的新表。每當 COMPANY 表中有一個新的記錄項時,日誌消息將被插入其中: 22 sqlite> CREATE TABLE COMPANY( 23 ID INT PRIMARY KEY NOT NULL, 24 NAME TEXT NOT NULL, 25 AGE INT NOT NULL, 26 ADDRESS CHAR(50), 27 SALARY REAL 28 ); 29 30 sqlite> CREATE TABLE AUDIT( 31 EMP_ID INT NOT NULL, 32 ENTRY_DATE TEXT NOT NULL 33 ); 34 35 /*在這裏,ID 是 AUDIT 記錄的 ID,EMP_ID 是來自 COMPANY 表的 ID,DATE 將保持 COMPANY 中記錄被建立時的時間戳。因此,如今讓咱們在 COMPANY 表上建立一個觸發器*/ 36 37 sqlite> CREATE TRIGGER audit_log AFTER INSERT 38 ON COMPANY 39 BEGIN 40 INSERT INTO AUDIT(EMP_ID, ENTRY_DATE) VALUES (new.ID, datetime('now')); 41 END; 42 43 44 //從 sqlite_master 表中列出全部觸發器 45 sqlite> SELECT name FROM sqlite_master 46 WHERE type = 'trigger'; 47 48 //列出特定表上的觸發器,則使用 AND 子句鏈接表名 49 sqlite> SELECT name FROM sqlite_master 50 WHERE type = 'trigger' AND tbl_name = 'COMPANY'; 51 52 53 //刪除已有的觸發器 54 sqlite> DROP TRIGGER trigger_name;
VIEW
視圖(View)能夠包含一個表的全部行或從一個或多個表選定行。視圖(View)能夠從一個或多個表建立,這取決於要建立視圖的 SQLite 查詢。
視圖(View)是一種虛表,容許用戶實現如下幾點:
SQLite 視圖是隻讀的,所以可能沒法在視圖上執行 DELETE、INSERT 或 UPDATE 語句。可是能夠在視圖上建立一個觸發器,當嘗試 DELETE、INSERT 或 UPDATE 視圖時觸發,須要作的動做在觸發器內容中定義。
1 CREATE [TEMP | TEMPORARY] VIEW view_name AS 2 SELECT column1, column2..... 3 FROM table_name 4 WHERE [condition]; 5 6 7 DROP VIEW view_name;
事務 索引-->indexed by
總結出處:http://wiki.jikexueyuan.com/project/sqlite/