当前位置:   article > 正文

数据库练习_将元组(“4001”,王晓英

将元组(“4001”,王晓英

数据库原理与应用每日一练

一、建表

学生表(s)

create table S
(
    sno  char(4) comment '学号',
    sname varchar(25) comment '学生姓名',
    sex  char(2) comment '性别',
    age  int comment '年龄',
    primary key(sno)
);
insert into S values('1001','张小丽','男',19),
                    ('1002','刘强','女',18),
                    ('2001','李志强','女',20),
                    ('3002','赵丹','男',19);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

课程表(c)

create table C
(
    cno  char(4) comment '课程号',
    cname varchar(25) comment '课程名',
    teacher varchar(25) comment '任课老师',
    primary key(cno)
);
insert into C values('C1','高等数学','王华'),
                    ('C2','数据结构','李大利'),
                    ('C3','操作系统','周明'),
                    ('C4','数据库原理','周明');
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

成绩表(sc)

create table SC
(
    sno  char(4) comment '学号',
    cno  char(4) comment '课程号',
    grade int comment '成绩',
    primary key(sno,cno),
    foreign key(sno) references S(sno),
    foreign key(cno) references C(cno)
);
insert into SC values('1001','C1',80),
                     ('1001','C3',83),
                     ('2001','C4',90),
                     ('3002','C2',76);

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

二、题目

1.设有如图所示的关系S、C和 SC,它们分别表示学生、课程和学生选课。试写出下列操作的SQL语句。
S:
sno	sname	sex	age
1001	张小丽	男	19
1002	刘强	女	18
2001	李志强	女	20
3002	赵丹	男	19

C:
cno	cname	teacher
C1	高等数学	王华
C2	数据结构	李大利
C3	操作系统	周明
C4	数据库原理	周明

SC:
sno	cno	grade
1001	C1	80
1001	C3	83
2001	C4	90
3002	C2	76

(1)查询所有学生的学号和姓名
(2)查询学号为‘1001’的学生的选课情况,结果包括学号、课程号、成绩。
(3)查询选修了‘C2’号课程的学生姓名。
(4)查询男同学的姓名和年龄,查询结果按年龄降序排列。
(5)将元组( ‘4001’,王晓英,女,20)插入到学生表中
(6)查询所有选修“王华”老师所教课程的学生学号及姓名。
(7)将对课程表的查询权限授予用户U3。
(8)查询年龄大于19岁的学生学号及姓名。
(9)查询姓李的学生,结果包含学号、姓名和年龄。
(10)查询选修了数据库课程的学生学号和成绩。
(11)查询出每名学生所选课程的平均成绩,结果包含学号和平均成绩,并按学号升序排列
(12)将学号为1001的学生各科成绩均提高5分。
(13)写出创建SC表的SQL语句。
(14)查询男同学的选课信息,结果包含姓名,课程号。
(15)创建视图SCVIEW,视图包含学生学号,学生姓名,课程号,课程名,成绩。
(16)为S表的sname创建索引,索引名为index_sname.
(17)查询SC表中成绩在60到80之间的所有记录。
(18)查询SC表中成绩为85,86或88的记录。
(19)查询SC表中的最高分的学生学号和课程号。
(20)查询最低分大于70,最高分小于90的sno.

三、解析

# (1)查询所有学生的学号和姓名
select sno as '学号',sname as '学生姓名' from S;
# (2)查询学号为‘1001’的学生的选课情况,结果包括学号、课程号、成绩。
select sno,cno,grade from SC where sno='1001';
# (3)查询选修了‘C2’号课程的学生姓名。
select sname from S where sno in(select sno from SC where cno='C2');
# (4)查询男同学的姓名和年龄,查询结果按年龄降序排列。
select sname from S where sex='男' order by age desc;
# (5)将元组( '4001',王晓英,女,20)插入到学生表中
insert into S values('4001','王晓英','女',20);
# (6)查询所有选修“王华”老师所教课程的学生学号及姓名。
select sno,sname from S where sno in(select sno from SC where cno in (select cno from C where teacher='王华'));
# (7)将对课程表的查询权限授予用户U3。
grant select on C to U3;
# (8)查询年龄大于19岁的学生学号及姓名。
select sno,sname from S where age>19;
# (9)查询姓李的学生,结果包含学号、姓名和年龄。
select sno,sname,age from S where S.sname like '李%';
# (10)查询选修了数据库课程的学生学号和成绩。
select sno,grade from SC where cno in (select cno from C where cname='数据库原理');
# (11)查询出每名学生所选课程的平均成绩,结果包含学号和平均成绩,并按学号升序排列
select sno,avg(grade) from SC group by sno order by sno asc;
# (12)将学号为1001的学生各科成绩均提高5分。
update SC set grade=grade+5 where sno='1001';
# (13)写出创建SC表的SQL语句。
 create table SC(
 sno char(4),
 cno char(4),
 grade int,
 primary key(sno,cno),
 foreign key(sno) references S(sno),
 foreign key(cno) references C(cno)
 );
# (14)查询男同学的选课信息,结果包含姓名,课程号。
select S.sname, SC.cno from S join SC on S.sno=SC.sno where S.sex='男';
# (15)创建视图SCVIEW,视图包含学生学号,学生姓名,课程号,课程名,成绩。
select S.sname, SC.cno from S join SC on S.sno=SC.sno where S.sex='男';
create view SCVIEW as
select SC.sno, S.sname, SC.cno, C.cname, SC.grade
from SC
join S on SC.sno=S.sno
join C on SC.cno=C.cno;
# (16)为S表的sname创建索引,索引名为index_sname.
create index index_sname on S(sname);
# (17)查询SC表中成绩在60到80之间的所有记录。
select * from SC where grade between 60 and 80;
# (18)查询SC表中成绩为85,86或88的记录。
select * from SC where grade in (85,86,88);
# (19)查询SC表中的最高分的学生学号和课程号。
select sno,cno from SC where grade=(select max(grade) from SC);
# (20)查询最低分大于70,最高分小于90的sno.
select sno from SC where grade > 70 and grade < 90 order by grade asc limit 1;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/908745
推荐阅读
相关标签
  

闽ICP备14008679号