(1)在表中添加1个新的系,dno为“d01006”,并为该系添加2个专业信息和2条教师信息,其他具体数据自拟。
insert department values('d01006','物理系',null)
insert major values('100166','量子力学','工学','d01006')
insert major values('100167','物理新材料','工学','d01006')
insert teacher values('010008','简','男','2002-05-06','教授','博士','d01006')
insert teacher values('010018','碎镜','男','2002-05-06','教授','博士','d01006')
(2)将“d01006”系所有年龄超过45岁且职称为讲师的教师的职称改为“副教授”。
update teacher
set tRank='副教授'
where tRank='讲师' and YEAR(getdate())-YEAR(tbirth)>45
或
update teacher set tRank='副教授'
where tNO=
(select tNO from teacher
where (YEAR(GETDATE())-YEAR(tbirth))>45 and tRank='讲师'and dNo='d01006')
(3)将“d01006”号院系所有的专业名称前面加上“互联网”三个字。
update major
set mName='互联网' + mName
where dNO='d01006'
(4)删除“d01006”号院系及其的所有相关信息。
delete from teacher where dNo='d01006'
delete from major where dNo='d01006'
delete from department where dNO='d01006'
(5)假设有某项业务:学校首次引进一门新课程“大数据理财”,并将教师编号为“010203”的“李明”老师调动到编号为“0101”的“大数据金融系”来开设该课程,课程计划开始2学分的考试课。请使用SQL语句记录上述业务。(未给定数据,请自拟。)
insert into course(cNO,cName)
values('99989','大数据理财')
update teacher
set dNo='0101'
where tNO='010203'
insert into course_class(ccNO,cNO,tNO,Credit,ExamType)
values('121554221221','99989','010203',2,'考试')
(1)
SELECT COUNT(*) AS ’该专业女生人数’
FROM student
WHERE mno=‘100165’ AND ssex=‘女’
查看100165专业的女生人数
(2)
SELECT DISTINCT SUBSTRING(sname,1,1)
FROM student
查看所有学生的姓氏
(3)
SELECT sno,sname,ssex,mno
FROM student
WHERE mno IN (‘100165’,’201148’,’100838’)
查看‘100165’,’201148’,’100838’三个专业的学生的学号,名字,性别和专业号
(4)
CREATE VIEW v_1
AS
SELECT ccno,mark
FROM student_course
WHERE sno =‘100212201’ AND mark >ANY
(SELECT mark FROM student_course
WHERE sno =‘100212208’ )
创建100212201学生课程成绩比100212208学生所有成绩都高的课程编号和成绩的视图
(5)
SELECT DISTINCT s.sno,sname,dname
FROM student AS s,department AS d, student_course as sc ,major as m
WHERE s.mno=m.mno and d.dno=m.dno AND s.sno=sc.sno AND mark <60
查看分数小于60分的学生的学号,名字和系别
(6)SELECT sno,sname,mname
FROM student AS s,major AS m
WHERE s.mno=m.mno AND s.sno IN
(SELECT DISTINCT sno
FROM student_course
WHERE mark <60 )
查看分数小于60的学生的学号,姓名和专业名称
(1)查询所有女同学的选课情况,要求列出学号、姓名、开课号、分数。
select s.sno,s.sName,ccNO,Mark from student_course as sc,student as s
where sc.sNO=s.sNO and s.sSex='女'
-- 变一下题目:
--(1)查询所有女同学(每一个)的选课情况,要求列出学号、姓名、开课号、分数。
--这里就不能使用内连接了,因为所有的女同学都要显示,
--所以必须要用外连接,这里使用左外连接为例
select s.sno,sname,ccno,mark
from student s left join student_course SC on s.sno=sc.sno
where s.ssex='女'
(2)查询所有学生的总成绩,最高成绩,要求列出学号、总成绩和最高成绩。
select sNO,SUM(mark) as 总成绩,MAX(mark) as 最高成绩 from student_course group by sno
(3)查询出姓张的学生或者姓名中带有“秋”的学生
select * from student where sName like '张%' or sname like '%秋%'
或
select * from student where left(sName,1)='张' or sName like '%秋%'
(4)查询出每门课程的课程名、平均分、最低分、最高分
select c.cName,平均分,最低分,最高分 from course as c right join
(select c.cNO,平均分,最低分,最高分 from course_class as c right join
(select ccNO,AVG(mark) as 平均分,MIN(mark) as 最低分,MAX(mark) as 最高分
from student_course as sc group by ccNO ) m on c.ccNO=m.ccNO) mm on c.cNO=mm.cNO
或(下面的好点)
select cname,AVG(mark),MIN(mark),MAX(mark)
from student_course as sc,course as c,course_class as cc
where sc.ccNO=cc.ccNO and cc.cNO =c.cNO
group by cname
(5)查询出平均分大于80分,且至少选修了2门课程的学生学号。
select sNo,AVG(mark) as 总成绩 from student_course
group by sNO having AVG(mark)>80 and count(*)>2
(6)查询选修课程号为’010104’且成绩在90以上的学生学号、姓名和成绩
select sc.sNO,s.sname,Mark from student_course as sc,student as s where ccNO=(
select ccNO from course_class where cNO='010104') and mark>90 and sc.sNO=s.sNO
(7)查询出每一门课程的间接先行课(即先行课的先行课)
select c1.cno,c2.cpno
from course as c1,course as c2
where c1.cpno=c2.cNO
(8)查询高等数学课程的成绩高于刘晨的学生学号及高等数学成绩
select sno,Mark from student_course where ccNO=(
select ccno from course_class where cNO=(
select cNO from course where cName='高等数学'))
and Mark>
(select mark from student_course where sNO=(
select sNO from student where sName='刘晨') and
ccNO=(select ccno from course_class where
cNO=(select cNO from course where cName='高等数学')) )
或
select sc.sno,mark
from student_course as sc,course_class as cc,course as c
where sc.ccNO=cc.ccNO and cc.cNO =c.cNO and c.cName ='高等数学'
and Mark>(select mark
from student as s,student_course as sc,course_class as cc,course as c
where sc.ccNO=cc.ccNO and cc.cNO =c.cNO and s.sNO=sc.sNO
and c.cName ='高等数学' and sName='刘晨')
go
(9)查询其他系中比计算机系某一学生年龄小的学生(即年龄小于计算机系年龄最大者的学生)
select s.sNO,s.sName,s.sBirth from student s,major m ,department d
where s.mNO=m.mNO and m.dNO=d.dNO and d.dName!='计算机系' and s.sBirth>(
select MIN( s.sBirth) from student s,major m ,department d
where s.mNO=m.mNO and m.dNO=d.dNO and d.dName='计算机系')
或
select *
from student as s,department as d,major as m
where s.mNO =m.mNO and m.dNO =d.dNO and d.dName <>'计算机系'
and sBirth >all(
select sBirth
from student as s,department as d,major as m
where s.mNO =m.mNO and m.dNO =d.dNO and d.dName ='计算机系')
(10)查询同时选修了“数据库”“金融学”“统计学”三门课程的学生姓名。(要求使用EXISTS)
select s.sname from student s where exists
(select * from student_course sc,course_class cc,course c
where sc.ccNO=cc.ccNO and cc.cNO=c.cNO and c.cName in ('数据库','金融学','统计学')
) group by s.sname having count(*) >3
或
select sname
from student as s
where not exists(
select *
from course as c,course_class as cc
where c.cNO=cc.cNO and c.cName in('数据库','金融学','统计学')
and not exists(
select *
from student_course as sc
where sc.sNO=s.sno and sc.ccNO=cc.ccNO ))
(11)查询同时选修了“数据库”“金融学”“统计学”三门课程的学生姓名。(要求不能使用EXISTS)
select s.sname from student s where s.sNO in
(select sno from student_course sc,course_class cc,course c
where sc.ccNO=cc.ccNO and cc.cNO=c.cNO and c.cName in ('数据库','金融学','统计学')
) group by s.sname having count(*) >3
或
select sname
from student
where sNO in(
select sNO
from course as c,course_class as cc,student_course as sc
where c.cNO=cc.cNO and cc.ccNO =sc.ccNO
and c.cName in('数据库','金融学','统计学')
group by sNO
having COUNT(*)=3)
go
(1)如何查询出“金融学”成绩排名第5到第10之间的学生学号。
select top 6 sNO,Mark from student_course
where ccNO=(
select ccNO from course_class where
cNO=(select cNO from course where cName='金融学'))
and sno not in(
select top 4 sNO from student_course
where ccNO=(
select ccNO from course_class where
cNO=(select cNO from course where cName='金融学')) order by Mark desc) order by Mark desc
(2)假设有两个结构完全相同的表,一个是运营数据表A,一个是历史数据表B,由于操作失误导致两个表中出现了部分完全相同的数据,请将完全相同的数据从A中删除?
我觉得就是对于B表的主键进行遍历,如果A表中出现了和B表相同的主键,
就将这条记录从A表中删除就可以达到目的了
stuManage2数据库的备份,下载还原即可