MySQL习题练习

一、基础练习
1、student(学生信息表)

create table student(
	sno bigint primary key not null comment '学号',
    sname varchar(20) not null comment '姓名',
    sex varchar(20) not null comment '性别',
    birthday date not null comment '出生日期',
    class bigint not null comment '所在班级'
)engine=innodb default charset utf8;

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

2、teacher(老师信息表)

create table teacher(
	tno bigint primary key not null comment '教师号',
    tname varchar(20) comment '姓名',
    sex varchar(20) not null comment '性别',
    birthday date not null comment '出生日期',
    prof varchar(20) not null comment '职称',
    depart varchar(32) not null comment '所在部门'
)engine=innodb default charset utf8;

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

3、course(课程表)

create table course(
	cno varchar(20) primary key not null comment '课程号',
    cname varchar(32) not null comment '课程名',
    tno bigint not null comment '老师号'
)engine=innodb default charset utf8;

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

4、score(成绩表)

create table score(
	sno bigint not null comment '学号',
    cno varchar(20) not null comment '课程号',
    degree int comment '成绩'
)engine=innodb default charset utf8;

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

5、列出student表中所有记录的sname、sex和class列。

select sname,sex,class from student;

6、显示教师所有的单位即不重复的depart

select distinct depart from teacher;

7、显示学生表的所有记录

select * from student;

8、显示score表中成绩在60到80之间的所有记录

select * from score where degree>60 and degree<80;

9、显示score表中成绩为85,86或88的记录

select * from score where degree=85 or degree=86 or degree=88;

10、显示student表中“95031”班或性别为“女”的同学记录

select * from student where class='95031' or sex='女';

11、以class降序显示student表中的所有记录。

select * from student order by class desc;

12、以cno升序,degree降序显示score表中的所有记录

select * from score order by degree desc,cno asc;

13、显示“98031”班的学生人数

select count(*) from student where class='95031';

14、显示score表中的最高分的学生学号和课程号

select sno,cno from score where degree=(select max(degree) from score);

15、显示“3-105”号课程的平均分

select avg(degree) from score where cno='3-105';

16、显示score表中至少有5名学生选修的并以3开头的课程号的平均数**

select avg(degree) from score where cno like '3%' group by  cno having count(*)>=5;

17、显示最低分大于70,最高分小于90的sno列。

select sno from score group by sno having min(degree)>70 and max(degree)<90;

18、显示所有学生的sname、cno和degree列。

select sname,cno,degree from student join score on student.sno=score.sno;

19、显示所有学生的sname、cname和degree列。

select sname,cname,degree from (score join course on course.cno=score.cno) join student on student.sno=score.sno;

20、列出“95033”班所选课程的平均分

select avg(degree) from score where sno in(select sno from student where class='95033'); 

21、显示选修“3-105”课程的成绩高于“109”号同学的所有同学的记录。

select * from score where degree>(select degree from score where sno='109' and cno='3-105') and cno='3-105';

22、显示score中选修多门课程的同学分数为非最高分成绩的记录。

select sno from score group by sno having count(*)>1); -- 选修了多门课程
select max(degree) from score; -- 有最高分
select cno from score where sno in(select sno from score group by sno having count(*)>1 ) and degree not in(select max(degree) from score where sno in(select sno from score group by sno having count(*)>1));

23、显示成绩高于“109”、课程号为“3-105”的成绩的所有记录。

select * from score where degree>(select degree from score where sno='109' and cno='3-105') and cno='3-105';

24、显示出和学号为“108”的同学同年出生的所有学生的sno、Sname、和birthday列。

select year(birthday) from student where sno='108'; -- 获取108学生的出生年份
select sno,sname,birthday from student where year(birthday)=(select year(birthday) from student where sno='108');

25、显示“李旭”老师任课的学生的成绩。

select tno from teacher where tname='李旭'; -- 获取李旭老师的编号
select cno from course where tno=(select tno from teacher where tname='李旭'); -- 获取李旭老师带的课
select degree from score where cno=(select cno from course where tno=(select tno from teacher where tname='李旭'));

26、显示选修某课程的同学人数多余5人的老师姓名。

select cno from score group by cno having count(*)>=5; -- 获取选修课程大于等于5人的课程号
select tno from course where cno=(select cno from score group by cno having count(*)>=5);  
select tname from teacher where tno=(select tno from course where cno=(select cno from score group by cno having count(*)>=5));

27、显示“95033”班和“95031”班全体学生的记录。

select * from student where class='95033' or class='95031';

28、显示存在有85分以上成绩的课程cno。

select distinct cno from score where degree>85;

