數據庫實驗二:數據庫操作

實驗二:數據庫操作

一、實驗目的

1.熟悉數據表結構及使用特點;
2.熟悉使用Management Stuio界面方式管理數據表數據;
3.熟悉使用T-SQL語句管理數據表數據;
4.掌握SELECT語句的基本語法和查詢條件表示方法;
5.掌握查詢條件表達式和使用方法;
6.掌握GROUP BY 子句的作用和使用方法;
7.掌握HAVING子句的作用和使用方法;
8.掌握ORDER BY子句的作用和使用方法;
9.掌握連接查詢的表示及使用;
10.掌握嵌套查詢的表示及使用;
11.瞭解集合查詢的表示及使用。

二、實驗環境

已安裝SQL Server 2008的計算機;
具有局域網環境,有固定IP;

三、實驗學時

2學時

四、實驗要求

1.瞭解數據庫查詢以及其他操作;
2.瞭解數據庫查詢以及其他操作的實現方式;
3.完成實驗報告;

五、實驗內容及步驟

首先建立student,SC,course表並導入數據:
在這裏插入圖片描述
① 查詢所有SC表數據:
在這裏插入圖片描述
② 執行語句select sno,sage from student where sdept=‘計算機系’:
在這裏插入圖片描述
③ 執行語句select sno,cno,grade from SC where grade between 70 and 80:
在這裏插入圖片描述
④ 執行語句select sname,sage from student where sdept='計算機系’and sage between 10 and 20 and ssex like ‘男%’
在這裏插入圖片描述
⑤ 執行語句select max(grade) as '最高分’from SC where cno=‘c001’
在這裏插入圖片描述
⑤ 執行語句select max(grade) as '最高分’from SC where cno=‘c001’
在這裏插入圖片描述
⑥ 執行語句select max(sage) as ‘最大年齡’,min(sage) as '最小年齡’from student where sdept=‘計算機系’
在這裏插入圖片描述
⑦ 執行語句select sdept,count() as’學生人數’from student group by sdept
在這裏插入圖片描述
⑨ 執行語句select cno,grade,count(
)‘選課門數’ from SC group by cno,grade order by cno
在這裏插入圖片描述
⑩ 執行語句select sno, sum(grade) from sc group by sno having sum(grade)>200
在這裏插入圖片描述
⑪ 執行語句 select sname,sdept from student s
join SC on s.sno =SC.sno
where cno=‘c002’
在這裏插入圖片描述
⑫ 執行語句 select sname,cno,grade from student,SC
where student.sno=SC.sno and SC.grade>80
order by grade desc
在這裏插入圖片描述
⑬ select s.sno,sname,sdept from student s left join SC on
s.sno =SC.sno where SC.cno is null
在這裏插入圖片描述
⑭ select c2.cname,c1.semester from course c1 join
course c2 on c1.semester=c2.semester where
c1.cname=‘java’ and c2.cname!=‘java’
在這裏插入圖片描述
⑮ select s2.sname,s2.sdept,s2.sage from student s1 join
student s2 on s1.sage =s2.sage where s1.sname =‘李勇’
and s2.sname!=‘李勇’
在這裏插入圖片描述
⑯ (a)select sname,sdept from student
where sno in(select sno from sc where cno=‘c001’)
(b)select student.sno,sname,cno,grade from student join SC
在這裏插入圖片描述
在這裏插入圖片描述
(c) select sname from student s join SC on s.sno=SC.sno where sdept=‘計算機系’ and grade=(select max(grade) from SC join student s on s.sno=SC.sno where sdept=‘計算機系’)
在這裏插入圖片描述
(d) select sname,sdept,ssex,grade from student s
join SC on s.sno=SC.sno
join course on SC.cno=course.cno
where cname=‘數據結構’
and grade=(select max(grade) from SC join student s on SC.cno=course.cno where sdept=‘計算機系’ )
在這裏插入圖片描述
17.查詢沒有選修JAVA課程的學生的姓名和所在系。
select Sname,sdept from Student where sno not in (select sno from SC join Course on Course.cno=SC.cno where cname=‘Java’)
18.查詢計算機系沒有選課的學生的姓名和性別。
select Sname,sdept from Student where Sdept='計算機系’and sno not in (select SC.sno from Student join SC on Student.Sno=SC.Sno where sdept=‘計算機系’)
19.創建一個新表,表名爲test_t,其結構爲(COL1,COL2,COL3)
CREATE TABLE test_t (
COL1 INT,
COL2 CHAR (10) NOT NULL,
COL3 CHAR (10)
);
20.刪除考試成績低於50分學生的選課記錄。
delete from SC where grade<50
21.刪除沒人選的課程記錄。
delete from Course where cno not in (select cno from SC )
22.刪除計算機系JAVA成績不及格學生的JAVA選課記錄。
delete from SC where grade<60 and cno in (select cno from Course where cname=‘java’) and sno in (select sno from Student where sdept=‘計算機系’)
23.將第二學期開設的所有課程的學分增加2分。
update Course set credit=credit+2 where semester=2
24.將JAVA的學分改爲3分。
update Course set credit=3 where cname=‘Java’
25.將計算機系學生的年齡加1歲。
update Student set sage=sage+1 where sdept=‘計算機系’
26.將信息系學生的”計算機文化學“課程的考試成績加5。
update SC set grade=grade+5 where sno in (select sno from Student where sdept=‘信息系’) and cno=(select cno from Course where cname=‘計算機文化學’)
27.查詢每個系年齡大於等於20的學生的人數,並將結果保存到一個新永久表Dept_Age中。
select sdept,count(*) as 人數 into Dept_Age from Student where sage>=20 and group by sdept
28.
select Student.sno 學號,sname 姓名,grade 成績,case when grade>=90 tnen '好 ’
when grade between 80 and 89 tnen ‘較好’
when grade between 70 and 79 tnen ‘一般’
when grade between 80 and 89 tnen ‘較差’
when grade between 70 and 79 tnen ‘差’
end as 成績情況
from Student join SC on Student.sno=SC.sno where cno=(select cno from Course where cname=‘Java’) and sdept=‘計算機系’
29.
select Student.sno 學號,count(SC.sno) 選課門數,
case when count(SC.sno)>=6 tnen '多 ’
when count(SC.sno) between 3 and 5 tnen ‘一般’
when count(SC.sno) between 1 and 2 tnen ‘較少’
when count(SC.sno)=0 tnen ‘未選課’
end as 選課情況
from SC left join Student on Student.Sno=SC.Sno group by sno
30.
Select
case credit=credit+
when semester between 1 and 2 tnen 5
when semester between 3 and 4 tnen 3
when semester between 5 and 6 tnen 1
Else 0
end
from Course
31.
select cname,semester,credit from Course join SC on Course.cno=SC.Cno join Student on Student.Sno=SC.Sno where sname=‘李勇’ union select cname,semester,credit from Course join SC on Course.Cno=SC.Cno join Student on Student.Sno=SC.Sno where sname=‘王大力’
32.
select Course.Cno, cname,credit from Course join SC on Course.cno=SC.Cno join Student on Student.Sno=SC.Sno where sname=‘李勇’ and semester=3 except select Course.Cno,cname,credit from Course join SC on Course.Cno=SC.Cno join Student on Student.Sno=SC.Sno where sname=‘王大力’ and semester=3
33.
select Course.Cno, cname,credit from Course join SC on Course.cno=SC.Cno join Student on Student.Sno=SC.Sno where sname=‘李勇’ and credit>3 intersect select Course.Cno,cname,credit from Course join SC on Course.Cno=SC.Cno join Student on Student.Sno=SC.Sno where sname=‘王大力’ and credit>3

