1. 程式人生 > >SQL Server資料庫基礎

SQL Server資料庫基礎

建庫建表:

if DB_id('TicketManager ')is not null --判斷資料庫是否已經存在
drop database TicketManager --移除已經存在的資料庫
go
create database TicketManager --新建資料庫
on
(
    name='TicketManager ',--資料庫名稱
    filename='E:\CS架構複習案例\TicketManager\DB\TicketManager.mdf'--編寫建立資料庫的位置
)
use TicketManager--開啟資料庫
if OBJECT_ID('
Ticket')is not null--判斷表是否已經存在 drop table Ticket--移除已經存在的資料庫 go create table Ticket--新建表 ( TrainID int identity(1,1) primary key,--列 資料型別 約束 TrainNO nvarchar(50) not null unique, leaveCity nvarchar(50) not null, arriveCity nvarchar(50) not null, LeaveTime nvarchar(50) not null
, SeatPrice int not null, BedPrice int not null ) insert Ticket values('N5545','北京','武漢','6:11','150','200') --新增資料 insert Ticket values('N5546','武漢','北京','6:12','150','200') insert Ticket values('N5547','廣東','江西','4:13','150','200') insert Ticket values('N5548','江西','廣東','7:14','150','200') insert Ticket values(
'N5549','珠海','深圳','6:15','50','70')

54個SQL經典案例入門到精通:

--1、查詢員工的編號、姓名、部門和出生日期,如果出生日期為空值,顯示日期不詳,並按部門排序輸出,日期格式為yyyy-mm-dd。
select emp_no,emp_name,dept,isnull(convert(char(10),birthday,120),'日期不詳') birthday
from employee
order by dept

--2、查詢與喻自強在同一個單位的員工姓名、性別、部門和職稱
select emp_no,emp_name,dept,title
from employee
where emp_name<>'喻自強' and dept in
(select dept from employee
where emp_name='喻自強')

--3、按部門進行彙總,統計每個部門的總工資
select dept,sum(salary)
from employee
group by dept

--4、查詢商品名稱為14寸顯示器商品的銷售情況,顯示該商品的編號、銷售數量、單價和金額
select a.prod_id,qty,unit_price,unit_price*qty totprice
from sale_item a,product b
where a.prod_id=b.prod_id and prod_name='14寸顯示器'

--5、在銷售明細表中按產品編號進行彙總,統計每種產品的銷售數量和金額
select prod_id,sum(qty) totqty,sum(qty*unit_price) totprice
from sale_item
group by prod_id

--6、使用convert函式按客戶編號統計每個客戶1996年的訂單總金額
select cust_id,sum(tot_amt) totprice
from sales
where convert(char(4),order_date,120)='1996'
group by cust_id

--7、查詢有銷售記錄的客戶編號、名稱和訂單總額
select a.cust_id,cust_name,sum(tot_amt) totprice
from customer a,sales b
where a.cust_id=b.cust_id
group by a.cust_id,cust_name

--8、查詢在1997年中有銷售記錄的客戶編號、名稱和訂單總額
select a.cust_id,cust_name,sum(tot_amt) totprice
from customer a,sales b
where a.cust_id=b.cust_id and convert(char(4),order_date,120)='1997'
group by a.cust_id,cust_name

--9、查詢一次銷售最大的銷售記錄
select order_no,cust_id,sale_id,tot_amt
from sales
where tot_amt=
(select max(tot_amt)
from sales)

--10、查詢至少有3次銷售的業務員名單和銷售日期
select emp_name,order_date
from employee a,sales b 
where emp_no=sale_id and a.emp_no in
(select sale_id
from sales
group by sale_id
having count(*)>=3)
order by emp_name

--11、用存在量詞查詢沒有訂貨記錄的客戶名稱
select cust_name
from customer a
where not exists
(select *
from sales b
where a.cust_id=b.cust_id)

--12、使用左外連線查詢每個客戶的客戶編號、名稱、訂貨日期、訂單金額訂貨日期不要顯示時間,日期格式為yyyy-mm-dd按客戶編號排序,同一客戶再按訂單降序排序輸出
select a.cust_id,cust_name,convert(char(10),order_date,120),tot_amt
from customer a left outer join sales b on a.cust_id=b.cust_id
order by a.cust_id,tot_amt desc

