MySQL连表查询练习题(一)

风格不统一 提交于 2021-01-13 07:18:08

MySQL连表查询练习题(一)

建库

库名:linux50 字符集:utf8 校验规则:utf8_general_ci

mysql> create database linux50 charset utf8 collate utf8_genneral_ci;

建表

表一

student(学生表)

字段 数据类型要求 是否为空 注释
sno 最多20位 学号(主键)
sname 可变长 学生姓名
sage 最小整数,非负数 学生年龄
ssex 0,1 学生性别(1是男,0是女)默认为男)
sbirthday 时间类型 默认为空 学生生日
class 可变长 学生班级
create table student( 
	sno int not null primary key auto_increment comment '学号',  
	sname varchar(20) not null comment '学生姓名',  
	sage tinyint unsigned not null comment '学生年龄',  
	ssex enum('1','0') default '1' not null comment '学生性别',  
	sbirthday datetime default null comment '入学时间', 
	class varchar(20) not null comment '班级');

insert into student(sname,sage,ssex,sbirthday,class) values('孟诗雨',18,'0',now(),1),('蒋佳娥',20,'0',now(),1),('吴奇宇',38,'1',now(),2),('胡俊涛',25,'1',now(),2);


mysql> select * from student;                                       
+-----+-----------+------+------+---------------------+-------+
| sno | sname     | sage | ssex | sbirthday           | class |
+-----+-----------+------+------+---------------------+-------+
|   1 | 孟诗雨     |   18 | 0    | 2019-12-02 19:11:38 | 1     |
|   2 | 蒋佳娥     |   20 | 0    | 2019-12-02 19:11:38 | 1     |
|   3 | 吴奇宇     |   38 | 1    | 2019-12-02 19:11:38 | 2     |
|   4 | 胡俊涛     |   25 | 1    | 2019-12-02 19:11:38 | 2     |
+-----+-----------+------+------+---------------------+-------+

表二

course(课程表)

字段 数据类型要求 是否为空 注释
cno 最多20位 课程号(主键)
cname 可变长 课程名称
tno 可变长 教师编号
create table course(
	cno int(20) not null primary key auto_increment comment '课程号',
    cname varchar(20) not null comment '课程名称',
    tno varchar(20) not null comment '教师编号');

insert into course(cname,tno) values('语文',001),('数学',002),('英语',003),('化学',004);

mysql> select * from course;                                                  
+-----+--------+-----+
| cno | cname  | tno |
+-----+--------+-----+
|   1 | 语文   | 001 |
|   2 | 数学   | 002 |
|   3 | 英语   | 003 |
|   4 | 化学   | 004 |
+-----+--------+-----+

这个tno比较有意思 需要001这样的方式 用int怎么表达出来?

添加zerofill参数 前面给的int(3) 意思是占三位 不够自动补全

如果不加int(3)会怎样?

默认全补全 10位

表三

score (成绩表)

字段 数据类型要求 是否为空 注释
sno 最多20位 学号(主键)
cno 最多20位 课程号
mark 浮点数(4,1) 成绩

**注意:**sno和cno在另外两个表中是主键,在这里应该是外键,不过咱们不需要创建,了解即可

mysql> create table score(
    ->     sno int(20) not null comment '学号',
    ->     cno int(20) not null comment '课程号',
    ->     mark float(4,1) not null comment '成绩');

mysql> insert into score(sno,cno,mark) values
    -> (1,1,90),(2,1,85),(3,1,77),(4,1,95),
    -> (1,2,85),(2,2,93),(3,2,78),(4,2,96),
    -> (1,3,80),(2,3,60),(3,3,88),(4,3,75),
    -> (1,4,82),(2,4,65),(3,4,89),(4,4,99)

mysql> select * from score;
+-----+-----+------+
| sno | cno | mark |
+-----+-----+------+
|   1 |   1 | 90.0 |
|   2 |   1 | 85.0 |
|   3 |   1 | 77.0 |
|   4 |   1 | 95.0 |
|   1 |   2 | 85.0 |
|   2 |   2 | 93.0 |
|   3 |   2 | 78.0 |
|   4 |   2 | 96.0 |
|   1 |   3 | 80.0 |
|   2 |   3 | 60.0 |
|   3 |   3 | 88.0 |
|   4 |   3 | 75.0 |
|   1 |   4 | 82.0 |
|   2 |   4 | 65.0 |
|   3 |   4 | 89.0 |
|   4 |   4 | 99.0 |
+-----+-----+------+

