针对学生课程选课数据库进行以下操作
create database student
use student
go
t_student( s_number CHAR(10) primary key,
Sname CHAR(20) ,
Ssex CHAR(2) ,
Birthday CHAR(10),
polity CHAR(5)
sage int DEFAULT 20,
sdept CHAR(20) )
t_course(
c_number char(10) PRIMARY KEY,
c_name Char(30) NOT NULL,
hours int,
credit int CHECK(Ccredit>0),
)
t_score(
s_number CHAR(10) NOT NULL,
c_number CHAR(10) NOT NULL,
score int, CHECK(([grade] >= 0 and [grade] <= 100))
PRIMARY KEY (Sno,Cno),
FOREIGN KEY(Sno) REFERENCES t_student(s_number),
FOREIGN KEY(Cno) REFERENCES t_course(c_number)
)
查看表关系图,使用“数据库”属性页中的“文件”,在“所有者”中输入有效的数据库登录名。
1、查询t_student表中的所有记录的s_name和sage列。
2、 查询学生所有的系别即不重复的sdept列。
3、 查询t_student表的所有记录。
4、 查询全体学生的出生年份和性别,并给出生年份列命名为“BIRTHDAY”。
select year(getdate())-year(birthday) BIRTHDAY,sex from t_student
5、 查询t_score表中分数大于85分的学生学号。
略
6、 查询t_score表中成绩在60到80之间的所有记录。
select * from t_score where score between 60 and 80
7、 查询t_score表中成绩为85,86或88的记录。
select * from t_score where score in (85,86,88)
8、 查询Student表中不姓“王”的同学记录。
select * from t_student where s_name not like '王%'
9、 查询全校同学名字中第二个字为“小”的同学的具体情况。
select * from t_student where s_name like'_小%';
11、查询学生表中姓赵、钱、孙、李的同学。
select * from t_student where Sname like'[赵钱孙李]%';
12、查询t_score表中没有成绩的同学的学号。
select * from t_score where score is NULL
13、查询t_student表中“数学系”或性别为“女”的同学记录。(复合条件查询,集合查询两种方式)
略。
14、查询计算机系男生的具体情况。
使用where子句,略。
15、以年龄降序查询t_student表的所有记录。
使用order by 子句,略。
16、以c_number升序、score降序查询t_score表的所有记录。
select * from t_score order by c_number,score desc
17、查询所有学生的s_name、c_name和score列。(连接查询,嵌套查询两种方式)
三张表的联合查询,略。
18、查询所有选修“单片机原理”课程的同学的姓名和成绩。(连接查询,嵌套查询)
连接查询:
select s_name,score
from t_student a,t_course b,t_score c
where a.s_number=c.s_number and b.c_number=c.c_number and b.c_name='单片机原理'
嵌套查询:
select s_name,score
from t_student a,t_score b
where a.s_number=b.s_number and b.c_number IN
(select c_number from t_course where c_name ='单片机原理')
19、查询和“李海”同性别的同学 的姓名. (嵌套查询)
select s_name from t_student where sex=(select sex from t_student where s_name='李海')
20、查询所有同学的基本情况和选课情况,包括未选课的同学。(外连接查询)
delete from t_score where s_number='B0451109'
select a.s_number,s_name,b.c_number,score
from t_student a left outer join t_score b on a.s_number=b.s_number
21、查询选修10010218号课程且成绩高于80分的同学的名字。
select s_name from t_student,t_score
where t_student.s_number=t_score.s_number
and c_number='10010218' and score>80
24、查询非数学系的比计算机系所有学生的年龄都大的学生姓名。(用ANY,ALL)
SELECT S_NAME FROM t_STUDENT
WHERE SDEPT<>'数学系' AND SAGE >ALL
(SELECT SAGE FROM T_STUDENT WHERE SDEPT='数学系');
25、查询存在有85分以上成绩的课程c_number.
SELECT DISTINCT c_number FROM t_score WHERE score>85
26、查询数学系同学的人数。
select count(*) from t_student where sdept='数学系'
27、查询数学系同学所选课程的平均分。
select AVG(score) from t_score,t_student where t_score.s_number=t_student.s_number and sdept='数学系'
28、查询t_student表中年龄最大和最小的同学的具体情况。
select * from t_student
where sage=(select max(sage) from t_student) or sage=(select min(sage) from t_student)
29、查询最高分同学的s_number、c_number列
select s_number,c_number from t_score
where score=(select max(score) from t_score)
30、查询10010218号课程的平均分。
select c_number,AVG(score)
from t_score
group by c_number
having c_number='10010218'
31、查询选修了10010218号课程且成绩比该课程平均成绩低的同学的学号和成绩。
select s_number,score
from t_score a
where c_number ='10010218'
and score<
(select AVG(score) from t_score b group by c_number having
c_number='10010218')
32、查询选修了10010218号课程或30020215号课程的同学的学号。(复合条件查询,集合查询)
select distinct s_number from t_score where c_number='10010218' or c_number='30020215'
注意:由于
33、查询各个课程号及相应的选课人数。
select c_number,count(*) from t_score group by c_number
34、查询选修了3门以上课程且总分大于200分的同学的学号。
select s_number from t_score group by s_number having count(*)>=3 and sum(score)>200