前言數據庫
本文是我的學習SQL Server 數據庫時的以往筆記的整理,內容主要是對數據庫的基本增刪改查的SQL語句操做和約束,視圖,存儲過程,觸發器的基本瞭解。ide
注:內容比較基礎,適合入門者對SQL Server 數據庫的瞭解!!!函數
正文性能
1.主鍵:學習
主鍵的做用:保證表中的每條數據的惟一性
特色: 主鍵不能重複 不能爲空
分類:
邏輯主鍵:選擇爲表中增長的那些「自動編號」列或者「GUID」列爲主鍵(沒有實際業務上的意義)的主鍵 (建議使用邏輯主鍵)
業務主鍵:選擇表中那些在業務中有實際意義的列做爲主鍵
》》》》》》》》》選擇主鍵的策略,選什麼樣的列做爲主鍵《《《《《《《《《
1》主鍵,建議選擇那些通常不會被修改的列
2》選擇單列,不選擇多列(不用組合主鍵)
3》選擇那些簡單列(整數列(自動編號))spa
2.char(),nchar(),varchar()之間的區別日誌
》》》》》》》》》char(10)與varchar(10)的區別《《《《《《《《《
char(10) 固定長度,表示在數據庫中存儲的時候佔用10個字節的空間,若是超出10個則報錯,若是不夠10個則用空格補全。
varchar(10) 可變長度,表示該列最多能夠存儲10個字節,若是實際存儲不夠10個字節,則會在存儲的時候自動計算一下實際的存儲個數,而動態的改變長度。【節省空間】code
》》》》》》》》》char(10)與nchar(10)的區別《《《《《《《《《排序
char(10) 能夠存儲10個字母或者5個漢字。 用來存儲數據的時候,英文站1個字節,中文站2個字節。unicode
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
後記
下篇分享視圖、觸發器等,分頁查詢、子查詢、連表查詢等