实验二查询部分及查询作业答案

发布 2022-07-02 07:16:28 阅读 4048

student表:

列名含义数据类型约束。

sno学号字符串,长度为7 主码。

sname姓名字符串,长度为10 非空。

ssex性别字符串,长度为2

sage年龄整形。

sdept所在系字符串,长度为20

course表:

列名含义数据类型约束。

cno课程号字符串,长度为10 主码。

cname课程名字符串,长度为20 非空。

ccredit 学分字节。

semester 学期字节

sc表: 列名含义数据类型约束。

sno学号字符串,长度为7主码。

引用student的sno作为外码。

cno课程号字符串,长度为10主码。

引用course的cno作为外码。

grade成绩小整形

xklb修课类别字符串,长度为4

请写明创建以上三张基本表的语句。

3.将如下数据插入建好的三张表中。

student表。

学号姓名性别年龄所在系。

9512101','李勇','男',19,'计算机系'

9512102','刘晨','男',20,'计算机系'

9512103','王敏','女',20,'计算机系'

9521101','张立','男',22,'信息系'

9521102','吴宾','女',21,'信息系'

9521103','张海','男',20,'信息系'

9531101','钱小平','女',18,'数学系'

9531102','张大力','男',19,'数学系'

course表。

课程号课程名学分学期。

c01','计算机文化学',3,1

c02','vb',2,3

c03','计算机网络',4,7

c04','数据库基础',6,6

c05','高等数学',8,2

c06','数据结构',5,4

sc表。学号课程号成绩修课类别。

9512101','c01',90,'必修'

9512101','c02',86,'选修'

9512101','c06',null,'必修'

9512102','c02',78,'选修'

9512102','c04',66,'必修'

9521102','c01',82,'选修'

9521102','c02',75,'选修'

9521102','c04',92,'必修'

9521102','c05',50,'必修'

9521103','c02',68,'选修'

9521103','c06',null,'必修'

9531101','c01',80,'选修'

9531101','c05',95,'必修'

9531102','c05',85,'必修'

请写明使用的数据插入语句。

4.数据查询(写明对应的查询语句)

1)查询学生选课表中的全部数据。

select * from sc

2)查询计算机系的学生的姓名、年龄。

select sname,sage from student where sdept=’计算机系’

3)查询成绩在70~80分之间的学生的学号、课程号和成绩。

select sno,cno,grade from sc where grade between 70 and 80

4)查询计算机系年龄在18~20之间且性别为“男”的学生的姓名、年龄。

select sname,sage from student where (sage between 18 and 20) and ssex=’男’and sdept=’计算机系’

5)查询课程号为“c01”的课程的最高的分数。

select max(grade) from sc where cno=’c01’

6) 查询计算机系学生的最大年龄和最小年龄。

select max(sage),min(sage) from student where sdept=’计算机系’

7) 统计每个系的学生人数。

select count(*)sdept from student group by sdept

8) 统计每门课程的选课人数和考试最高分。

select count(*)max(grade),cno from sc group by cno

9) 统计每个学生的选课门数和考试总成绩,并按选课门数升序显示结果。

select count(*)sum(grade),sno from sc group by sno order by count(*)asc

10) 查询总成绩超过200分的学生,要求列出学号、总成绩。

select sum(grade),sno from sc group by sno h**ing sum(grade)>200

11) 查询选修了“c02”号课程的学生的姓名和所在系。

select sname,sdept from student where sno in(select sno from sc where cno=’c02’)

12) 查询成绩80分以上的学生的姓名、课程号和成绩,并按成绩降序排列结果。

select sname,cno,grade from student inner join sc on where grade>80 order by grade desc

13) 查询哪些课程没有人选,要求列出课程号和课程名。

select from course as c left join sc on where is null

14) 用子查询实现如下查询:

1)查询选修了c01号课程的学生的姓名和所在系。

select sname,sdept from student where sno in(select sno from sc where cno=’c01’)

2)查询数学系成绩80分以上的学生的学号、姓名。

select sno,sname from student where sdept=’数学系’and sno in(select sno from sc where grade>80)

3)查询计算机系考试成绩最高的学生的姓名。

select sname from student as s inner join sc on where sdept=’计算机系’and grade=(select max(grade) from sc where sno in(select sno from student where sdept='计算机系'))

create table test_t(

col1 int,col2 char(10) not null,col3 char(10)

insert into test_t values(null,’b1’,null)

insert into test_t values(1,’b2’,’c2’)

insert into test_t(col1,col2) values(2,’b3’)

delete from sc where grade<50

update sc set grade=grade+10 where cno=’c01’

update sc set grade=grade+10 where sno in(

select sno from student where sdept=’计算机系’) and cno in(

select from course where cname=’计算机文化学’)

create table student

sno char(7) primary key,sname varchar(10) not null,ssex char(2),sage int,sdept varchar(10)

create table course

cno char(3) primary key,cname varchar(20) not null,ccredit byte,semester byte

create table sc

sno char(7) references student(sno),cno char(3) references course(cno),grade smallint,xklb char(4)

primary key(sno,cno)

insert into student values('9512101','李勇','男',19,'计算机系')

实验二SQL简单查询 答案

实验目的 掌握select语句的基本语法格式和用法,能够灵活使用select语句完成提出的查询要求。实验内容 使用select语句完成以下查询 1.查询全体学生的学号 姓名,要求查询结果按学号的升序进行排序。select sno,sname from student order by sno 2.查...

实验5SQL查询答案

1.查询zgda表中所有职工的信息。sele from zgda 2.查询zgda表中所有职工的职称字段值。sele职称 from zgda 3.查询zgda表中所有职工的职称种类,即去掉重复的职称信息。sele dist 职称 from zgda 4.查询zgda表中所有职工的姓名 性别 年龄 职...

实验五答案SQL语句查询

1 列出所有不姓刘的所有学生。selectsname,snofromstudent wheresnamenotlike 刘 2 列出姓 沈 且全名为3个汉字的学生selectsnamefromstudent wheresnamelike 沈 3 显示在1985年以后出生的基本信息。selectsno...