SQL Server 資料庫基礎筆記分享(上)

一人腳步發表於2018-07-14

前言

本文是個人學習SQL Server 資料庫時的以往筆記的整理,內容主要是對資料庫的基本增刪改查的SQL語句操作約束,檢視,儲存過程,觸發器的基本瞭解。

注:內容比較基礎,適合入門者對SQL Server 資料庫的瞭解!!!

正文

1.主鍵:

主鍵的作用:保證表中的每條資料的唯一性
特點: 主鍵不能重複 不能為空
分類:
邏輯主鍵:選擇為表中增加的那些“自動編號”列或者“GUID”列為主鍵(沒有實際業務上的意義)的主鍵 (建議使用邏輯主鍵)
業務主鍵:選擇表中那些在業務中有實際意義的列作為主鍵
》》》》》》》》》選擇主鍵的策略,選什麼樣的列作為主鍵《《《《《《《《《
1》主鍵,建議選擇那些一般不會被修改的列
2》選擇單列,不選擇多列(不用組合主鍵)
3》選擇那些簡單列(整數列(自動編號))

 

2.char(),nchar(),varchar()之間的區別

》》》》》》》》》char(10)與varchar(10)的區別《《《《《《《《《
char(10) 固定長度,表示在資料庫中儲存的時候佔用10個位元組的空間,如果超出10個則報錯,如果不夠10個則用空格補全。
varchar(10) 可變長度,表示該列最多可以儲存10個位元組,如果實際儲存不夠10個位元組,則會在儲存的時候自動計算一下實際的儲存個數,而動態的改變長度。【節省空間】

》》》》》》》》》char(10)與nchar(10)的區別《《《《《《《《《

char(10) 可以儲存10個字母或者5個漢字。 用來儲存資料的時候,英文站1個位元組,中文站2個位元組。

nchar(10) 表示可以儲存10個字母或10個漢字,因為每個字元都是按照unicode方法來儲存的。當使用nchar(10),來儲存資料的時候無論儲存的是中文還是英文都是每個字元佔2個。

 

3. 建立資料庫

–建立一個資料庫
create database School

–刪除資料庫
drop database School

–建立資料庫的時候,指定一些資料庫的相關引數。
create database School
on primary –主資料檔案
(
name=`School`,
size=10mb,
filename=`c:school.mdf`,
filegrowth=10%,
maxsize=100mb
)
log on –日誌檔案
(
name=`School_log`,
filename=`c:school.ldf`,
size=5mb,
filegrowth=5mb,
maxsize=50mb
)

–切換資料庫
use school
go

4. 建立表

–建立表
create table Class
(
ClassId int identity(1,1) primary key,
ClassName varchar(50) not null,
ClassDesc varchar(50) not null
)

–刪除表
drop table Class

–向Class表中插入資料
insert into Class(ClassName,ClsDesc)values(`大三`,`三年`);

–insert into…values.. 這種寫法每次只能插入一條資料

–向Class表中插入多條資料
–重複資料不重複插入,union關鍵字本身就具有去掉重複的意思
–union | union all (重複插入)
insert into Class
select `大三`,`三年` union
select `三五`,`間諜` union
select `一一`,`多久` union
select `六七`,`得到`

–將Class表中的資料備份到Student表中
–這種寫法會將Class表中的所有資料插入到Student表中
–前提是Student表不存在,如果這個表存在則報錯。
select * into Student from Class

–向一個已經存在的表中插入資料,資料的來源是Class表
insert into Student(ClassName,ClsDesc)
select ClassName,ClsDesc from Class

 

–查詢表中資料
select * from Class

5.update 資料

–將所有年齡小於20歲的人的年齡都改成19(tage是Class表後加屬性)
update Class set tage=19 where tage<20

–將年齡為19歲的並且性別為0的人的姓名兩邊★改為☆
update Class set ClassName =replace (tname,`★`,`☆`) where tage=19 and tgender=0

6.刪除資料

delete from Class –刪除所有資料 自動編號沒有恢復到預設值 可以根據條件來刪除
truncate table Class –重新設定了自動編號 刪除只能一次性都清空,不能根據條件來刪除 清除速度(效能)比delete語句快的多

delete from Class where tage=19 or tage is null –刪除19歲或者空值

》》》》》》》》》刪除重複資料只保留一條(id最小的一條)《《《《《《《《《
》》》》》》》》》刪除表中多餘的重複記錄,重複記錄是根據單個欄位(peopleId)來判斷,只留有rowid最小的記錄 《《《《《《《《《
delete from people
where peopleName in (select peopleName from people group by peopleName having count(peopleName) > 1)
and peopleId not in (select min(peopleId) from people group by peopleName having count(peopleName)>1)

 

