sqlite3提供的特殊命令, 以.開頭:sql
.help: 幫助
.databases: 列出數據庫
.tables: 列出表名
.open dbname: 打開數據庫
.save dbname: 保存爲數據庫
.exit: 退出, 或Ctrl-D
.schema [tbname]: 列出表, 索引, 觸發器的建立語句
.output fname.txt: 寫結果到文件
.show, 顯示各類設置的默認值
.indices tbname, 列出某表的索引數據庫
各類設置:dom
.echo on|off, 開啓或關閉命令回顯
.explain on|off, 開啓或關閉適合於EXPLAIN的輸出模式, 更適合人閱讀
.headers on|off, 是否顯示字段信息頭
.stats on|off, 開啓或關閉統計信息
.timer on|off, 開啓或關閉命令執行的時間測量函數
create table company ( id int primary key not null autoincrement, name text not null, age int not null unique, address char(50), salary real default 50000.00 check(salary>0) );
int, text, real, char(5), 都是字段的類型
char(50), 代表此字段存儲字符不超過50個
not null, 代表此字段不能爲空
primary key, 代表此字段爲基鍵, 不能重複
unique, 確保某列中沒有重複值
default, 當列沒有值時,提供默認值
check, 確保某列中的全部值知足必定條件
autoincrement, 確保列中值自動增長, 天然無需手動提供測試
alter table company add column sex char(1); --爲company表添加列sex, 類型爲char(1) alter table company rename to old_company; --爲表重全名
Sqlite3中, alter容許用戶重命名錶,或向現有表添加一個新列
但不能重命名列, 刪除列, 從表中添加或刪除約束code
insert into company (id, name, age, address, salary) values (1, 'Paul', 32, 'Beijing', 20000.00); insert into company values (1, 'Paul', 32, 'Beijing', 20000.00); --插入全部字段時可省略列名 insert into company_bkp select * from company; --將company表中的全部記錄所有插入到company_bkp表中, 兩表結構必須類似
select * from company; select id, name from company; --查詢company表中id和name字段 select tbl_name from sqlite_master where type=='table'; --查詢當前數據庫存在的表 select current_timestamp; --查詢當前時間戳 select * from company limit 6; --只顯示查詢結果的前6行 select * from company limit 3 offset 2; --只顯示從第3行起, 再多2行, 一共3行 select * from company order by salary asc; --以salary字段升序顯示記錄, desc爲降序 select * from company order by name, salary asc; --將結果按name和salary字段升序顯示, 即name相同的按salary排序 select name, sum(salary) from company group by name; --將結果中相同name的salary相加, 再構成name, sum(salary)列表 select name, sum(salary) from company group by name order by name; --同上, 將結果以name升序顯示 select * from company group by name having count(name) < 2; --以name分組, 相同name記錄數小於2, having設置分組的過濾條件 select distinct name from company; --去重, 相同name不顯示 select * from company where salary>10000 group by name having count(name)>=2 order by name --相同name的記錄數大於或等於2, 且salary大於10000, 以name升序顯示 select * from company cross join department; select * from company, department; --將company的每一行與第二個表的每一行進行匹配, 分別有x和y行, 則結果有x*y行, 分別有x和y列, 則結果有x+y列. 交叉鏈接可能產生很是大的表 select * from company [inner] join department on company.id==department.emp_id; --選取company的id列與department的emp_id列相等的行進行鏈接, 內鏈接是默認鏈接, 可省略inner, 橫向鏈接 select * from company join department using (id); --使用兩表共有的id列進行相同值鏈接 select * from company natural join department; --自動測試存在兩個表中的每一列的值之間相等值 select * from company left outer join department on company.id==department.emp_id; --不一樣於內鏈接, 左外鏈接還會合並進第一個表的非匹配行, 這些行多餘的列, 即對應第二個表的列爲null. 之因此第一表顯示, 由於是left嘛. select col1, col2, ... from table1 where conditions union [all] select col1, col2, ... from table2 where conditions; --不侷限於上面的語句, 事實上union將兩個select的結果縱向鏈接去重.所以這要求結果必須列相同, 列類型相同. join則是橫向鏈接. union all不去重. select c.id, c.name, c.age, d.dept from company as c, department as d where c.id==d.emp_id; select c.id, c.name, c.age, d.dept from company as c join department as d on c.id==d.emp_id; --經過as給表起別名 select * from company where id in (select id from company where salary > 45000); select * from company where salary > 45000; --子查詢, `()`中的select先執行, 此處兩個查詢相同, 可與select, insert, update, delete混合使用
create trigger audit_log after insert on company begin insert into audit (emp_id, entry_date) values (new.ID, datetime('now')); end; --建立觸發器audit_log, 當向company表執行insert操做後, 會觸發向audit表插入記錄, 值爲插入company表的id和執行時間戳. select name from sqlite_master where type=='trigger' and tbl_name=='company'; --列出關聯於company表的觸發器 drop trigger audit_log; --刪除觸發器
create index salary_index on company (salary); --對company表的salary列建立索引salary_index select name from sqlite_master where type=='index' and tbl_name=='company'; --列出對應於company表的索引 select * from company indexed by salary_index where salary > 5000; --使用索引從company表中選擇數據 drop index salary_index --刪除索引
索引可加快數據檢索, 但不利於數據更新和插入
索引不影響數據
可對多列索引, (col1, col2)
建立表時會自動建立主鍵primary key的索引sqlite
create view company_view as select id, name, age from company; --爲company表的id,name,age列建立視圖 select * from company_view; --列出視圖的全部數據, 由於其只有真表的id,name,age三個列, 所以這裏只列出三列 drop view company_view; --刪除視圖
可將視圖認爲是虛表, 它自己不真正存儲數據, 它只是提供真正表的一個觀察角度
由於視圖不是真正的表, 所以並不能插入或更新數據, 但可能建立觸發器, 當插入或更新數據時, 執行真正的操做.排序
begin; --事務開始 delete from company where age==25; --刪除age等於25的全部記錄 rollback; --回滾, 即恢復數據 commit; --提交更改
事務具備原子性, 即事務要麼成功要麼失敗, 而不會停留在中間狀態
事務只與insert, update, delete一塊兒使用索引
count, 謀算表的行數
max, min, 選擇某列的最大值, 最小值
avg, 計算某列的平均值
sum, 計算某列的總和
random, 返回僞隨機數
abs, 返回絕對值, 全部字串返回0.0
upper, 將字符串轉換爲大寫字母
lower, 將字符串轉換爲小寫字母
length, 返回字串的長度
sqlite_version, 返回sqlite的版本事務
select count(*) from company; --company表的行數, 注意, 指定特定列時, 爲null值的記錄不計數 select max(salary) from company; --選擇company表的salary列的最大值 select avg(salary) from company; select sum(salary) from company; select random(); select abs(-5); select upper(name) from company; --列出company表的name列的大寫