数据库练习

本文提供了一系列SQL数据库操作案例,包括创建表、插入数据、多表查询等,通过具体实例展示了如何进行学生信息、课程成绩及教师信息的管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

create table student(
sno varchar(20) not null primary key,
sname varchar(20) not null,
ssex varchar(20) not null,
sbirthday datetime,
class varchar(20)
);

create table teacher(
tno varchar(20) not null primary key,
tname varchar(20) not null,
tsex varchar(20) not null,
tbirthday datetime,
prof varchar(20),
depart varchar(20) not null
);

create table course(
cno varchar(20) not null,
cname varchar(20) not null,
tno varchar(20) not null
);

create table score(
sno varchar(20) not null,
cno varchar(20) not null,
degree decimal
);

 

insert into student(sno,sname,ssex,sbirthday,class) values('108','曾华','男','1977-09-01','95033'),('105','匡明','男','1975-10-02','95031'),('107','王丽','女','1976-01-23','95033'),('101','李军','男','1976-02-20','95033'),('109','王芳','女','1975-02-10','95031'),('103','陆君','男','1974-06-03','95031');

insert into course(cno,cname,tno) values('3-105','计算机导论','825'),('3-245','操作系统','804'),('6-166','数字电路','856'),('9-888','高等数学','831');

insert into score(sno,cno,degree) values('103','3-245','86'),('105','3-245','75'),('109','3-245','68'),('103','3-105','92'),('105','3-105','88'),('109','3-105','76'),('103','3-105','64'),('105','3-105','91'),('109','3-105','78'),('103','6-166','85'),('105','6-166','79'),('109','6-166','81');

insert into teacher(tno,tname,tsex,tbirthday,prof,depart) values('804','李诚','男','1958-12-02','副教授','计算机系'),('856','张旭','男','1969-03-12','讲师','电子工程系'),('825','王萍','女','1972-05-05','助教','计算机系'),('831','刘冰','女','1977-08-14','助教','电子工程系');

1、 查询Student表中的所有记录的Sname、Ssex和Class列。
select sname,ssex,class from student;

2、 查询教师所有的单位即不重复的Depart列。
select distinct depart from teacher;

3、 查询Student表的所有记录
select * from student;

4、 查询Score表中成绩在60到80之间的所有记录。
select * from score where degree between 60 and 80;

5、 查询Score表中成绩为85,86或88的记录。
select * from score where degree in(85,86,88);

6、 查询Student表中“95031”班或性别为“女”的同学记录
select * from student where class='95031' or ssex='女';

7、 以Class降序查询Student表的所有记录。
select * from student order by class desc;

8、 以Cno升序、Degree降序查询Score表的所有记录。
select * from score order by cno asc,degree desc;

9、 查询“95031”班的学生人数。
select count(class) from student where class='95031'; 

10、查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
select sno,cno from score where degree=(select max(degree) from score);

11、查询每门课的平均成绩。
select cno,avg(degree) from score group by cno;

12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
select avg(degree) from score where cno like '3%' and cno in(select cno from score group by cno having count(*)>=5);

13、查询分数大于70,小于90的Sno列。
select sno from score where degree>'70'and degree<'90';

14,查询所有学生的Sname、Cno和Degree列
select sname,cno,degree from score inner join student on student.sno=score.sno;

15 查询所有学生的Sno、Cname和Degree列。
select sno,cname,degree from score inner join course on course.cno=score.cno;

16、查询所有学生的Sname、Cname和Degree列。
select sname,cname,degree from score inner join student on student.sno=score.sno
inner join course on course.cno=score.cno;

19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
select * from score where cno='3-105' and degree>(select max(degree) from score where sno='109');
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值