7.條件查詢,模糊查詢

–查詢數學沒有及格的學生的學號
select
fid as 學號,
fmath as 分數
from MyStudent where fmath<60

–查詢年齡在20-30歲之間的男學生
select
fname as 姓名 from MyStudent where fage between 20 and 30 and fgender=`男`

–查詢班級id 1 2 3 的所有學生
select * from MyStudent where classid in (1,2,3)

–查詢所有姓趙的同學 (萬用字元%表示:任意多個任意字元)
select * from MyStudent where fname like `趙%`

–查詢出姓名中只要包含一個‘民’字即可。
select * from MyStudent where fname like `%民%`

–查詢所有姓趙的同學,並且姓名字數是3個
–萬用字元 _ :表示任意的單個字元。
select * from MyStudent where fname like `趙__`
select * from MyStudent where fname like `趙%` and len(fname)=3

–查詢出姓名中包含‘民’或‘用’的同學
–萬用字元[]:表示中括號中的任意個字元,只選一個匹配
–萬用字元 ^a :表示除了a這個字元都行。
select * from MyStudent where fname like `%[民用]%`

8.聚合函式

–查詢數學成績最高低分
select max(fMath) as 數學成績最高分 from MyStudent
select min(fMath) as 數學成績最低分 from MyStudent

–平均分(計算平均分的時候對空值不處理)
select avg(fMath) as 平均分 from MyStudent

–求資料記錄中的總條數(總人數)
select count(*) as 班級總人數 from MyStudent

select
最高分=(select max(fMath) as 數學成績最高分 from MyStudent),
最低分=(select min(fMath) as 數學成績最低分 from MyStudent),
平均分=(select avg(fMath) as 平均分 from MyStudent)

–分數評級
–90以上 優秀
–80以上 良好
–70以上 中
–70以下 差
select chengji,
評級=
case
when shuxue>=90 then `優秀`
when shuxue>=80 then `良好`
when shuxue>=70 then `中`
else `差`
end
from Student

9.null 問題

–請查詢出學生表中所有數學成績為null的人的資訊
–null在資料庫中表示unknow(不知道),判斷一個值是否為null,也就不能用=或者<>來判斷
select * from MyStudent where fMath=null 錯誤(不返回任何資料)

正確 select * from MyStudent where fMath is null

–查詢所有fmath為非null的值
select * from MyStudent where fMath is not null

–null值與任何資料運算後得到的還是null值。
update MyStudent set fage=fage+1 where fid=1

10.分組group by

–統計出mystudent表中,男女同學的個數

select
fgender as 性別, –這時,count(*)統計的是每一組的記錄條數, 不是總條數
count(*) as 人數
from MyStudent group by fgender –先執行group by語句分組,分完組在統計每 組個數。 分出來幾個組,那麼count(*)就統 計幾次

–查詢班級的男同學的人數大於2的資訊

–having是group by的條件對分組後的資料進行篩選(與where類似,都是篩選,只不過having是用來篩選分組後的組的)
select
classid as 班級號,
count(*) as 班級人數
from TblStudent
where fgender=`男`
group by classid
having count(*)>2

》》》》》》》》》語句執行順序《《《《《《《《《

select
–distinct / top 之類的關鍵字
fgender as 性別, –5》選擇列
count(*) as 人數
from MyStudent –1》先從表中拿到資料
where fage>30 –2》從MyStudent的資料中篩選出所有年齡大於30歲的任的資訊
group by fgender –3》按照性別分組,分完組得到一個新的結果集
having count(*)>500 –4》基於分組以後的結果集,然後再篩選,篩選出那些組中記錄大於500的組
order by 人數 asc –6》最後把顯示出來的結果排序

–語句執行順序
from > where > group by > having > select > order by

11.日期函式

–請查詢出所有入職一年以上的員工資訊
select * from TblStudent
where dateadd(year,1,tsday)<getdate()

–計算兩個時間差
–查詢90年距今是多少年
select datediff(year,`1990-9-9`,getdate())

–查詢一個日期的特定部分
select year(getdate())
select datepart(year,getdate())

–輸出所有資料中通話時間最長的5條記錄。
select top 5 *,`通話時長(秒)`=datediff(second,Startdatetime,Enddatetime) from Calltecords order by datediff(second,Stardatetime,enddatetime) desc

後記

下篇分享檢視、觸發器等,分頁查詢、子查詢、連表查詢等

 

相關文章