表四

teacher (教师表)

字段 数据类型要求 是否为空 注释
tno 最多20位 教师编号(主键)
tname 可变长 教师姓名
tage 最小整数,非负数 教师年龄
tsex 0,1 教师性别(1是男,0是女)默认为男)
prof 可变长 教师职称
depart 可变长 教师部门
mysql> create table teacher(
    ->     tno varchar(20) not null primary key comment '教师编号',
    ->     tname varchar(20) not null comment '教师姓名',
    ->     tage tinyint unsigned not null comment '教师年龄',
    ->     tsex enum('1','0') not null default '1' comment '教师性别 1男0女',
    ->     prof varchar(20) null comment '教师职称',
    ->     depart varchar(20) not null comment '教师部门');

mysql> insert into teacher(tno,tname,tage,tsex,prof,depart) values('001','曾导',38,'1','教学总监','语言系'),('002','邱导',49,'1','讲师','科学系'),('003','钱导',26,'1','助教','文学系'),('004','高某',33,'0','班主任','管理系');

mysql> select * from teacher;
+-----+--------+------+------+--------------+-----------+
| tno | tname  | tage | tsex | prof         | depart    |
+-----+--------+------+------+--------------+-----------+
| 001 | 曾导   |   38 | 1     | 教学总监      | 语言系     |
| 002 | 邱导   |   49 | 1     | 讲师          | 科学系    |
| 003 | 钱导   |   26 | 1     | 助教          | 文学系    |
| 004 | 高某   |   33 | 0     | 班主任        | 管理系     |
+-----+--------+------+------+--------------+-----------+

练习题

插入数据练习:

1.将自己班级小组所有人员信息插入到student表中(数据自定义)

insert into student(sname,sage,ssex,sbirthday,class) values('孟诗雨',18,'0',now(),1),('蒋佳娥',20,'0',now(),1),('吴奇宇',38,'1',now(),2),('胡俊涛',25,'1',now(),2);


mysql> select * from student;                                       
+-----+-----------+------+------+---------------------+-------+
| sno | sname     | sage | ssex | sbirthday           | class |
+-----+-----------+------+------+---------------------+-------+
|   1 | 孟诗雨     |   18 | 0    | 2019-12-02 19:11:38 | 1     |
|   2 | 蒋佳娥     |   20 | 0    | 2019-12-02 19:11:38 | 1     |
|   3 | 吴奇宇     |   38 | 1    | 2019-12-02 19:11:38 | 2     |
|   4 | 胡俊涛     |   25 | 1    | 2019-12-02 19:11:38 | 2     |
+-----+-----------+------+------+---------------------+-------+

2.将曾导、徐导、李导信息插入教师表中(数据自定义)

mysql> insert into teacher(tno,tname,tage,tsex,prof,depart) values('001','曾导',38,'1','教学总监','语言系'),('002','邱导',49,'1','讲师','科学系'),('003','钱导',26,'1','助教','文学系'),('004','高某',33,'0','班主任','管理系');

mysql> select * from teacher;
+-----+--------+------+------+--------------+-----------+
| tno | tname  | tage | tsex | prof         | depart    |
+-----+--------+------+------+--------------+-----------+
| 001 | 曾导   |   38 | 1     | 教学总监      | 语言系     |
| 002 | 邱导   |   49 | 1     | 讲师          | 科学系    |
| 003 | 钱导   |   26 | 1     | 助教          | 文学系    |
| 004 | 高某   |   33 | 0     | 班主任        | 管理系     |
+-----+--------+------+------+--------------+-----------+

3.将数学、语文、英语学科插入到课程表中(数据自定义)

insert into course(cname,tno) values('语文',001),('数学',002),('英语',003),('化学',004);

