mysql基础操作练习

建数据库
create database selectTest;

查看当前有哪些数据库
show databases;

使用selectTest
use selectTest;

建表:

学生表student
学号 主键
姓名 非空
性别 非空
出生年月日
所在班级
create table student(
sno varchar(20) primary key,
sname varchar(20) not null,
ssex varchar(20) not null,
sbirthday datetime,
class varchar(20)
);

教师表teacher
教师编号
教师姓名
教师性别
教师出生年月日
职称
所在部门
create table teacher(
tno varchar(20) primary key,
tname varchar(20) not null,
tsex varchar(20) not null,
tbirthday datetime,
prof varchar(20) not null,
department varchar(20) not null
);

课程表course
课程号
课程名称
教师编号  外键 关联到教师表
create table course(
cno varchar(20) primary key,
cname varchar(20) not null,
tno varchar(20) not null
foreign key(tno) references teacher(tno)
);

成绩表score
学号
课程号
成绩
create table score(
sno varchar(20) not null,
cno varchar(20) not null,
degree decimal,
foreign key(sno) references student(sno),
foreign key(cno) references course(cno),
primary key(sno,cno)
);

往表中添加数据
学生信息
insert into student values('101','曾华','男','1977-09-01','95033');
insert into student values ('102','小明','男','1975-10-02','95031');
insert into student values ('103','王丽','女','1976-01-23','95033');
insert into student values ('104','李军','男','1976-02-20','95033');
insert into student values ('105','王芳','女','1975-02-10','95031');
insert into student values ('106','陆君','男','1974-06-03','95031');
insert into student values ('107','王尼玛','男','1976-02-20','95033');
insert into student values ('108','张全蛋','男','1975-02-10','95031');
insert into student values ('109','赵铁柱','男','1974-06-03','95031');

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

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

成绩表
insert into score values ('103','3-245','86');
insert into score values ('105','3-245','75');
insert into score values ('109','3-245','68');
insert into score values ('103','3-105','92');
insert into score values ('105','3-105','88');
insert into score values ('109','3-105','76');
insert into score values ('103','6-166','85');
insert into score values ('105','6-166','79');
insert into score values ('109','6-166','81');

查询练习
查询student表中的所有记录
select * from student;

查询student 表中的所有记录的sname ssex class列
select sname,ssex,class from student;

查询教师的所有单位即不重复的department列
select distinct department from teacher;

查询score 中成绩在60到80的记录
select * from score where degree between 60 and 80;
select * from score where degree > 60 and degree < 80;

score中成绩85 86 或88的记录
select * from score where degree in (85,86,88);

student中‘95031’班或性别为女的同学记录
select * from student where class='95031' or ssex = '女';

以class降序查询student的所有记录  desc 降序  asc升序
select * from student order by class desc;

以cno升序,degree降序查询score中的所有记录
select * from score order by cno asc,degree desc;

查询95031班中学生人数  统计
select count(*) from student where class='95031';

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

查询每门课的平均成绩
一门:select avg(degree) from score where cno='3-105';
每门:select avg(degree) from score group by cno;
select cno,avg(degree) from score group by cno;

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

查询分数大于70,小于90的sno列
select sno,degree from score where degree >70 and degree <90;
select sno,degree from score where degree between 70 and 90;

查询所有学生的sname、cno和degree 列
多表查询
sname,sno在student表中,sno,cno和degree在score 表中  sno相同
select sname,cno,degree from student,score where student.sno=score.sno;

查询所有学生的sno,cname,degree列
select sno,cname,degree from course,score where course.cno=score.cno;

查询所有学生的sname,cname,degree列
三表联查
select sname,cname,degree from student,course,score where student.sno =score.sno and course.cno = score.cno;

查询95031班学生每门课的平均分
select cno,avg(degree) from score where sno in (select sno from student where class = '95031') group by cno;

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

查询和学号为108,101的同学同年出生的所有同学的sno、sname和sbirthday列
select sno,sname,sbirthday from student where year(sbirthday) in (select year(sbirthday) from student where sno in ('108','101'));

查询张旭教师任课的学生成绩
select sno,degree from score where cno in (select cno from course where tno = (select tno from teacher where tname = '张旭'));

查询选修某课程的同学人数多于5人的教师姓名
数据不足,再插入3条
insert into score values ('101','3-105','90');
insert into score values ('102','3-105','91');
insert into score values ('104','3-105','89');
select tname from teacher where tno in (select tno from course where cno in (select cno from score group by cno having count(cno)>5));

查询95033班全体同学的记录
select * from student where class in ('95033');

查询存在有85分以上成绩的课程cno
select cno from score where degree >85;

查询计算机系所有教师所教课程的成绩表
select * from score where cno in (select cno from course  where tno in (select tno from teacher where department = '计算机系'));

查询计算机系与电子工程系不同职称的教师的tname和prof
union 求并集
select * from teacher where department = '计算机系' and prof not in (select prof from teacher where department = '电子工程系')
union
select * from teacher where department = '电子工程系' and prof not in (select prof from teacher where department = '计算机系');

查询选修编号为3-105课程且成绩至少高于选修课程编号为3-245的同学的cno,sno和degree,并按degree从高到低次序排序
至少:至少大于其中一个  any
select * from score where cno = '3-105' and degree > any(select degree from score where cno = '3-245') order by degree desc;

查询选修编号为‘3-105’且成绩高于选修编号为3-245课程的同学的cno,sno和degree
select * from score where cno = '3-105' and degree > all(select degree from score where cno = '3-245');

查询所有教师和同学的name,sex,和birthday
select sname name,ssex sex,sbirthday as birthday from student
union
select tname,tsex,tbirthday from teacher;
as 取别名 可省略

查询所有女教师、女同学的name,sex,和birthday
select sname name,ssex sex,sbirthday birthday from student where ssex = '女'
union
select tname name,tsex sex,tbirthday birthday from teacher where tsex = '女';

查询成绩比该课程平均成绩低的同学的成绩表
select * from score a where degree < (select avg(degree) from score b where a.cno=b.cno);

查询所有任课教师的tname和department
select tname,department from teacher where tno in (select tno from course);

查询至少有2名男生的版号
select  class from student where ssex = '男' group by class having count(*)>1;

查询student表中不姓王的同学记录
select * from student where sname not like '王%';

查询student表中每个学生的姓名和年龄
select sname,2022-year(sbirthday) from student;
select sname,year(now())-year(sbirthday) '年龄' from student;

查询student表中最大和最小的sbirthday日期值
select max(sbirthday) as maxbirth,min(sbirthday) as minbirth from student;

以班号和年龄从大到小的顺序查询student表中的全部记录
select * from student order by class desc,sbirthday;

查询男教师及其所上的课程
select * from course where tno in (select tno from teacher where tsex = '男');

查询最高分同学的sno,cno,degree列
select * from score where degree = (select max(degree) from score);

查询和李军同性别的所有同学的sname
select sname from student where ssex = (select ssex from student where sname = '李军');

查询所有选修计算机导论课程的男同学的成绩表
select * from score where sno in (select sno from student where ssex = '男') and cno = (select cno from course where cname = '计算机导论');

假设使用如下命令建立了一个grade表
create table grade(
low int(3),
upp int(3),
grade char(1)
);
insert into grade values(90,100,'A');
insert into grade values(80,89,'B');
insert into grade values(70,79,'C');
insert into grade values(60,69,'D');
insert into grade values(0,59,'E');
现查询所有同学的sno,cno,grade列
select sno,cno,grade from score,grade where degree between low and upp;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值