一改艇、簡(jiǎn)介
*1、sql概念 :
sql是結(jié)構(gòu)化查詢語(yǔ)言坟岔,是一種對(duì)關(guān)系型數(shù)據(jù)庫(kù)中的數(shù)據(jù)進(jìn)行定義和操作的語(yǔ)言谒兄,可以在程序運(yùn)行過(guò)程中操作數(shù)據(jù)庫(kù)中的數(shù)據(jù)。
*2炮车、sql特點(diǎn):
1)不區(qū)分大小寫;
2)每條語(yǔ)句必須以分號(hào)“ ; ”結(jié)尾
3)sql常用關(guān)鍵字:select 酣溃、insert瘦穆、update、delete赊豌、from扛或、create、where碘饼、desc熙兔、order悲伶、by、group住涉、table麸锉、alter、view舆声、index等花沉。
數(shù)據(jù)庫(kù)中不可以使用關(guān)鍵字來(lái)命名表、字段媳握。
*3碱屁、sql語(yǔ)句的種類
1)DDL(Data Definition Language數(shù)據(jù)定義語(yǔ)句)
create、drop (創(chuàng)建及刪除表)
create table if not exists表名(字段名1字段類型1,字段名2字段類型2, …) ;
drop table if exists表名;
drop table if exists表名;
2)DML(Data Manipulation Language數(shù)據(jù)操作語(yǔ)句)
insert蛾找、update娩脾、delete(插入、修改打毛、刪除表中的數(shù)據(jù))
> insert into表名(字段1,字段2, …)values(字段1的值,字段2的值, …) ;
> update表名set字段1=字段1的值,字段2=字段2的值, … ;
> delete from表名;
3) DQL(Data Query Language數(shù)據(jù)查詢語(yǔ)句)
select(查詢)
where 柿赊、order by、group by 隘冲、having
>select字段1,字段2, …from表名;
> select*from表名;查詢所有的字段
4)字段類型
integer:整型值
real:浮點(diǎn)值
text:文本字符串
blob:二進(jìn)制數(shù)據(jù)(eg:文件)
5)條件語(yǔ)句
如果只想更新或者刪除某些固定的記錄闹瞧,那就必須在DML語(yǔ)句后加上一些條件
條件語(yǔ)句的常見格式
where字段 = 某個(gè)值;
where字段 is 某個(gè)值;is相當(dāng)于=
where字段 != 某個(gè)值;
where字段 is not 某個(gè)值;is not相當(dāng)于!=
where字段 > 某個(gè)值;
where字段1=某個(gè)值 and 字段2>某個(gè)值;and相當(dāng)于C語(yǔ)言中的&&
where字段1=某個(gè)值 or 字段2=某個(gè)值;or相當(dāng)于C語(yǔ)言中的||
二、其他sql語(yǔ)句格式
*1展辞、計(jì)算記錄的數(shù)量
selectcount(字段)from表名;
selectcount( * )from表名;
示例:
select count(age)fromt_student ;
select count( * )fromt_studentwherescore>=60;
*2奥邮、 起別名(字段和表都可以起別名)
select 字段1 別名,字段2 別名, …from 表名 別名;
select 字段1 別名,字段2 as 別名, …from 表名 as 別名;
select 別名.字段1,別名.字段2, …from表名 別名;
示例:
select name myname, age myagefromt_student ;
給name起個(gè)叫做myname的別名,給age起個(gè)叫做myage的別名
select s.name, s.agefromt_student s ;
給t_student表起個(gè)別名叫做s罗珍,利用s來(lái)引用表中的字段
*3洽腺、排序
select * from t_student order by字段;
默認(rèn)是按照升序排序(由小到大),也可以變?yōu)榻敌颍ㄓ纱蟮叫覆旱。?/p>
示例:
select * from t_student order by age desc;降序
select*from t_student order by age asc;升序(默認(rèn))
也可以用多個(gè)字段進(jìn)行排序
select*fromt_studentorder by age asc, height desc;
先按照年齡排序(升序)蘸朋,年齡相等就按照身高排序(降序)
*4、limit:可以精確地控制查詢結(jié)果的數(shù)量扣唱;可以用作分頁(yè)查詢
select*from表名limit數(shù)值1,數(shù)值2 ;
示例:
select*fromt_studentlimit4, 8 ;
可以理解為:跳過(guò)最前面4條語(yǔ)句藕坯,然后取8條記錄
limit常用來(lái)做分頁(yè)查詢,比如每頁(yè)固定顯示5條數(shù)據(jù)噪沙,那么應(yīng)該這樣取數(shù)據(jù)
第1頁(yè):limit 0, 5
第2頁(yè):limit 5, 5
第3頁(yè):limit 10, 5
…
第n頁(yè):limit 5*(n-1), 5
猜猜下面語(yǔ)句的作用
select*fromt_studentlimit7 ;
相當(dāng)于select*fromt_studentlimit0, 7 ;
表示取最前面的7條記錄
*5炼彪、簡(jiǎn)單約束
not null :規(guī)定字段的值不能為空
unique: 規(guī)定字段的值必須唯一
default: 指定字段的默認(rèn)值
*6、主鍵約束
? ?主鍵(Primary Key),用來(lái)唯一的標(biāo)識(shí)某一條記錄正歼。每張表都必須要有辐马。
? ? ?主鍵默認(rèn)包含了not null和unique兩個(gè)約束
*7、外鍵約束:用來(lái)建立表與表之間的關(guān)系局义。
外鍵的一般情況是:一張表的某個(gè)字段喜爷,引用著另一張表的主鍵字段冗疮。
create table t_student (id integer primary key autoincrement, name text, age integer, class_id integer,constraint fk_t_student_class_id_t_class_id foreign key(class_id) references t_class (id)) ;
t_student表中有一個(gè)叫做fk_t_student_class_id_t_class_id的外鍵
這個(gè)外鍵的作用是用t_student表中的class_id字段引用t_class表的id字段
*8、表連接查詢:需要聯(lián)合多張表才能查到想要的數(shù)據(jù)檩帐。
內(nèi)連接(inner join或join):顯示的是左右表都有完整字段值的記錄术幔。
左外連接(left outer join):保證左表數(shù)據(jù)的完整性。
select s.name,s.age from t_student s, t_class c where s.class_id =c.idandc.name= ‘0316iOS’;