--13、查詢16M DRAM的銷售情況,要求顯示相應的銷售員的姓名、性別,銷售日期、銷售數量和金額,其中性別用男、女表示
select emp_name 姓名, 性別= case a.sex when 'm' then ''
when 'f' then '' 
else ''
end,
銷售日期= isnull(convert(char(10),c.order_date,120),'日期不詳'),
qty 數量, qty*unit_price as 金額
from employee a, sales b, sale_item c,product d
where d.prod_name='16M DRAM' and d.prod_id=c.prod_id and
a.emp_no=b.sale_id and b.order_no=c.order_no

--14、查詢每個人的銷售記錄,要求顯示銷售員的編號、姓名、性別、產品名稱、數量、單價、金額和銷售日期
select emp_no 編號,emp_name 姓名, 性別= case a.sex when 'm' then ''
when 'f' then '' 
else ''
end,
prod_name 產品名稱,銷售日期= isnull(convert(char(10),c.order_date,120),'日期不詳'),
qty 數量, qty*unit_price as 金額
from employee a left outer join sales b on a.emp_no=b.sale_id , sale_item c,product d
where d.prod_id=c.prod_id and b.order_no=c.order_no

--15、查詢銷售金額最大的客戶名稱和總貨款
select cust_name,d.cust_sum
from customer a,
(select cust_id,cust_sum
from (select cust_id, sum(tot_amt) as cust_sum
from sales
group by cust_id ) b
where b.cust_sum = 
( select max(cust_sum)
from (select cust_id, sum(tot_amt) as cust_sum
from sales
group by cust_id ) c )
) d
where a.cust_id=d.cust_id 

--16、查詢銷售總額少於1000元的銷售員編號、姓名和銷售額
select emp_no,emp_name,d.sale_sum
from employee a,
(select sale_id,sale_sum
from (select sale_id, sum(tot_amt) as sale_sum
from sales
group by sale_id ) b
where b.sale_sum <1000 
) d
where a.emp_no=d.sale_id 

--17、查詢至少銷售了3種商品的客戶編號、客戶名稱、商品編號、商品名稱、數量和金額
select a.cust_id,cust_name,b.prod_id,prod_name,d.qty,d.qty*d.unit_price
from customer a, product b, sales c, sale_item d
where a.cust_id=c.cust_id and d.prod_id=b.prod_id and 
c.order_no=d.order_no and a.cust_id in (
select cust_id
from (select cust_id,count(distinct prod_id) prodid
from (select cust_id,prod_id
from sales e,sale_item f
where e.order_no=f.order_no) g
group by cust_id
having count(distinct prod_id)>=3) h )

--18、查詢至少與世界技術開發公司銷售相同的客戶編號、名稱和商品編號、商品名稱、數量和金額
select a.cust_id,cust_name,d.prod_id,prod_name,qty,qty*unit_price
from customer a, product b, sales c, sale_item d
where a.cust_id=c.cust_id and d.prod_id=b.prod_id and 
c.order_no=d.order_no and not exists
(select f.*
from customer x ,sales e, sale_item f
where cust_name='世界技術開發公司' and x.cust_id=e.cust_id and
e.order_no=f.order_no and not exists
( select g.*
from sale_item g, sales h
where g.prod_id = f.prod_id and g.order_no=h.order_no and
h.cust_id=a.cust_id)
)

19、查詢表中所有姓劉的職工的工號,部門,薪水
select emp_no,emp_name,dept,salary
from employee
where emp_name like '劉%'

20、查詢所有定單金額高於2000的所有客戶編號
select cust_id
from sales
where tot_amt>2000

21、統計表中員工的薪水在4000-6000之間的人數
select count(*)as 人數
from employee
where salary between 4000 and 6000

22、查詢表中的同一部門的職工的平均工資,但只查詢"住址"是"上海市"的員工
select avg(salary) avg_sal,dept 
from employee 
where addr like '上海市%'
group by dept