六、出現問題及解決辦法

(10)題報錯:
select sno,grade count() '總成績’from SC group by grade having count()>200
在這裏插入圖片描述
第一次修改:
select sno,grade count() ‘總成績’ from SC group by grade having count sum(grade) > 200
在這裏插入圖片描述
(11)題報錯:
select sname,sdept from student s
join SC on s.sno =SC.sno
join course d on d.sdept= student.sdept
where cno=‘c002’
在這裏插入圖片描述
(12)題報錯:
select sno,cno,grade from SC s
join student on SC.sno =student.sno
where grade>80
(13)題報錯:
select sno,sname,sdept from student
where sno not in(
select sno from SC join course
on SC.cno=course.cno
where cname=
)
在這裏插入圖片描述
繼續報錯:
select s.sno,sname,sdept from student s left join SC on
s,sno =SC.sno where SC.cno is null
在這裏插入圖片描述
(14)題報錯:
select s2.sname,s2.sdept,s2.sage from student s1 join
student s1 on s1.sage =s2.sage where s1.same =‘李勇’
and s2.sname!=‘李勇’
在這裏插入圖片描述
(16)題報錯:
select sno,sname,cno,grade from student join SC
on student.sno=SC.sno where sdept=‘數學系’ and sno in (select sno from SC where grade>80)
在這裏插入圖片描述
16)題d報錯:
select sname,sdept,ssex,grade from student s
join sc on s.sno=SC.sno
join course on SC.cno=course.cno
where cname=‘數據結構’
and grade=(select max(grade) from SC join count(*) on SC.cno=course.cno
where cname=‘數據結構’)
在這裏插入圖片描述
繼續報錯:
select sname,sdept,ssex,grade from student s
join SC on s.sno=SC.sno
join course on SC.cno=course.cno
where cname=‘數據結構’
and grade=(select max(grade) from SC where cname='數據結構’group by sno )
在這裏插入圖片描述
上次使用後,由於修改了SQLserver中數據庫的默認存儲位置,此外還把已有數據庫位置改變了,所有出現恢復掛起狀態:
在這裏插入圖片描述
使用代碼:
USE master
GO
ALTER DATABASE 學生數據庫 SET SINGLE_USER
GO
ALTER DATABASE 學生數據庫 SET EMERGENCY
GO
DBCC CHECKDB(學生數據庫,REPAIR_ALLOW_DATA_LOSS)
go
ALTER DATABASE 學生數據庫 SET ONLINE
GO
ALTER DATABASE 學生數據庫 SET MULTI_USER
GO
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章