ubuntu下使用SQLite3的基本命令
系統(tǒng)平臺(tái):ubuntu10.04
簡(jiǎn)介
sqlite3一款主要用于嵌入式的輕量級(jí)數(shù)據(jù)庫(kù),本文旨在為熟悉sqlite3基本命令提供技術(shù)文檔。
備注:本文所有操作均在root用戶下進(jìn)行。
1、安裝sqlite3
ubuntu下安裝sqlite3直接在終端運(yùn)行命令:
#apt-get install sqlite3
查看版本信息:
#sqlite3 -version
2 、sqlite3常用命令
當(dāng)前目錄下建立或打開test.db數(shù)據(jù)庫(kù)文件,并進(jìn)入sqlite命令終端,以sqlite>前綴標(biāo)識(shí):
#sqlite3 test.db
查看數(shù)據(jù)庫(kù)文件信息命令(注意命令前帶字符"."):
sqlite>.database
查看所有表的創(chuàng)建語(yǔ)句:
sqlite>.schema
查看指定表的創(chuàng)建語(yǔ)句:
sqlite>.schema table_name
以sql語(yǔ)句的形式列出表內(nèi)容: sqlite>.dump table_name 設(shè)置顯示信息的分隔符: sqlite>.separator symble Example:設(shè)置顯示信息以‘:"分隔 sqlite>.separator : 設(shè)置顯示模式: sqlite>.mode mode_name Example:默認(rèn)為list,設(shè)置為column,其他模式可通過(guò).help查看mode相關(guān)內(nèi)容 sqlite>.mode column 輸出幫助信息: sqlite>.help 設(shè)置每一列的顯示寬度: sqlite>.width width_value Example:設(shè)置寬度為2 sqlite>.width 2 列出當(dāng)前顯示格式的配置: sqlite>.show 退出sqlite終端命令: sqlite>.quit 或 sqlite>.exit
3、sqlite3指令
sql的指令格式:所有sql指令都是以分號(hào)(;)結(jié)尾,兩個(gè)減號(hào)(--)則表示注釋。
如:
sqlite>create studen_table(Stu_no interger PRIMARY KEY, Name text NOT NULL, Id interger UNIQUE, Age interger CHECK(Age>6), School text DEFAULT "xx小學(xué));
該語(yǔ)句創(chuàng)建一個(gè)記錄學(xué)生信息的數(shù)據(jù)表。
3.1 sqlite3存儲(chǔ)數(shù)據(jù)的類型
NULL:標(biāo)識(shí)一個(gè)NULL值
INTERGER:整數(shù)類型
REAL:浮點(diǎn)數(shù)
TEXT:字符串
BLOB:二進(jìn)制數(shù)
3.2 sqlite3存儲(chǔ)數(shù)據(jù)的約束條件
Sqlite常用約束條件如下:
PRIMARY KEY - 主鍵:
1)主鍵的值必須唯一,用于標(biāo)識(shí)每一條記錄,如學(xué)生的學(xué)號(hào)
2)主鍵同時(shí)也是一個(gè)索引,通過(guò)主鍵查找記錄速度較快
3)主鍵如果是整數(shù)類型,該列的值可以自動(dòng)增長(zhǎng)
NOT NULL - 非空:
約束列記錄不能為空,否則報(bào)錯(cuò)
UNIQUE - 唯一:
除主鍵外,約束其他列的數(shù)據(jù)的值唯一
CHECK - 條件檢查:
約束該列的值必須符合條件才可存入
DEFAULT - 默認(rèn)值:
列數(shù)據(jù)中的值基本都是一樣的,這樣的字段列可設(shè)為默認(rèn)值
3.3 sqlite3常用指令
1)建立數(shù)據(jù)表 create table table_name(field1 type1, field2 type1, ...); table_name是要?jiǎng)?chuàng)建數(shù)據(jù)表名稱,fieldx是數(shù)據(jù)表內(nèi)字段名稱,typex則是字段類型。 例,建立一個(gè)簡(jiǎn)單的學(xué)生信息表,它包含學(xué)號(hào)與姓名等學(xué)生信息: create table student_info(stu_no interger primary key, name text); 2)添加數(shù)據(jù)記錄 insert into table_name(field1, field2, ...) values(val1, val2, ...); valx為需要存入字段的值。 例,往學(xué)生信息表添加數(shù)據(jù): Insert into student_info(stu_no, name) values(0001, alex); 3)修改數(shù)據(jù)記錄 update table_name set field1=val1, field2=val2 where expression; where是sql語(yǔ)句中用于條件判斷的命令,expression為判斷表達(dá)式 例,修改學(xué)生信息表學(xué)號(hào)為0001的數(shù)據(jù)記錄: update student_info set stu_no=0001, name=hence where stu_no=0001; 4)刪除數(shù)據(jù)記錄 delete from table_name [where expression]; 不加判斷條件則清空表所有數(shù)據(jù)記錄。 例,刪除學(xué)生信息表學(xué)號(hào)為0001的數(shù)據(jù)記錄: delete from student_info where stu_no=0001; 5)查詢數(shù)據(jù)記錄 select指令基本格式: select columns from table_name [where expression]; a查詢輸出所有數(shù)據(jù)記錄 select * from table_name; b限制輸出數(shù)據(jù)記錄數(shù)量 select * from table_name limit val; c升序輸出數(shù)據(jù)記錄 select * from table_name order by field asc; d降序輸出數(shù)據(jù)記錄 select * from table_name order by field desc; e條件查詢 select * from table_name where expression; select * from table_name where field in ("val1", "val2", "val3"); select * from table_name where field between val1 and val2; f查詢記錄數(shù)目 select count (*) from table_name; g區(qū)分列數(shù)據(jù) select distinct field from table_name; 有一些字段的值可能會(huì)重復(fù)出現(xiàn),distinct去掉重復(fù)項(xiàng),將列中各字段值單個(gè)列出。 6)建立索引 當(dāng)說(shuō)數(shù)據(jù)表存在大量記錄,索引有助于加快查找數(shù)據(jù)表速度。 create index index_name on table_name(field); 例,針對(duì)學(xué)生表stu_no字段,建立一個(gè)索引: create index student_index on student_table(stu_no); 建立完成后,sqlite3在對(duì)該字段查詢時(shí),會(huì)自動(dòng)使用該索引。 7)刪除數(shù)據(jù)表或索引 drop table table_name; drop index index_name;
參考資料:
http://www.sqlite.com.cn/MySqlite/4/378.Html
相關(guān)文章:
1. SQLite3 命令行操作指南2. SQLite3中的日期時(shí)間函數(shù)使用小結(jié)3. SQLite3數(shù)據(jù)庫(kù)的介紹和使用教程(面向業(yè)務(wù)編程-數(shù)據(jù)庫(kù))4. SQLite3 API 編程手冊(cè)5. 一篇文章帶你掌握SQLite3基本用法6. SQLite3中自增主鍵相關(guān)知識(shí)總結(jié)7. SQLITE3 使用總結(jié)8. django 將自帶的數(shù)據(jù)庫(kù)sqlite3改成mysql實(shí)例9. SQLite3的綁定函數(shù)族使用與其注意事項(xiàng)詳解10. 初識(shí)SQLITE3數(shù)據(jù)庫(kù)
