SQL Server基礎SQL指令碼之內外連線、交叉連線;函式、子查詢
阿新 • • 發佈:2022-05-03
程式碼大概200行左右 本系列,幾乎都是程式碼,記得當時寫的時候用的是微軟的官方例項資料庫AdventureWorks_Data.mdf、AdventureWorks_Log.ldf來執行的。 下載連結:連結: https://pan.baidu.com/s/1pMdLz6N 密碼: xvhu 或者回復“AdventureWorks”來獲取連結。
use AdventureWorks --切換到AdventureWorks資料庫 --建立Student表和Marks表,用於操作各種聯接 create table Student --建立學生表,裡面包含兩列,學號和姓名 ( RollNo char(4), Name varchar(20) ) insert into Student values --向Student表中插入5行記錄 ('S001','Allen'), ('S002','Jhon'), ('S003','David'), ('S004','Stefen'), ('S005','Steve') create table Marks --建立成績表,裡面包含三列,學號,RDMBS和Math ( RollNo char(4), RDBMS int, Math int ) insert into Marks values --向成績表中插入三行記錄 ('S001',98,76), ('S002',67,64), ('S003',76,96) select * from Student select * from Marks --1. 內聯接 INNTER JOIN- 顯示滿足公共列中聯接條件的行 inner可加可不加 --問題:查詢有考試成績的學生的學號,姓名,RDBMS成績和Math成績 -----練習:已知 select * from HumanResources.Employee select * from HumanResources.EmployeeAddress go --顯示:EmployeeID, Title, AddressID 的匹配資訊 ----inner join --給表名一個別名 --2. 外聯接 - 顯示包含一個表中的所有行以及另外一個表中匹配行的結果集,不匹配的用NULL值填充 --(1)左外聯接 - 返回LEFT OUTER JOIN 左側的表的所有行,以及右側指定的表的匹配行,若右邊找不到匹配項,顯示NULL值 --(2)右外聯接 - 返回RIGHT OUTER JOIN 右側的表的所有行,以及左側指定的表的匹配行,若左邊找不到匹配項,顯示NULL值 --(3)完整外聯接 - 左外聯接和右外聯接的組合,返回兩個表中所有匹配的行和不匹配的行,匹配記錄只顯示一次 --3. 交叉聯接(Cross Join) Product運算,將一個表中的每一行與另一個表中的 -------------------- create table Course --建立Course表,裡面包含一列CourseName (CourseName varchar(10)) insert into Course values --向Course表中插入兩行記錄 ('English'), ('C Language') select * from Student select * from Course --要求顯示結果為每個學生都修一遍Course表中的所有課程 --4. 等值聯接 --使用=號聯接表的內聯接 --練習:查詢員工的員工編號,所屬部門名稱和工資 聯接多個表 select * from HumanResources.Employee select * from HumanResources.EmployeeDepartmentHistory select * from HumanResources.Department --5. 自聯接 - 同一個表當成兩張表使用,一個表中的一行聯接另一個表中的一行 select * from HumanResources.Employee select a.EmployeeID,a.Title,a.ManagerID,b.Title from --查詢員工的編號,職位,其主管的員工編號和其主管的職位 HumanResources.Employee a join HumanResources.Employee b on a.ManagerID=b.EmployeeID --根據其主管的員工編號找到對應的職位 select a.EmployeeID,a.Title,a.ManagerID,b.Title from --查詢員工的編號,職位,其主管的員工編號和其主管的職位 HumanResources.Employee a , HumanResources.Employee b where a.ManagerID=b.EmployeeID --根據其主管的員工編號找到對應的職位 ---------------------- (二)、使用子查詢查詢資料---------------------------- --子查詢:將一個select的查詢結果作為另外一個select查詢的輸入/條件,查詢裡面的查詢 --1. 使用比較運算子,IN和EXISTS關鍵字 --比較運算子,以=號為主 select * from HumanResources.Employee --問題:查詢和員工編號為1的員工職位(Title)相同的員工的資訊 --IN 多個值 --問題:查詢和員工編號為1,3,4的員工的職位相同的員工的資訊 --EXISTS關鍵字-檢查一組記錄是否存在,返回True或False --if exists(select * from databases where name='UDB') drop database UDB ------------------ select * from HumanResources.Employee select * from HumanResources.EmployeeDepartmentHistory --2. 使用修改過的比較運算子 ALL,ANY --問題:查詢 --查詢RDBMS成績高於S002或者高於S003的學生的資訊 select * from Marks go --查詢RDBMS成績高於S002並且高於S003的學生的資訊 --3. 使用聚合函式 --問題:查詢RDBMS成績最高的學生的學號和RDBMS成績 --4. 使用巢狀子查詢 --子查詢裡面可以包含一個或多個子查詢,這樣叫做巢狀子查詢 --問題:查詢工資最高的員工的編號 HumanResources.EmployeePayHistory select * from HumanResources.EmployeePayHistory --問題:查詢工資最高的員工所在的部門編號 select * from HumanResources.EmployeeDepartmentHistory --5. 使用關聯子查詢 - 根據外部查詢作為評估依據的查詢 --問題:查詢每個部門最早加入的員工的資訊 select * from HumanResources.EmployeeDepartmentHistory a where StartDate= ( select min(StartDate) from HumanResources.EmployeeDepartmentHistory where DepartmentID=a.DepartmentID ) --6. APPLY運算子 --合併兩個查詢的結果集, --------------------------------------------- create table Depositor --建立Depositor表,儲存儲蓄使用者資訊,表中有兩列,客戶姓名和儲蓄賬戶 ( "客戶姓名" varchar(20), "儲蓄賬戶" char(3) ) insert into Depositor values --向Depositor表中插入兩條記錄 ('Allen','D01'), ('David','D02') create table Borrower --建立Borrower表,儲存貸款使用者資訊,表中有兩列,客戶姓名和貸款賬戶 ( "客戶姓名" varchar(20), "貸款賬戶" char(3) ) insert into Borrower values --向Borrower表中插入兩行記錄 ('Amy','B11'), ('David','B12') -------------------------------------- select * from Depositor select * from Borrower --CROSS APPLY - 返回外部結果集中與內部結果集匹配的行 select a.客戶姓名,a.儲蓄賬戶,br.貸款賬戶 from Depositor a --外部結果集 cross apply (select * from Borrower b where b.客戶姓名=a.客戶姓名) br --br為內部結果集的別名 --OUTER APPLY - 返回外部結果集中所有的行,即使內部結果集中沒有找到此行 select a.客戶姓名,a.儲蓄賬戶,br.貸款賬戶 from Depositor a --外部結果集 outer apply (select * from Borrower b where b.客戶姓名=a.客戶姓名) br --br為內部結果集的別名