一、sqlite3存儲數據類型
null:標識一個null值;
interger:整數類型;
real:浮點數;
text:字符串;
blob:二進制數。
約束
primary key : 主鍵(唯一,用于標識每一條記錄);
not null:非空;
unique:唯一;
check:條件檢查(約束該列的值必須符合條件才可存入);
default:默認值。
二、基礎指令
1、建立數據表
create table if not exists table_name(field1 type1,field2 type2......);
2、添加數據記錄
insert into table_name(field1,field2...)values(val1,val2,...);
3、修改數據記錄
update table_name set field1=val1,field2=val2 where experssion;
4、刪除數據記錄
delete from table_name where expression;
5、查詢數據記錄
select * from table_name where expression;
查詢所有:select * from table_name;
限制輸出數據記錄數量(分頁):
select * from table_name limit val;
升序輸出數據記錄:
select * from table_name order by field asc;
降序輸出數據記錄:
select * from table_name order by field desc;
條件查詢:
select * from table_name where field in ('val1','val2','val3');
select * from table_name where field between val1 and val2;
查詢記錄的數目:
select count(*) from table_name [where expression];
區分列數據(distinct去掉重復項將列中各字段值單獨列出):
select distinct field from table_name;
6、簡歷索引
當數據表存在大量記錄,索引有助于加快查找數據表速度。
create index index_name on table_name(field);
如:為學生表stu_no字段建立一個索引:create index s_index on t_stu(stu_no);
7、刪除數據表或索引
drop table table_name;
drop index index_name;
8、模糊查詢 like
select * from t_stu where sName like '張%';
select * from t_stu where sName like '%李%';
select * from t_stu where sName like '%[]%'--[0-9] [a-z] [123];
9、分組 group by
select sClassId,count(sId),max(sAge) from t_stu group by sClassId;
10、having :對分組后的數據進行篩選
select sClassId,count() from t_stu group by sClassId having count()>3;//按照sClassId分組,然后選出數據數目3條以上的;
11、嵌套查詢(子查詢)
在子查詢中,一般使用in 代替=使用:
select employee_id,employee_name from t_employee where department_id in (select department_id from t_department where department_name='銷售部');
12、連接查詢(內連接、左連接、右連接、交叉連接)
內連接:inner(outer) join...on...:查詢滿足on后面條件的數據;
左連接:left(right) join...on...:先查出左表中的所有數據,再使用on后面的條件對數據過濾。
交叉連接:cross join:每一個表的每一行和后面表的每一行進行連接,沒有條件。
三、新增指令
1、增加一個列
alter table t_name add column col type;
2、添加主鍵
alter table t_name add primary key(col);
alter table t_name drop primary key(col);
3、創建視圖
create vire viewname as select statement;
drop view viewname;
4、高級查詢運算符
union:組合其他兩個結果表并消除表中任何重復行而派生出一個新的結果表。(union all 不排除重復)
except:通過包括所有在table1中但不在table2中的行并消除所有重復行而派生出一個結果表。
intersect:只包括table1和table2中都有的行并消除所有重復行而派生出一個結果表。
5、in 使用方法
select ** from table1 where a [not] in ('val1','val2','val3','val4'...);
6、top 查出最前多少數據
select top 10 ** from table1 where expression;//前10條數據.