mysql> select * from course;                                                  
+-----+--------+-----+
| cno | cname  | tno |
+-----+--------+-----+
|   1 | 语文   | 001 |
|   2 | 数学   | 002 |
|   3 | 英语   | 003 |
|   4 | 化学   | 004 |
+-----+--------+-----+

4.将分数插入到成绩表中(数据自定义)

mysql> insert into score(sno,cno,mark) values
    -> (1,1,90),(2,1,85),(3,1,77),(4,1,95),
    -> (1,2,85),(2,2,93),(3,2,78),(4,2,96),
    -> (1,3,80),(2,3,60),(3,3,88),(4,3,75),
    -> (1,4,82),(2,4,65),(3,4,89),(4,4,99)

mysql> select * from score;
+-----+-----+------+
| sno | cno | mark |
+-----+-----+------+
|   1 |   1 | 90.0 |
|   2 |   1 | 85.0 |
|   3 |   1 | 77.0 |
|   4 |   1 | 95.0 |
|   1 |   2 | 85.0 |
|   2 |   2 | 93.0 |
|   3 |   2 | 78.0 |
|   4 |   2 | 96.0 |
|   1 |   3 | 80.0 |
|   2 |   3 | 60.0 |
|   3 |   3 | 88.0 |
|   4 |   3 | 75.0 |
|   1 |   4 | 82.0 |
|   2 |   4 | 65.0 |
|   3 |   4 | 89.0 |
|   4 |   4 | 99.0 |
+-----+-----+------+

查询练习:

1.查询student表中的所有记录的sname、ssex和class列。

mysql> select  sname,ssex,class from student ;
+-----------+------+-------+
| sname     | ssex | class |
+-----------+------+-------+
| 孟诗雨    | 0    | 1     |
| 蒋佳娥    | 0    | 1     |
| 吴奇宇    | 1    | 2     |
| 胡俊涛    | 1    | 2     |
+-----------+------+-------+

2.查询教师所有的单位即不重复的depart列。

mysql> select distinct(depart) from teacher;                                           
+-----------+
| depart    |
+-----------+
| 语言系    |
| 科学系    |
| 文学系    |
| 管理系    |
+-----------+

3.查询student表的所有记录。

mysql> select * from student;                                       
+-----+-----------+------+------+---------------------+-------+
| sno | sname     | sage | ssex | sbirthday           | class |
+-----+-----------+------+------+---------------------+-------+
|   1 | 孟诗雨     |   18 | 0    | 2019-12-02 19:11:38 | 1     |
|   2 | 蒋佳娥     |   20 | 0    | 2019-12-02 19:11:38 | 1     |
|   3 | 吴奇宇     |   38 | 1    | 2019-12-02 19:11:38 | 2     |
|   4 | 胡俊涛     |   25 | 1    | 2019-12-02 19:11:38 | 2     |
+-----+-----------+------+------+---------------------+-------+

4.查询score表中成绩在60到80之间的所有记录。

mysql> select * from score where mark >60 and mark <80;
+-----+-----+------+
| sno | cno | mark |
+-----+-----+------+
|   3 |   1 | 77.0 |
|   3 |   2 | 78.0 |
|   4 |   3 | 75.0 |
|   2 |   4 | 65.0 |
+-----+-----+------+

5.查询score表中成绩为85,86或88的记录。

mysql> select * from score where mark in (85,86,88);
+-----+-----+------+
| sno | cno | mark |
+-----+-----+------+
|   2 |   1 | 85.0 |
|   1 |   2 | 85.0 |
|   3 |   3 | 88.0 |
+-----+-----+------+

6.查询student表中1班或性别为“女”的同学记录。

mysql> select * from student where ssex='0' or class='1';
+-----+-----------+------+------+---------------------+-------+
| sno | sname     | sage | ssex | sbirthday           | class |
+-----+-----------+------+------+---------------------+-------+
|   1 | 孟诗雨    |   18 | 0    | 2019-12-02 19:11:38 | 1     |
|   2 | 蒋佳娥    |   20 | 0    | 2019-12-02 19:11:38 | 1     |
+-----+-----------+------+------+---------------------+-------+

7.以class降序查询Student表的所有记录。

