sqlite數據庫的基本操作


細節:
1,什么是主鍵:
          用來唯一標識一張表中的某一條數據,所有的表都必須有自己的主鍵,主鍵可以是整型,一般都用自增,也可以是字符串類型,  如果是字符串類型的話一般使用uuid(是用java語言生成的)。一般使用主鍵來唯一查詢某一條記錄,或者更新刪除某一條記錄。
 
2,order by永遠放到最后面,語法規范!
 
3,函數
          函數count: 用來統計數量,一般統計行數有幾行     
          函數max:      用來計算某列中的最大值,列的類型一定是整型(一般情況下遇到類似年齡,分數等將來可能會計算最大值的或者本身就是數字,類型設置為整型)。
          函數sum:       類似max,列的字段類型一定是整型
 
4,左連接:左邊表全部顯示,右邊顯示匹配成功的。
5,全連接:左右兩邊完全匹配成功。
6,sqlite 無右連接
 
 
-------------------表的插入------------
insert into students (id,name,age,sex)values('3','Mike','21','女');--add
insert into students (id,name,age,sex)values('4','Lisa','21','女');--add
insert into students (id,name,age,sex)values('5','Raro','21','女');--add
insert into students (id,num,name,age,sex,c_id)values(6,'201107014303','Jame','21','女','')
------------條件刪除 ---------------------
delete from students where name='Jame';--delete
select *from students;
select *from students where name='Mike' and age='12';
select * from students where name like 's%' collate nocase;
------------修改表名------------
alter table student rename to students--修改表名
alter table students {rename to student|add column course text}--不識別 {}:表示為可選項,但是運行失敗
 
------------添加列---------------------
alter table students add column tel text not null default '' collate nocase
 
------------條件修改數據-----------
update students set tel='18500111111' where name='lisa' collate nocase
update students set num='201107014301'
update students set age='20' where name='Mike'
 
----------創建表,id,course兩列
create table course (id integer primary key autoincrement, course text);
------------表刪除--,只能刪除表,不能刪除列----------
drop table course;--刪除一張表(沒有關聯關系)
 
-----------如果想忽略大小寫,即 case-insensitive,需要用到COLLATE NOCASE :-----------
update students set age='12' where name='mike' collate nocase;
 
----------執行以下語句查看所有用戶的用戶名和年齡:
select name,age from students;
select *from cqx_db where type='table' and name='teacher';
 
--查看一張表中 字段name沒有重復的條目
select distinct name from students;
 
select * from students group by age having count(*)>1;
 
------------- limit a offset b或者limit b ,a ---表示調過b項,返回a項數據--------
select * from students where num like '_011%' order by age asc, name limit 1,2;--如果相同,則根據名字的首字母排序
select * from students where num like '2011%' order by age asc -- num 以2011結尾的所有學生升序,默認為升序
select * from students where num like '2011%' order by age desc, age limit 2 offset 1;--根據年齡排序
select * from students where num like '2011%' limit 2 offset 1;
 
--------------- -----'%a' :以a結尾(_a),'a%':以a開頭 '%a%':包含a-------------------------------
select * from students where num like '%01' order by age desc; 以01結尾的匹配
select * from students where num not like '%02%';選取不包含02的num
 
-- 函數upper( name ) 大寫 ,lower( name ) 小寫 ,count(*)統計數量大小,sum(age) 求和,max(age)求最大值
select upper(name) ,length(name) from students where num like '2011%'
select lower(name) ,length(name) from students where num like '2011%'
select upper(name) ,length(name) from students where num like '%01' and length(name)<5
---------- 分組group by- 並采用函數計算租大小---------
select * ,count(*)from students group by num;--按照num進行分組
select count(*) from students where num like '%01';
------------------------- 去掉重復 distinct-----------------------
select distinct num from students ;
 
--------------- 多表連接 join-------------------------
 
 
 
insert into teacher (id,t_name,t_course) values(1,'a','語文')
insert into teacher (id,t_name,t_course) values(2,'a','語文')
insert into teacher (id,t_name,t_course) values(3,'a','語文')
 
update teacher set c_id='1'
update students set c_id=12 where num like '%01';
update teacher set c_id=1;
--添加列 not null時 default ''
alter table teacher add column c_id text not null default '';
alter table teacher drop column c_course;
select teacher.t_name,students.name from teacher ,students where teacher.c_id=students.c_id;
 
------------------------內連接 inner join--------------
 select * from teacher inner join students on teacher.c_id=students.c_id;
alter table course add column id integer;
insert into course values(1,1,'語文');
insert into course values(1,2,'語文');
select * from students inner join course on students.c_id=course.c_id;
---------------交叉連接-----------------
select * from students,course;
----------左外連接 未能匹配以左為基准,右為null--,無右連接------------
select * from students left outer join teacher on students.c_id=teacher.c_id;
 
 
--------------------別名-類似表名.相同的列名,from +原名 別名------------------------
select s.name,t.name from students s,teacher t where s.c_id=t.c_id limit 3;
 
 
 
--------------null 不等於任何值 是確實信息的占位符
--三種邏輯運算
select null is null;--結果1 非0的任何值都表示為真
select null is not null;--結果0 非0的任何值都表示為真
 
-------------coalesce函數表示為將一組值輸入並返回其中第一個非null的值------
select coalesce(null,7,null,4);--結果7
 
 
-------------nullif函數相反,表示為2個值輸入,如果相同則返回null,否則返回第一個參數----
select nullif (1,1);--結果null
select nullif (1,2);--結果1
 
 
-------子查詢-------------
select teacher.name from teacher where teacher.c_id in(select students.c_id from students where num like '2011%')
select count(name) from students where students.[c_id] in(select teacher.c_id from teacher )
select * from students group by num like '%01'
 
 
 
 
---------==================考核 練習================================-----
---查詢
select * from students where name='Lisa';  
select name,sex from students where name like '%am%'; --查詢包含am的所有name,sex
-----分組-------
select sex, count(id) from students group by sex; --根據性別sex分組,查詢sex和對用的數量
select sex,age,count(id) from students group by sex, age; --根據性別和年齡分組,查詢性別和年齡以及數量
select name,age from students order by age desc ; --根據年齡降序查詢顯示姓名和年齡
 
 
------------------------------函數max: 用來計算某列中的最大值,列的類型一定是整型(一般情況下遇到類似年齡,分數等將來可能會計算最大值的或者本身就是數字,類型設置為整型)。-------------------------------------------
select max(age) from students ; --從學生表中選取年齡最大值
 
--從學生表中選取年齡最大值的姓名和對應年齡
select name, age from students where age in (select max(age) from students) 
--計算學生表中的年齡和
select sum(age) from students ;
 
--按照性別分組並顯示性別和對應的和
select sex, count(id) from students group by sex;
 
--采用別名的方式全連接 ,別名規則為from+原表名 別名,別的地方使用到表名則采用別名
select t.t_name,s.name from teacher t,students s where t.c_id=s.c_id;
 
--左連接 通過學生和教師表查詢學生和教師名稱 sqlite無右連接
select students.name,teacher.t_name from students left join teacher on students.c_id=teacher.c_id;
--通過別名的方式左連接
select s.name,t.t_name from students s left join teacher t on s.c_id=t.c_id;
--通過左連接查詢性別為女和年齡大於21的
select s.name,t.t_name,s.age from students s left join teacher t on s.c_id=t.c_id where sex='女' and age>21 ;
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM