1. 程式人生 > >資料庫實驗二

資料庫實驗二

/*對xs表增加身份證號碼屬性列,要求是18位的字元型別*/
alter table xs
add 身份證號碼 char(18)

/*通過生日計算年齡*/
select *,DATEDIFF(yy,CONVERT(varchar(10),CAST(SUBSTRING(xs.身份證號碼,7,8) AS datetime),120),GETDATE()) 年齡
from xs;

/*顯示不同專業不同性別的人數*/
select xs.性別,xs.專業,count(*) 人數
from xs
group by xs.專業,xs.性別;


/*1.查詢有直接先行課的課程的課號,課名和先行課號*/
select kc.課程號,kc.先行課號
from kc
where kc.先行課號!=''

/*2.查詢先行課號是“J001”號課程的課號和課名*/
select kc.課程號,kc.課程名
from kc
where kc.課程號='J001'

/*3.查詢所有的網路工程系姓李,張,王的同學的學號和姓名*/
select xs.學號,xs.姓名
from xs
where xs.專業='網路工程' and (xs.姓名 like '李%' or xs.姓名 like '張%' or xs.姓名 like '王%')

/*4.查詢不在網路工程和資訊管理專業學習的學生的學號和姓名,系別,並對查詢結果按照專業的升序和學號的降序排序*/
select xs.學號,xs.姓名,xs.性別
from xs
where xs.專業!='網路工程' and xs.專業!='資訊管理'
order by xs.專業,xs.學號 desc

/*5.查詢每門課不及格的學生的人數,顯示課號和人數*/
select cj.課程號,count(*) 人數
from cj
where cj.成績<60
group by cj.課程號

/*6.查詢年齡不在30-35之間的網路工程系的學生的學號,姓名和年齡*/
select xs.學號,xs.姓名,DATEDIFF(yy,xs.出生時間,GETDATE()) 年齡
from xs
where  DATEDIFF(yy,xs.出生時間,GETDATE()) not between 30 and 35

/*7.查詢沒有選修‘J001’號課程的學生的學號(注意去掉重複的元組)*/
--缺少一門都未選課的
/*select distinct 學號
from cj
where not exists
(
select *
from cj cj1
where cj1.課程號 = 'J001' and cj.學號=cj1.學號
)*/

select distinct 學號 
from xs
where not exists(
select *
from cj
where xs.學號=cj.學號   and 課程號 = 'J001')

select distinct xs.學號 
from xs
except
select cj.學號 from cj where cj.課程號='J001'

/*8.查詢每個學生的學號,姓名,出生年份,並給出生年份起別名為chusheng*/
select xs.學號,xs.姓名,Datename(year,xs.出生時間) chusheng
from xs

/*9.查詢每個學生的學號,姓名和出生日期(出生日期根據身份證號碼查詢)*/
select xs.學號,xs.姓名,DATEDIFF(yy,CONVERT(varchar(10),CAST(Substring(xs.身份證號碼,7,8) as datetime),120),GETDATE()) 出生日期
from xs

/*10.查詢選修J001課程成績排名第一的同學的學號和成績*/
select top 1 cj.學號,cj.成績
from cj
order by cj.成績 desc

/*11. 查詢所有名字中含有’明’或者’麗’的同學的學號,姓名*/
select xs.學號,xs.姓名
from xs
where xs.姓名 like '%明%' or xs.姓名 like '%麗%'

/*12. 查詢資訊管理專業年齡超過20歲的學生的人數*/
select count(*)
from xs
where xs.專業='資訊管理' and DATEDIFF(yy,xs.出生時間,GETDATE())>20

/*13. 查詢平均成績超過80分的課程的課程號和平均成績*/
select cj.課程號,AVG(cj.成績)
from cj
group by cj.課程號
having AVG(cj.成績)>80

/*14. 查詢每個專業所有姓張的人數*/
select xs.專業,count(*) 人數
from xs
where xs.姓名 like '張%'
group by xs.專業

/*15. 查詢各種姓氏的人數(假設沒有複姓)*/
select left(xs.姓名,1),count(left(xs.姓名,1)) 人數
from xs
group by left(xs.姓名,1)