mysql> select * from student order by class desc;
+-----+-----------+------+------+---------------------+-------+
| sno | sname     | sage | ssex | sbirthday           | class |
+-----+-----------+------+------+---------------------+-------+
|   3 | 吴奇宇    |   38 | 1    | 2019-12-02 19:11:38 | 2     |
|   4 | 胡俊涛    |   25 | 1    | 2019-12-02 19:11:38 | 2     |
|   1 | 孟诗雨    |   18 | 0    | 2019-12-02 19:11:38 | 1     |
|   2 | 蒋佳娥    |   20 | 0    | 2019-12-02 19:11:38 | 1     |
+-----+-----------+------+------+---------------------+-------+

8.以cno升序、mark降序查询Score表的所有记录

mysql> select * from score order by cno,mark desc;
+-----+-----+------+
| sno | cno | mark |
+-----+-----+------+
|   4 |   1 | 95.0 |
|   1 |   1 | 90.0 |
|   2 |   1 | 85.0 |
|   3 |   1 | 77.0 |
|   4 |   2 | 96.0 |
|   2 |   2 | 93.0 |
|   1 |   2 | 85.0 |
|   3 |   2 | 78.0 |
|   3 |   3 | 88.0 |
|   1 |   3 | 80.0 |
|   4 |   3 | 75.0 |
|   2 |   3 | 60.0 |
|   4 |   4 | 99.0 |
|   3 |   4 | 89.0 |
|   1 |   4 | 82.0 |
|   2 |   4 | 65.0 |
+-----+-----+------+

9.查询2班的学生人数。

mysql> select class,count(sname) from student where class='2' group by class;
+-------+--------------+
| class | count(sname) |
+-------+--------------+
| 2     |            2 |
+-------+--------------+

10.查询”曾志高翔“教师任课的学生成绩。

mysql> select student.sname,score.mark,teacher.tname
    -> from student,course,score,teacher
    -> where tname='曾导'
    -> and student.sno=score.sno
    -> and teacher.tno=course.tno
    -> and score.cno=course.cno;
+-----------+------+--------+
| sname     | mark | tname  |
+-----------+------+--------+
| 孟诗雨    | 90.0 | 曾导   |
| 蒋佳娥    | 85.0 | 曾导   |
| 吴奇宇    | 77.0 | 曾导   |
| 胡俊涛    | 95.0 | 曾导   |
+-----------+------+--------+

11.查询语文课程所有男生的成绩并且查出对应课程的教师名,职称,及所在部门。

mysql> select student.sname,student.ssex,course.cname,score.mark,teacher.tname,teacher.prof,teacher.depart
    -> from student,course,score,teacher
    -> where cname='语文' and ssex='1'
    -> and student.sno=score.sno
    -> and teacher.tno=course.tno
    -> and score.cno=course.cno;
+-----------+------+--------+------+--------+--------------+-----------+
| sname     | ssex | cname  | mark | tname  | prof         | depart    |
+-----------+------+--------+------+--------+--------------+-----------+
| 吴奇宇    | 1    | 语文   | 77.0 | 曾导   | 教学总监     | 语言系    |
| 胡俊涛    | 1    | 语文   | 95.0 | 曾导   | 教学总监     | 语言系    |
+-----------+------+--------+------+--------+--------------+-----------+

12.把11题查出的成绩按照降序排序。

mysql> select student.sname,student.ssex,course.cname,score.mark,teacher.tname,teacher.prof,teacher.depart
    -> from student,course,score,teacher
    -> where cname='语文' and ssex='1'
    -> and student.sno=score.sno
    -> and teacher.tno=course.tno
    -> and score.cno=course.cno
    -> order by mark desc;
+-----------+------+--------+------+--------+--------------+-----------+
| sname     | ssex | cname  | mark | tname  | prof         | depart    |
+-----------+------+--------+------+--------+--------------+-----------+
| 胡俊涛    | 1    | 语文   | 95.0 | 曾导   | 教学总监     | 语言系    |
| 吴奇宇    | 1    | 语文   | 77.0 | 曾导   | 教学总监     | 语言系    |
+-----------+------+--------+------+--------+--------------+-----------+
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!