23、將表中住址為"上海市"的員工住址改為"北京市"
update employee 
set addr like '北京市'
where addr like '上海市'

24、查詢業務部或會計部的女員工的基本資訊。
select emp_no,emp_name,dept
from employee 
where sex='F'and dept in ('業務','會計')

25、顯示每種產品的銷售金額總和,並依銷售金額由大到小輸出。
select prod_id ,sum(qty*unit_price)
from sale_item 
group by prod_id
order by sum(qty*unit_price) desc

26、選取編號界於'C0001''C0004'的客戶編號、客戶名稱、客戶地址。
select CUST_ID,cust_name,addr
from customer 
where cust_id between 'C0001' AND 'C0004'

27、計算出一共銷售了幾種產品。
select count(distinct prod_id) as '共銷售產品數'
from sale_item 

28、將業務部員工的薪水上調3%。
update employee
set salary=salary*1.03
where dept='業務'

29、由employee表中查找出薪水最低的員工資訊。
select *
from employee
where salary=
(select min(salary )
from employee )

30、使用join查詢客戶姓名為"客戶丙"所購貨物的"客戶名稱","定單金額","定貨日期","電話號碼"
select a.cust_id,b.tot_amt,b.order_date,a.tel_no
from customer a join sales b
on a.cust_id=b.cust_id and cust_name like '客戶丙'

31、由sales表中查找出訂單金額大於"E0013業務員在1996/10/15這天所接每一張訂單的金額"的所有訂單。
select *
from sales
where tot_amt>all
(select tot_amt 
from sales 
where sale_id='E0013'and order_date='1996/10/15')
order by tot_amt

32、計算'P0001'產品的平均銷售單價
select avg(unit_price)
from sale_item
where prod_id='P0001'

33、找出公司女員工所接的定單
select sale_id,tot_amt
from sales
where sale_id in 
(select sale_id from employee
where sex='F')

34、找出同一天進入公司服務的員工
select a.emp_no,a.emp_name,a.date_hired
from employee a
join employee b
on (a.emp_no!=b.emp_no and a.date_hired=b.date_hired)
order by a.date_hired

35、找出目前業績超過232000元的員工編號和姓名。
select emp_no,emp_name
from employee 
where emp_no in
(select sale_id
from sales 
group by sale_id
having sum(tot_amt)<232000)

36、查詢出employee表中所有女職工的平均工資和住址在"上海市"的所有女職工的平均工資
select avg(salary)
from employee
where sex like 'f'
union
select avg(salary)
from employee
where sex like 'f' and addr like '上海市%'

37、在employee表中查詢薪水超過員工平均薪水的員工資訊。
Select * 
from employee 
where salary>( select avg(salary) 
from employee)

38、 找出目前銷售業績超過10000元的業務員編號及銷售業績,並按銷售業績從大到小排序。
Select sale_id ,sum(tot_amt)
from sales 
group by sale_id 
having sum(tot_amt)>10000
order by sum(tot_amt) desc

39、 找出公司男業務員所接且訂單金額超過2000元的訂單號及訂單金額。 
Select order_no,tot_amt
From sales ,employee
Where sale_id=emp_no and sex='M' and tot_amt>2000

40、 查詢sales表中訂單金額最高的訂單號及訂單金額。
Select order_no,tot_amt from sales 
where tot_amt=(select max(tot_amt) from sales)

41、 查詢在每張訂單中訂購金額超過4000元的客戶名及其地址。
Select cust_name,addr from customer a,sales b 
where a.cust_id=b.cust_id and tot_amt>4000

42、 求出每位客戶的總訂購金額,顯示出客戶號及總訂購金額,並按總訂購金額降序排列。
Select cust_id,sum(tot_amt) from sales
Group by cust_id 
Order by sum(tot_amt) desc

43、 求每位客戶訂購的每種產品的總數量及平均單價,並按客戶號,產品號從小到大排列。
Select cust_id,prod_id,sum(qty),sum(qty*unit_price)/sum(qty)
From sales a, sale_item b
Where a.order_no=b.order_no
Group by cust_id,prod_id
Order by cust_id,prod_id

