1. 程式人生 > >SQL Server 數據庫基礎筆記分享(上)

SQL Server 數據庫基礎筆記分享(上)

重復 -- like cheng -s min 字節 同學 color

前言

本文是個人學習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

後記

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

SQL Server 數據庫基礎筆記分享(上)