文章目錄
根據郝斌老師視頻教程,整理學習筆記內容實用,完整全面;
視頻郝斌-教程
作者
郝斌
狀態
已學完
簡介
學習筆記;快速掌握查詢相關的基本操作,內容完整實用
SQL學前導圖
:::info
系統數據庫:、model、msdb、【我們自定義數據庫的管理維護運行都需要系統庫支持】
:::
一 、基本信息 1 相關名詞 數據庫相關基本概念:字段、屬性、記錄(元祖)、表、主鍵、外鍵 名稱說明
字段
記錄事物某一特征
記錄
同一事物多個字段的組合,表示某一事物
表
多條記錄的組合,表示同類事物
主鍵
唯一標識某一事物的一個屬性
外鍵
此字段:來自另一個表的主鍵
表之間多對一時:外鍵建在"多"表中
2 基本語句
-- 創建表案例
create table table1
(
-- |字段|類型|約束 |約束名 |主鍵
t_id int constraint t_pk primary key,
t_name varchar(20) not null,
t_age int not null,
t_sex varchar(1)
)
create table table2
(
t2_id int constraint t2_pk primary key,
t2_name varchar(15) not null,
-- |字段|類型 |約束 |約束名 |外鍵 |涉及到 |表名
t1_id int constraint t2_fk foreign key references table1
)
3 約束:主鍵約束、外鍵約束、check約束、約束、唯一約束
定義:
對于表中某字段,操作的限制
:::info
含有主鍵的表叫做主鍵表
主鍵通常都是整數 不建議使用字符串當主鍵(如果主鍵是用于集群式服務)
主鍵的值通常都不允許修改,除非本記錄被刪除
多對多表查詢中,必須借助第三張表;
constraint t_pk primary key(字段1,字段2,字段3)
二、查詢
查詢相關: 計算列、、、in、top、null、order by 、模糊查詢、聚合函數、group by、、鏈接查詢、嵌套查詢
語句執行順序
第一步:查看來自哪張表(from)
第二步:查看查詢字段【如果是表中字段則輸出相關字段】 如果是其他值則輸出與表格對應行數的值
1 計算列
在查詢語句中,可以對字段進行算術運算
select ename, sal*12 as "年薪" from emp
2 (去重)
會過濾掉重復的任意值【包括null】
-- 過濾重復字段
select distinct deptno as "部門編號" from emp
3
限定取值范圍;需要配合where一起使用
-- between的使用 查詢sal在800-1500之間的值【包含兩端】
select sal from emp
where sal between 800 and 1500
4 in包含
取值為in里面的值
-- 只取in內值
select sal from emp
where sal in(800,1500)
-- 只取in外的值
select sal from emp
where sal not in(800,1500)
5 top 分頁【取前幾個值】
-- 取所有sal中的前兩個值
select top 2 sal from emp
案例
-- 輸出工資在1500-3000之間工資最高的前四個人姓名工資
select top 4 ename,sal from emp
where sal between 1500 and 3000
order by sal desc
6 null
null不能參與數學運算,否則值為空
(a,b):如果不為空則值取a,否則取b
-- 輸出前四個人年薪和基本信息
select top 4 *,sal*12 + ISNULL(comm,0) from emp
7 order by 排序 排序:order by 排序【默認升序】
asc:升序
desc:降序
① order by A,B
先按照A升序排序,再將A相同的B升序排序
② order by A desc, B
A降序排序,再將A相同的B升序排序
③ order by A desc, B,C,D
對A降序排序,不會對BCD產生影響
④ order by A,B desc
先按A升序,再按A相同的B降序
-- 查詢姓名和工資,以工資降序排列
select ename,sal from emp
order by sal desc
-- 查詢各部門姓名和
8 模糊查詢
基本格式: 字段名 from 表名 where 字段名 like 匹配條件
單引號和雙引號的區別:單引號表示字符串,雙引號表示標識符(變量 函數等的名字)
匹配條件:需要用單引號‘’括起來
% :任意一個或多個字符
_ : 任意單個字符
[a-f]: 匹配從a-f任意單個包含a和f
[a,f]:匹配a和f任意單個
[^a-f]:匹配不是a-f的其他任意單個字符
-- 在員工表中查詢所有名字以A開頭的人名
select ename from emp where ename like 'A%'
-- 在員工表中查詢所有名字以A-F開頭的人名
select ename from emp where ename like '[A-F]%'
-- 在員工表中查詢所有名字以A或F開頭的人名
select ename from emp where ename like '[A,F]%'
-- 在員工表中查詢所有名字不以A-F開頭的人名
select ename from emp where ename like '[^A-F]%'
轉義字符 【 ‘\’】
在中我們可以通過 定義任意符號為:轉義字符
-- 搜索名字中帶有%的內容
select name from student where name like '%\%%' escape '\'
三、聚合函數
單行函數和多行函數不能混合使用
(1)為空(null)的記錄不會被統計
-- 統計emp表中所有記錄數
select count(*) from emp
-- 統計emp表中所有人名
select count(ename) from emp
四、分組 group by
理解:group by a,b,c的用法先按a分組,如果a相同sql查詢條件左連接,再按b分組,如果b相同,再按c分組最終統計的是最小分組的信息
對分組后的數據進行過濾
綜合案例
select deptno, job,count(*) from emp
where sal > 1000
group by deptno,job
having count(*) > 1
五、鏈接查詢(多表查詢)
內鏈接: (1) … from A,B (2) … from A,B where … (3) … from A join B on … (4) … from A,B
--1.求出每個員工的姓名 部門編號 薪水 和 薪水的等級
select S.grade
from emp "E"
join salgrade "S"
on E.sal<=S.hisal and E.sal>=S.losal
--2.查找每個部門的編號 該部門所有員工的平均工資 平均工資的等級
select T.dno,S.grade,T.avg_sal
from(select D.deptno as "dno",AVG(sal) as "avg_sal"
from emp "E" join dept "D" on E.deptno = D.deptno
group by D.deptno) "T" join salgrade "S" on T.avg_sal >= S.losal and T.avg_sal<= S.hisal
--3.求出emp表中所有領導的姓名
select ename from emp
where empno in(select mgr from emp)
--4.求出平均薪水最高的部門的編號和部門的平均工資
select T.deptno,D.dname,T.avg_sal from
(select deptno,AVG(sal) as "avg_sal"
from emp group by deptno) "T" join dept "D" on T.deptno = D.deptno
--5.把工資大于所有員工平均工資最低的前3個人的姓名 工資 部門編號輸出
select top 3 E.ename,E.sal,E.deptno,D.dname
from emp "E" join dept "D" on E.deptno = D.deptno
where E.sal > (select AVG(sal) from emp )
order by sal asc
外鏈接
定義:不但返回滿足連接條件的所有記錄,而且會返回部分不滿足條件的記錄
(1)左外鏈接
用左表的第一行分別和右表的所有行進行聯接,如果有匹配的行,則一起輸出,如果右表有多行匹配,則結果集輸出多行,如果沒有匹配行sql查詢條件左連接,則結果集中只輸出一行,該輸出行左邊為左表第一行內容,右邊全部輸出null然后再用左表第二行和右邊所有行進行聯接,如果有匹配的行,則一起輸出,如果右表有多行匹配,則結果集輸出多行,如果沒有匹配行,則結果集中只輸出一行,該輸出行左邊為左表第二行內容,右邊全部輸出null以此類推,直至左邊所有行連接完畢因為右邊很可能出現有多行和左邊的某一行匹配,所以左聯接產生的結果集的行數很可能大于 左邊表的記錄的總數左向外聯接的結果集包括子中指定的左表的所有行,而不僅僅是聯接列所匹配的行。如果左表的某行在右表中沒有匹配行則在相關聯的結果集行中右表的所有選擇列表列均為空值。
實際意義:返回一個事物及其該事物的相關信息,如果該事物沒有相關信息,則輸出null
(2)右外鏈接
與左鏈接相同
自連接
--在不使用聚合函數的情況下,查詢工資最高的人員信息
select * from emp "E"
where E.sal not in (select E1.sal from emp "E1" join emp "E2" on E1.sal < E2.sal)
聯合的用法【union】
聯合:是將兩個表縱向合并
-- 聯合的使用
select emp.ename,emp.sal,E.ename "上級名稱"
from emp join emp "E"
on emp.mgr = E.empno
union select ename ,sal,'boss'
from emp where mgr is null
聯合注意事項:
六 分頁查詢【top只有有】 公式:
假設每頁顯示n條記錄,當前要顯示的是第m頁表名是A 主鍵是A_id
** top n ***
from A
*where A_id not in ( top (m-1)n A_id from A)
案例:
-- 分頁查詢每頁顯示4人信息(按照工資降序)
--1-3
select top 3 * from emp
order by sal desc
-- 4-6
select top 3 * from emp
where empno not in(select top 3 empno from emp
order by sal desc)
order by sal desc
-- 7-9
select top 3 * from emp
where empno not in(select top 6 empno from emp
order by sal desc)
order by sal desc
-- 10-12
select top 3 * from emp
where empno not in(select top 9 empno from emp
order by sal desc)
order by sal desc
-- 13-14
select top 3 * from emp
where empno not in(select top 12 empno from emp
order by sal desc)
order by sal desc
關鍵字:主鍵自動增長;【當被刪除一個數據后:會打斷主鍵連續自增】
create table tableT
(
empid int identity(1, 1),
ename nvarchar(20) not null
)
-- 插入數據
insert into tableT values ('aaaa');
insert into tableT values ('bbbb');
insert into tableT values ('cccc');
insert into tableT values ('dddd');
--刪除empid為4的記錄select* from emp
delete from tableT delete from emp where empid =4
--因為執行delet時empid為4,所以下一句插入時empid會從5開始
insert into tableT values('eeee')
delete from tableT where empid = 5
dbcc checkident('tableT',reseed,3) --此行把emp表中identity字段的初始值重新設置為3
insert into tableT values('eeee') --此時插入記錄時,empid為4,上一行已經把empid設置成了3
select* from emp
學習思維:
七 視圖:
視圖可以作為一個臨時表處理:可以簡化查詢
-- 視圖操作
--創建vs1視圖
CREATE VIEW vs1
AS SELECT ename,deptno FROM emp
-- 使用視圖vs1查數據
select sal from vs1