29、显示“计算机系”老师所教课程的成绩表。

select tno from teacher where depart='计算机系';
select cno from course where tno in(select tno from teacher where depart='计算机系');
select * from score where cno in(select cno from course where tno in(select tno from teacher where depart='计算机系'));

30、显示“计算机系”和“电子工程系”不同职称的老师的tname和prof。题解

select tname,prof from teacher where prof in(select prof from teacher where depart in('计算机系','电子工程系') group by prof having count(distinct depart)=1);

31、显示选修编号为“3-105”课程且成绩至少高于“3-245”课程的同学的cno、sno和degree,并按degree从
高到低次序排列。

select max(degree) from score where cno='3-245';
select cno,sno,degree from score where cno='3-105' and degree>(select min(degree) from score where cno='3-245') order by degree desc;

32、显示选修编号为“3-105”课程且成绩高于“3-245”课程的同学的cno、sno和degree。

select cno,sno,degree from score where cno='3-105' and degree>(select max(degree) from score where cno='3-245');

33、列出所有任课老师的tname和depart。

select tname,depart from teacher;

34、列出所有未讲课老师的tname和depart。

select tname,depart from teacher where tno not in(select tno from course);

35、列出所有老师和同学的姓名、性别和生日。

select tname,sex,birthday from teacher union select sname,sex,birthday from student; 

36、检索所学课程包含学生“103”所学课程的学生学号。

select sno from score where cno in(select distinct cno from score where sno='103');

37、检索选修所有课程的学生姓名。

select sname from student where sno in( select sno from score group by sno having count(*)>(select count(*) from course));

二、
1、建立三个表 员工表(员工号[主键]、姓名、性别、出生日期); 销售表(流水号、产品编号、销售单价、销售时间、销售数量); 产品表(编号[由字母和数字合成]、产品单价)。

create table employe(
	Eno int primary key auto_increment comment '员工号',
	Ename varchar(20) default null comment '姓名',
	Sex boolean default 1 comment '性别',
	Birth date default null comment '出生日期'
)engine=innodb default charset utf8;
    
create table sales(
	Sid int not null comment '流水号',
	id int not null comment '产品编号',
	price numeric not null comment '销售价格',
	salestime date not null comment '销售时间',
	count int not null comment '销售数量'
)engine=innodb default charset utf8;
    
create table production(
	id varchar(20) comment '产品编号',
	price numeric not null comment '产品单价'
)engine=innodb default charset utf8;

2、 修改员工表,用姓名字段建立一个索引。

create index name_index on employe(Ename);

3、 修改员工表将姓名列改为非空列。

alter table employe modify Ename varchar(20) not null;

4、修改员工表为姓名列创建一个唯一性约束。

alter table employe add constraint Ename_unique unique(Ename);

5、 为员工表添加一条约束,要求是所有员工必须是1983以前出生的,并且该字段不能为空,而且输入 格式必须为年-月-日。

alter table employe add constraint check_Birth check(Birth<1983-01-01);

6、用Insert语句测试一下以上的修改是否成功。

insert into employe values(1001,'张三',1,'1980-04-22');

7、用函数将当天的日期和时间显示出来;显示本月的最后一天。

select now();   select last_day(curdate());

8、向产品表中按要求加入几条记录。

insert into production values('小米1-s',2000.00);
insert into production values('小米2-s',2100.01);
insert into production values('小米3-s',2200.02);
insert into production values('小米4-s',2300.03);
insert into production values('小米5-s',2400.04);
insert into production values('小米6-s',2500.05);

9、将产品表中的产品名称的前三个字母用函数取出来。

select left(id,3) from production;

10、用函数找出产品表中产品名超过15的所有产品信息。

select * from production where length(id)>15;

11、向销售表插入两条当天的销售记录,两条4天以前的销售记录,(全部用日期函数插入)。

insert into sales values(1,12301,1999.9,now(),5);
insert into sales values(2,12302,2999.9,now(),6);
insert into sales values(4,12304,2888.8,'2022-04-25',2);
insert into sales values(5,12305,3888,'2022-04-24',10);

12、将销售表中的同一天的销售额算出来,(保留小数点后2位,四舍五入)用函数完成。

select sum(price * count) from sales;

13、将各种产品的单价以整数形式显示出来(不要求四舍五入)。

select price from production;

14、 下一个周五的日期

select date_sub(curdate(),interval weekday(curdate()) - 11 day);

15、两个月前的今天的日期