/*16.查詢選修課程超過5門的學生的學號和選課門數,以及平均成績*/
select cj.學號,count(cj.課程號) 門數,AVG(cj.成績) 平均成績
from cj
group by cj.學號
having count(cj.課程號)>5

/*17. 查詢選修‘J001’課程的成績排名前五的學生的學號和成績*/
select top 5 cj.學號,cj.成績
from cj
order by cj.成績 desc

/*18.查詢每個學生的最低分和選課門數*/
select min(cj.成績),count(cj.課程號) 選課門數
from cj
group by cj.學號


/*19. 查詢各個專業各種性別的人數*/
select xs.專業,xs.性別,count(xs.性別) 人數
from xs
group by xs.專業,xs.性別


/*20.查詢各個專業男生的人數*/
select xs.專業,count(xs.性別) 人數
from xs
where xs.性別='男'
group by xs.專業

/*21. 列出有二門以上課程(含兩門)不及格的學生的學號及該學生的平均成績*/
select cj.學號,AVG(cj.成績)
from cj
where cj.成績<60
group by cj.學號
having count(cj.成績)>=2

select cj.學號,AVG(cj.成績)
from cj
group by cj.學號
having count(case when cj.成績<60 then 1 end)>=2

/*22. 顯示學號第五位或者第六位是1、2、3、4或者9的學生的學號、姓名、性別、年齡及專業*/
select xs.學號,xs.姓名,xs.性別,DATEDIFF(yy,xs.出生時間,GETDATE()) 年齡,xs.專業
from xs
where xs.學號 like '____[1,2,3,4,9]%' or xs.學號 like '_____[1,2,3,4,9]%'


/*23. 顯示選修課程數最多的學號及選修課程數最少的學號*/
select cj.學號
from cj
group by cj.學號
having count(cj.課程號)>=all(select count(cj.課程號)
from cj
group by cj.學號)

select cj.學號
from cj
group by cj.學號
having count(cj.課程號)<=all(select count(cj.課程號)
from cj
group by cj.學號)


/*24. 查詢選修了A001或者A002或者J001或者J002課程的學生的學號和課程號*/
select cj.學號,cj.課程號
from cj
where cj.課程號 in ('A001','A002','J001','J002')

/*25. 查詢姓名為兩個字的不同姓氏的人數,輸出姓氏,人數*/
select left(xs.姓名,1) 姓氏,count(left(xs.姓名,1)) 人數
from xs
where len(xs.姓名)=2
group by left(xs.姓名,1) 

--建立spj資料庫
create database SPJ

create table S(
sno char(2) not null primary key,
sname char(8),
status char(2),
city char(6)
)

create table P(
pno char(2),
pname char(8),
color char(4),
weight char(2)
)

create table J(
jno char(2),
jname char(8),
city char(4)
)

create table SPJ(
sno char(2),
pno char(2),
jno char(2),
qit int
)


--1.	求供應工程J1零件的供應商號碼SNO
select SPJ.sno
from SPJ
where SPJ.jno='J1'

--2.	求查詢每個工程使用不同供應商的零件的個數
select SPJ.jno,SPJ.pno,count(SPJ.sno) 個數
from SPJ
group by SPJ.jno,SPJ.pno

--3.	求供應工程使用零件P3數量超過200的工程號JNO
select SPJ.jno
from SPJ
where SPJ.pno='P3' and SPJ.qit>200

--4.	求顏色為紅色和藍色的零件的零件號和名稱
/*select pno,pname
from p p1
where color='紅' and p1.pname in (select p2.pname
from p p2
where p2.color='藍'
)*/

select distinct pno, pname
from p
where color in ('藍','紅')

--5.	求使用零件數量在200-400之間的工程號
select SPJ.jno
from SPJ
group by SPJ.jno
having sum(SPJ.qit) between 200 and 400

--6.	查詢每種零件的零件號,以及使用該零件的工程數
select SPJ.pno,count(DISTINCT SPJ.jno) 工程數
from SPJ
group by SPJ.pno