實驗一資料庫查詢

2022-12-12 16:27:04 字數 3613 閱讀 5335

一.實驗目的:

使學生掌握sql server query analyzer的使用方法,加深對sql和t-sql語言的查詢語句的理解。熟練掌握表的基本查詢,連線查詢和巢狀查詢,以及掌握資料排序和資料分組的操作方法。

二.實驗原理:

select [all|distinct] 《目標列表示式》[,《目標列表示式》]…

from 《表名或檢視名》[,《表名或檢視名》]…

[where 《條件表示式》]

[group by 《列名1> [h**ing 《條件表示式》]]

[order by 《列名2> [asc|desc]];

三.實驗方法:

將查詢需求用t-sql語言表示;在sql server query analyzer的輸入區中輸入t-sql查詢語句;設定 query analyzer的結果區為standard execute(標準執行)或execute to grid(網格執行)方式;發布執行命令,並在結果區中檢視查詢結果;如果結果不正確,要進行修改,直到正確為止。

四.實驗內容:

1. 查詢所有的usa國家的員工.

select employeeid

from employees

where country like 'usa%'

2. 查詢出生地為london的員工

select employeeid

from employees

where city like 'london%'

3. 查詢所有7號員工簽訂的訂單.

select orderdate

from orders

where employeeid='7'

4. 查詢所有 firstname為 『robert』的員工簽訂的訂單

select orderdate

from orders,employees

wherefirstname like'robert%'

5. 檢視所有訂單

select orderdate

from orders

6. 查詢所有沒有按時發貨的訂單

select orderdate

from orders

where shippeddate > requireddate or shippeddate > requireddate

7. 查詢哪些顧客的訂單在規定時間前沒有發貨。給出顧客名,**

select orderdate,contactname,phone

from orders,customers

where shippeddate > requireddate and

8. 查詢所有已經訂購了貨物但尚未發貨的顧客資訊(分別用連線,in和exists實現)

select distinct customers.*

from customers,orders

where is null and

select *

from customers

where customerid in(

select customerid

from orders

where shippeddate is null

)select *

from customer

where exists(

select *

from orders

where shippeddate is null and

)9. 檢視每個員工簽訂的訂單的總金額,按照金額由小到大排序(分別用連線,in和exists實現)

select

from orders,products

where exists(

select *

from employees,products

)and

group by

order by sum(unitprice*quantity*(1-discount)+freight)

select

from orders,products

where employeeid in(

select employeeid

from employees,products

)and

group by

order by sum(unitprice*quantity*(1-discount)+freight)

select

from orders,products,employees

where and

group by

order by sum(unitprice*quantity*(1-discount)+freight)

10. 假設運費要顧客自己承擔,計算每個顧客為自己的訂單付出的金額

select customerid,quantity*(1-discount)*unitprice+freight

from orders,products

where

11. 查詢所有與1號和9號員工都簽過訂單的顧客

select distinct customerid

from customers

where customerid in(

select customerid

from orders

where employeeid='1' and customerid in(

select customerid

from orders

where employeeid='9'))

12. 查詢簽訂訂單金額超過100000美元的員工姓名

select firstname,sum(unitprice*quantity*(1-discount)+freight) as money

from employees,orders,products

where and

group by firstname

h**ing sum(unitprice*quantity*(1-discount)+freight)>100000

13. 查詢員工firstname為robert的所有同事

select *

from employees

where city in(

select city

from employees

where firstname='robert')

14. 查詢已經現已經退休的職工,假設退休年齡為60,給出他的姓名,工作年限,住址,**。

select firstname,lastname,address,homephone,60-datediff(yy,birthdate,hiredate) as workyear

from employees

where datediff(yy,birthdate,getdate())>=60

五.實驗總結:

通過本次實驗知道了sql語句的使用規則,更加熟練的掌握了資料庫語言。在本次實驗中也遇到了各種問題:在執行過程**現了錯誤,原因是在多表查詢時沒有進行連線。

在有也因為粗心問題忘寫各種符號了,英文單詞寫錯了等等。總之在這次實驗中從改正錯誤中學到了很多,不能粗心大意,還要熟練掌握sql語句的規則。

資料庫實驗三 查詢

南昌大學實驗報告 學生姓名 丁金芝學號 6100511080 專業班級 管理科學與工程類112班 實驗型別 驗證 綜合 設計 創新實驗日期實驗成績 一 實驗目的 1 掌握select語句的基本語法 2 了解select語句中各子句的作用和特點 3 掌握select語句的統計函式的作用和用法 4 掌握...

實驗三 資料庫的巢狀查詢實驗

實驗目的 加深對巢狀查詢語句的理解。實驗內容 使用in 比較符 any或all和exists操作符進行巢狀查詢操作。實驗步驟 一.使用帶in謂詞的子查詢 1.查詢與 劉晨 在同乙個系學習的學生的資訊 select from student where sdept in select sdept fr...

實驗一資料庫 資料表的建立

1 掌握建立資料庫的方法 2 掌握建立資料表的方法 3 掌握檢視 修改表的定義 屬性的方法 4 掌握資料插入命令insert的使用。5 掌握資料庫的備份和恢復方法。1 實驗環境 sql server 2 基本操作 1 建立資料庫 資料庫名為自己的學號姓名 裡面包含學生 表名 xs 課程 表名 kc ...