最近正在自學(xué)數(shù)據(jù)庫(kù)原理與編程,覺得SQL Server上手非常舒適,于是寫了這篇簡(jiǎn)書,希望對(duì)學(xué)習(xí)數(shù)據(jù)庫(kù)的朋友們有所幫助??
基本命令
內(nèi)容包含:
selectinsertupdatedelete
為列,表起別名
select * from UserInfo as ui
為UserInfo起別名為ui,當(dāng)然as可以省略.注意,一旦取別名之后,調(diào)用表的列時(shí),必須用別名來調(diào)用。
select查詢
查詢某些列
-- 查詢某些列
select UserName , UserPwd
from UserInfo
查詢前n部分?jǐn)?shù)據(jù):
-
top n 列名: 表示查詢前n行 -
top n percent 列名: 表示查看前百分之N的數(shù)據(jù)
select top 2 *
from UserInfo
排序查詢:
- 語(yǔ)法:
order by 列名 asc|desc
select * from StudentInfo
-- 當(dāng)前面的排序條件,數(shù)據(jù)是滿足相同的條件,此時(shí)可以提供多種排序方式
order by cId desc,sId asc
上述步驟會(huì)先從cId開始排序,如果所有的cId都相同,則會(huì)再根據(jù)sId排序。
條件查詢:
- 語(yǔ)法:
where-
添加細(xì)節(jié) :
-
between .. and ...: 區(qū)間之間 -
in(n1,n2): 符合n1或者n2條件,非連續(xù) - 邏輯運(yùn)算符 :
and | or | not
-
-
添加細(xì)節(jié) :
select * from ClassInfo
-- sId 在1-5之間的
where sId between 1 ans 5
-- sId不在1或者3, 使用 not 和 in
where sId not in(1,3)
-- 邏輯運(yùn)算符條件
where sId=1 or sId-3
-- 查找編號(hào)在3-5的1班學(xué)生
where sId between 3 and 5 and cId=1
注意:使用
between ... and ...需要連續(xù)區(qū)間。
注意 between的
and會(huì)尋找sql語(yǔ)句中離它最近的。
模糊查詢
- 語(yǔ)法 :
like % _ [] ^-
%: 表示零到多個(gè)任意字符 -
_: 表示一個(gè)任意字符 -
[]: 表示范圍內(nèi)的一個(gè)字符 -
^: 非,寫在[]之前表示不在范圍內(nèi)
-
-- 查詢班級(jí)姓張的同學(xué)
select * from ClassInfo
where sName like '張%'
-- 電話號(hào)碼第二位為0-4
where sPhone like '1[^579]'
連接查詢
當(dāng)需要的結(jié)果從多張表中取得時(shí),使用連接查詢
-- 查詢學(xué)生姓名及所在班級(jí)名稱
-- StudentInfo
-- ClassInfo
-- 關(guān)系:StudentInfo.cid =>ClassInfo.cid
select UserInfo.UserName , classInfo.cTitle
from UserInfo
inner join ClassInfo on StudentInfo.cid=ClassInfo.cid
-- 當(dāng)然也可以多個(gè)表聯(lián)結(jié),只需要在后面繼續(xù)添加join語(yǔ)句即可
on后面寫入連接查詢的條件
- 連接關(guān)鍵字:
join
左右表:在
join左邊的表為左表。
| 兩種主要的連接方式 | |
|---|---|
內(nèi)聯(lián)結(jié):inner join
|
如果查詢的內(nèi)容嚴(yán)格對(duì)應(yīng),則為內(nèi)聯(lián)結(jié) |
| 外聯(lián)結(jié) | |
左外聯(lián)結(jié):left outer join
|
左表特有的數(shù)據(jù),右邊沒有,依舊可以查詢出來,顯示值為NULL
|
右外聯(lián)結(jié):right outer join
| |
完全外聯(lián)結(jié):full outer join
|
左右兩表公有的,左表和右表特有的,三者均表示出來 |
insert插入
select * from UserInfo
-- md5加密
insert UserInfo(UserName,UserPwd)
values('1','2')
如果所有列按照默認(rèn)順序賦值,則可以使用如下方式
insert UserInfo
values('小孫','password')
- 只為某些列進(jìn)行賦值
insert UserInfo(UserName)
values('小杰')
- 一次增寫入多個(gè)數(shù)據(jù)
使用逗號(hào)隔開各個(gè)數(shù)據(jù)
select * from UserInfo
insert into UserInfo
values('小紅'),('小菊'),('小巴')
update修改
語(yǔ)法:
update 表名
set 列名1=值1,列名2=值2,...
where ...
where : 為指定行進(jìn)行修改列
將值設(shè)置為空
update 表名 set 列名=null
delete刪除
語(yǔ)法:
delete from 表名
where ...
其中
from寫不寫都可以。當(dāng)刪除一個(gè)列的數(shù)據(jù),如果設(shè)置了~Id為唯一標(biāo)識(shí),則Id的計(jì)數(shù)器會(huì)在刪除的最后一條數(shù)據(jù)的Id的基礎(chǔ)上,繼續(xù)自增,即如果刪到了第15條,則此時(shí)insert插入后,這條數(shù)據(jù)的Id將從16開始計(jì)數(shù)。
清空 : truncate table 表名
-- 清空
truncate table UserInfo
這個(gè)命令既是將表內(nèi)容刪除了,也會(huì)同時(shí)將表的內(nèi)容進(jìn)行重置,即計(jì)數(shù)器將會(huì)從1開始。
消除重復(fù)行:
- 語(yǔ)法 :
distinct
select distinct cId from StudentInfo
本文作者: Freyr_sau(弗雷)
注明:文章為作者一字一句敲出來,整理實(shí)在不容易,希望各位轉(zhuǎn)載寫明出處,覺得有幫助的還請(qǐng)多多分享點(diǎn)贊??
須知:未經(jīng)允許,不得轉(zhuǎn)載