SQL综合练习部分参考答案

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

针对学生课程选课数据库进行以下操作

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

相关文档
最新文档