select date_add(now(),interval-2 month);
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
     软件工程上机实验要求      利用一种高级语言或数据库程序设计语言,依照所学的软件工程理论设计一个小型软件。要求:   1.大作业在教学17周结束前完成后提交。   2.设计过程原则上要求分组进行,每组一个题目(各组自定)。每组最多不超过3人,组内不同的学生可分工不同,内容不能完全雷同。   3.有详细、完整的文档资料。包括以下内容:      可行性研究报告;     需求规格说明书;     设计说明书(由于编写的是小型软件,故只写概要设计说明书);      用户操作手册;     测试计划;      测试分析报告;      软件开发总结报告。   4.有系统设计代码(其中代码注释不少于代码的30%),系统运行说明。   5.软件工程设计题目示例:        题目一 “教务管理系统之子系统——系内课程安排”   1.系统简介    每学期的期中,学院教务处分别向各个系发出下学期的教学计划,包   括课程名、课时、班级类别(本科、专科、高职)、号等;系教学主管人员根据教学任务和要求给出各课程的相关限制(如:任课教师职称、和数、最高周学时数等);任课教师自报本人授课计划,经所在教研室协调确认,将教学计划上交系主管教学计划的主任,批准后上报学院教务处,最终有教务处给出下学期全系教师的教学任务书。   假设上述排课过程全部为人工操作,现要求改造为能利用计算机实现的自动处理过程。   1. 限定条件   (1)每位教师的主讲门数不超过2门/学期:讲师以下职称的教师不能承担系定主课的主讲任务。   (2)系级干部的主讲课时不能超过4学时/周。   (3)本学期出现严重教学事故的教师不能承担下学期的主讲任务。   (4)本系统的输入项至少应包含3个:教务处布置的教学计划、系教师自报的讲课计划和系定的有关讲课限制条件。   (5)本系统的输出项至少应包含2个:教务处最终下达的全系教师教学任务书和系各教学一学期的课程表(可不包含上课地点)。       题目二、“学校教材订购系统”   1、 系统简介   本系统可细化为两个子系统:销售系统和采购系统   销售系统的工作过程为:首先由教师或学生提交购书单,经教材发行人员审核是有效购书单后,开发票、登记并返给教师或学生领书单,教师或学生即可去书库领书。   采购系统的主要工作过程为:若是脱销教材,则登记缺书,发缺书单给书库采购人员;一旦新书入库后,即发进书通知给教材发行人员   以上的功能要求在计算机上实现。   2、 技术要求和限制条件   (1) 当书库中的各种书籍数量发生变化(包括领书和进书时),都应修改相关的书库记录,如库存表或进/出库表。   (2) 在实现上述销售和采购的工作过程时,需考虑有关单据的合法性验证   (3) 系统的外部项至少包含三个:教师、学生和教材工作人员。   (4) 系统的相关数据存储至少包含6个:购书表、库存表、缺书登记表、待购教材表、进/出库表。       题目三、“机票预订系统”   1、系统简介   航空公司为给旅客乘机提供方便,需开发一机票预定系统。各旅行社把预定机票的旅客信息(姓名、性别、工作单位、身份证号码、旅行时间、旅行目的地等)输入到该系统,系统为旅客安排航。当旅客交付了预定金后,系统印出取票通知和帐单给旅客,旅客在飞机起飞的前一天凭取票通知和帐单交款取票,系统核对无误即印出机票给旅客。此外航空公司为随时掌握各航向飞机的乘载情况,需定期进行查询统计,以便适当调整。   2、 技术要求及限定条件   (1)在分析系统功能时要考虑有关证件的合法性验证(如身份证、取票通知、交款发票等)。   (2)对于本系统还应补充以下功能:    (1) 旅客延误了取票时间的处理    (2) 机取消后的处理    (3) 旅客临时更改机票次的处理   (3) 系统的外部项至少包含三个:旅客、旅行社和航空公司。       题目四:“学校内部工资管理系统”   1、 系统简介   假设学校共有教职工约1000人,10个行政部门和8个系部。每个月20日前各部门(包括系、部)要将出勤情况上报人事处,23日前人事处将出勤工资、奖金及扣款清单送财务处。财务处于每月月底将教职工的工资表做好并将数据送银行。每月初(3日前)将工资条发给各单位。若有员工调入、调出、校内调动、离退休等数据变化,则由人事处通知相关部门和财务处。   2、 技术要求及限定条件   (1) 本系统的数据存储至少包含:工资表、部门汇总表、扣税款表、银行发放表   (2) 除人事处、财务处外,其他只能部门和系部名称可简化,如系1,系2…..等   (3) 工资、奖金、扣款细节可由学生自定       题目五、“实验室设备管理系统”   1

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值