这是我课本上习题作业,没有参考答案,是我自己写的。语句可能有错误!!!
有误请指正,谢谢。
《数据库原理与应用教程》第4版 何玉洁 编著 机械工业出版社
下面两个地址数据库内容一样 百度网盘里有txt的数据
配套数据库sql server(主要是想赚积分)https://download.csdn.net/download/jivder/11162509
百度网盘链接(有txt数据文件): 链接:https://pan.baidu.com/s/14HiZPpX1lSljrTgmjQLinw
提取码:q72d
CREATE TABLE [dbo].[Course](
[Cno] [nchar](4) NOT NULL,
[Cname] [varchar](50) NOT NULL,
[Credit] [tinyint] NULL,
[Semester] [tinyint] NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[SC](
[Sno] [nchar](7) NOT NULL,
[Cno] [nchar](4) NOT NULL,
[Grade] [tinyint] NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[Student](
[Sno] [nchar](7) NOT NULL,
[Sname] [nchar](10) NOT NULL,
[Sex] [nchar](2) NULL,
[Sage] [tinyint] NULL,
[Sdept] [nchar](20) NULL
) ON [PRIMARY]
SELECT * FROM SC
SELECT Sname, Sage FROM Student WHERE Sdept = '计算机系'
select Sno, Cno, Grade from SC
where Grade between 70 and 80
select Sname, Sage from Student
where Sage between 18 and 20 and Ssex = '男'
and Sdept = '计算机系'
select top 1 Grade from SC
where Cno = 'c001'
select MAX(Sage) as 最大年龄, MIN(Sage) as 最小年龄 from Student
select Sdept as 系别, COUNT(Sno) as 学生人数 from Student
group by Sdept
select Cno as 课程号, COUNT(Sno) as 选课人数, MAX(Grade) as 最高分
from SC
group by Cno
select Student.Sno 学号, Student.Sname 姓名, count(Grade) 门数, sum(Grade) 总成绩 from SC
join Student on Student.Sno = SC.Sno
group by SC.Sno, Student.Sname, Student.Sno
order by count(Grade)
select Student.Sno 学号,sum(Grade) 总成绩 from SC
join Student on Student.Sno = SC.Sno
group by SC.Sno, Student.Sname, Student.Sno
having sum(Grade) > 200
select Sname, Sdept from Student S
join SC on S.Sno = SC.Sno
where Cno = 'c002'
select Student.Sname, Cno, Grade from Student
join SC on Student.Sno = SC.Sno
where Grade > 80
order by Grade desc
select sno, Sname, Sdept from Student left join SC on student.sno= sc.sno
where sc.sno is null
select Cname, Semester from Course
where Semester = (
select Semester from Course where Cname='Java')
select Sname 姓名, Sdept 所在系, Sage 年龄 from Student
where Sage = (select Sage from Student where Sname = '李勇')
select Sname, Sdept from Student
where Sno in (
select Sno from SC
where Cno = 'c001')
(2)查询数学成绩80分以上的学生的学号、姓名、课程号和成绩。
select student.Sno, sname,Sdept, Cno, Grade from Student
join SC on Student.Sno = SC.Sno
where Sdept = '数学系' and Grade >= 80
(3)查询计算机系考试成绩最高的学生的姓名。
select Sname from Student x join SC on x.Sno = SC.Sno
where Sdept = '计算机系' and Grade = (
select MAX(Grade) from SC join Student y on y.Sno = SC.Sno
where Sdept = '计算机系')
(4)查询数据结构考试成绩最高的学生的姓名、所在系、性别和成绩。
select Sname, Sdept, Ssex, Grade from Student s join SC on s.Sno = SC.Sno
where Grade = (
select MAX(Grade) from SC join Course c on c.Cno = SC.Cno
where Cname = '数据结构')
select sname,sdept from Student
where sno in (
select sno from sc
where cno in (select cno from Course
where Cname != 'Java'))
select Sname from Student
left join SC on Student.Sno = SC.Sno
where Sdept = '计算机系' and SC.Sno is null
COL1 | COL 2 | COL2 |
---|---|---|
B1 | ||
1 | B2 | C2 |
2 | B3 |
insert into test_t(COL2) values ('B1')
insert into test_t(COL1,COL2) values ('1','B1')
insert into test_t(COL1,COL2) values ('2','B3')
delete from SC
where Grade < 50
delete from Course
where Cno not in (select Cno from SC)
select Sno, Cname, Grade from SC
join Course on sc.Cno= Course.Cno
where Grade <60
删除语句
delete from SC
where Grade < 60 and Cno in (
select Cno from Course
where Cname = 'Java')
update Course set Credit = Credit + 2
where Semester = '2'
update Course set Credit = 3
where Cname = 'Java'
update Student set Sage = Sage + 1
where Sdept = '计算机系'
select sname, Sdept, grade from Student s
join SC on s.Sno = sc.Sno
where Sdept= '信息系' and Cno in (
select Cno from Course
where Cname = '计算机文化学')
select Sdept 系名, COUNT(*) 人数 into Dept_Age
from Student
where Sage > = 20
group by Sdept
select student.Sno 学号, Sname 姓名,grade 成绩,
case
when Grade >= 90 then '好'
when Grade between 80 and 89 then '较好'
when Grade between 70 and 79 then '一般'
when Grade between 60 and 69 then '较差'
when Grade < 60 then '差'
end as 考试情况
from Student
join SC on Student.Sno = SC.Sno
where Sdept = '计算机系'and Cno in (
select Cno from Course where Cname='Java')
select s.sno 学号, count(Sc.Cno) 选课门数,
case
when count(sc.Cno) > 4 then '多'
when count(sc.Cno) between 2 and 4 then '一般'
when count(sc.Cno) between 1 and 2 then '少'
when count(sc.Cno) = 0 then '未选'
end as 选课情况
from student s left join sc on s.sno = sc.sno
group by s.Sno
order by COUNT(sc.cno) desc
update Course set Credit = Credit +
case
when Semester between 1 and 2 then 5
when Semester between 3 and 4 then 3
when Semester between 5 and 6 then 1
else 0
end
select Cname 课程名, Credit 学分, Semester 开课学期
from Student S join SC on S.Sno = sc.Sno
join Course c on c.Cno = sc.Cno
where Sname = '李勇'
intersect
select cname 课程名, credit 学分, Semester 开课学期
from Student s join sc on s.Sno = sc.Sno
join Course c on c.Cno = sc.Cno
where Sname = '王大力'
select Cname 课程名, Credit 学分
from Student S join SC on S.Sno = sc.Sno
join Course c on c.Cno = sc.Cno
where Sname = '李勇' and Semester = 3
except
select cname 课程名, credit 学分
from Student s join sc on s.Sno = sc.Sno
join Course c on c.Cno = sc.Cno
where Sname = '王大力' and Semester = 3
select Cname 课程名, Credit 学分
from Student S join SC on S.Sno = sc.Sno
join Course c on c.Cno = sc.Cno
where Sname = '李勇' and Credit > 3
intersect
select cname 课程名, credit 学分
from Student s join sc on s.Sno = sc.Sno
join Course c on c.Cno = sc.Cno
where Sname = '王大力' and Credit > 3