44、 查詢訂購了三種以上產品的訂單號。
Select order_no 
from sale_item
Group by order_no
Having count(*)>3

45、 查詢訂購的產品至少包含了訂單3號中所訂購產品的訂單。
Select distinct order_no
From sale_item a
Where order_no<>'3'and not exists ( 
Select * from sale_item b where order_no ='3' and not exists 
(select * from sale_item c where c.order_no=a.order_no and c.prod_id=b.prod_id))

46、 在sales表中查找出訂單金額大於"E0013業務員在1996/11/10這天所接每一張訂單的金額"的所有訂單,並顯示承接這些訂單的業務員和該訂單的金額。
Select sale_id,tot_amt from sales
where tot_amt>all(select tot_amt 
from sales 
where sale_id='E0013' and order_date='1996-11-10') 

47、 查詢末承接業務的員工的資訊。
Select *
From employee a
Where not exists 
(select * from sales b where a.emp_no=b.sale_id)

48、 查詢來自上海市的客戶的姓名,電話、訂單號及訂單金額。
Select cust_name,tel_no,order_no,tot_amt
From customer a ,sales b
Where a.cust_id=b.cust_id and addr='上海市'

49、 查詢每位業務員各個月的業績,並按業務員編號、月份降序排序。
Select sale_id,month(order_date), sum(tot_amt) 
from sales 
group by sale_id,month(order_date)
order by sale_id,month(order_date) desc

50、 求每種產品的總銷售數量及總銷售金額,要求顯示出產品編號、產品名稱,總數量及總金額,並按產品號從小到大排列。 
Select a.prod_id,prod_name,sum(qty),sum(qty*unit_price)
From sale_item a,product b
Where a.prod_id=b.prod_id 
Group by a.prod_id,prod_name
Order by a.prod_id
51、 查詢總訂購金額超過'C0002'客戶的總訂購金額的客戶號,客戶名及其住址。
Select cust_id, cust_name,addr
From customer
Where cust_id in (select cust_id from sales 
Group by cust_id
Having sum(tot_amt)>
(Select sum(tot_amt) from sales where cust_id='C0002'))

52、 查詢業績最好的的業務員號、業務員名及其總銷售金額。
select emp_no,emp_name,sum(tot_amt)
from employee a,sales b
where a.emp_no=b.sale_id
group by emp_no,emp_name
having sum(tot_amt)=
(select max(totamt)
from (select sale_id,sum(tot_amt) totamt
from sales
group by sale_id) c)

53、 查詢每位客戶所訂購的每種產品的詳細清單,要求顯示出客戶號,客戶名,產品號,產品名,數量及單價。
select a.cust_id, cust_name,c.prod_id,prod_name,qty, unit_price
from customer a,sales b, sale_item c ,product d
where a.cust_id=b.cust_id and b.order_no=c.order_no and c.prod_id=d.prod_id

54、 求各部門的平均薪水,要求按平均薪水從小到大排序。
select dept,avg(salary) 
from employee
group by dept 
order by avg(salary)

SQL語法格式:

select <查詢的物件>from<物件所在的檔案>
Delete<刪除的物件>from<物件所在的檔案>
insert <新增的表(新增的列)> select <'新增的資料'>或values<('新增的資料')>
update <修改物件所在表> set <修改物件>=<值> where <條件物件>=<'條件內容'>

create database【新增型別—資料庫】 <資料庫名>

on【新增到什麼位置】
(
name='<資料庫名>',
filename='E:\SQL資料庫\Tourism.Mdf'【'<路徑資訊+資料庫名.Mdf>'】
)
create table【新增型別—表】 <表名>
(
<列名> <資料格式> <約束>【identity(1,1) primary key—主鍵】,
<列名> <資料格式> <約束>【check—其他約束】
)
foreign key(<列名>) references <表名>(<列名>)【外來鍵—從本表的列與目標表的列相連】

select * into <備份表名> from <目標表名> where 1=0【不相等的條件只備份列名、約束,不備份資料】