MySQL连表查询练习题
1.建库
库名:linux50 字符集:utf8 校验规则:utf8_general_ci

create database linux4 charset utf8 default collate = utf8_general_ci;
2.建表
2.1表一
表名:student(学生表)
| 字段 | 数据类型要求 | 是否为空 | 注释 |
|---|---|---|---|
| sno | 最多20位 | 否 | 学号(主键) |
| sname | 可变长 | 否 | 学生姓名 |
| sage | 最小整数,非负数 | 否 | 学生年龄 |
| ssex | 0,1 | 否 | 学生性别(1是男,0是女)默认为男) |
| sbirthday | 时间类型 | 默认为空 | 学生生日 |
| class | 可变长 | 否 | 学生班级 |

student表:
create table if not exists student(
sno varchar(21) not null primary key comment '学号',
sname varchar(12) not null comment '学生姓名',
sage tinyint unsigned not null comment '学生年龄',
ssex enum('1','0') default '1' comment '学生性别',
sbirthday datetime comment '学生生日',
class varchar(10) not null comment '学生班级') default charset=utf8;
2.2表二
表名:course(课程表)
| 字段 | 数据类型要求 | 是否为空 | 注释 |
|---|---|---|---|
| cno | 最多20位 | 否 | 课程号(主键) |
| cname | 可变长 | 否 | 课程名称 |
| tno | 可变长 | 否 | 教师编号 |

course表:
create table if not exists course(
cno varchar(21) not null primary key comment '课程号',
cname varchar(10) not null comment '课程名称',
tno varchar(10) not null comment '教师编号') default charset=utf8;
2.3表三
表名:score(成绩表)
| 字段 | 数据类型要求 | 是否为空 | 注释 |
|---|---|---|---|
| sno | 最多20位 | 否 | 学号(主键) |
| cno | 最多20位 | 否 | 课程号(主键) |
| mark | 浮点数(4,1) | 否 | 成绩 |
注意:sno和cno在另外两个表中是主键,在这里应该是外键,不过咱们不需要创建,了解即可

score表:
create table if not exists score(
sno varchar(21) not null primary key comment '学号',
cno varchar(21) not null comment '课程号',
mark float(4,1) not null comment '成绩') default charset=utf8;

2.4表四
表名:teacher(教师表)
| 字段 | 数据类型要求 | 是否为空 | 注释 |
|---|---|---|---|
| tno | 最多20位 | 否 | 教师编号(主键) |
| tname | 可变长 | 否 | 教师姓名 |
| tage | 最小整数,非负数 | 否 | 教师年龄 |
| tsex | 0,1 | 否 | 教师性别(1是男,0是女)默认为男) |
| prof | 可变长 | 是 | 教师职称 |
| depart | 可变长 | 否 | 教师部门 |


teacher表:
create table if not exists teacher(
tno varchar(21) not null primary key comment '教师编号',
tname varchar(10) not null comment '教师姓名',
tage tinyint unsigned not null comment '教师年龄',
tsex enum('0','1') not null default '1' comment '教师性别',
prof varchar(10) comment '教师职称',
depart varchar(15) not null comment '教师部门') default charset=utf8;
练习题
#插入数据练习:
1.将自己班级小组所有人员信息插入到student表中(数据自定义)
insert into student(sno,sname,sage,ssex,sbirthday,class)
values(1,'李洋',18,1,19980422,'1'),
(2,'温俊林',19,0,19970422,'1'),
(3,'周恒华',20,1,19950422,'1'),
(4,'张松涛',22,1,19940422,'2'),
(5,'李晖',20,0,19960622,'2'),
(6,'包政',23,1,19930422,'2');
2.将曾导、徐导、李导信息插入教师表中(数据自定义)
insert into teacher(tno,tname,tage,tsex,prof,depart)
values('001','曾导',18,1,'校长','linux'),
('002','徐导',19,1,'教学总监','linux'),
('003','李导',20,1,'讲师','python');
3.将linux、python学科插入到课程表中(数据自定义)
insert into course(cno,cname,tno)
values('1','linux','001'),
('2','linux','002'),
('3','python','003');
4.将分数插入到成绩表中(数据自定义)
insert into score(sno,cno,mark)
values('1','1','99.5'),
('2','1','80.5'),
('3','1','85.5'),
('4','1','84.5'),
('5','1','89.5'),
('6','1','89.5');
#查询练习:
1.查询student表中的所有记录的sname、ssex和class列。
mysql> select sname,ssex,class from student;
+-----------+------+--------+
| sname | ssex | class |
+-----------+------+--------+
| 李洋 | 1 | 1 |
| 温俊林 | 1 | 1 |
| 周恒华 | 1 | 1 |
| 张松涛 | 1 | 2 |
| 李晖 | 1 | 2 |
| 包政 | 1 | 2 |
+-----------+------+--------+
2.查询教师所有的单位即不重复的depart列。
mysql> select depart from teacher;
+--------+
| depart |
+--------+
| linux |
| linux |
| python |
+--------+
3.查询student表的所有记录。
mysql> select * from student;
+-----+-----------+------+------+---------------------+--------+
| sno | sname | sage | ssex | sbirthday | class |
+-----+-----------+------+------+---------------------+--------+
| 1 | 李洋 | 18 | 1 | 1998-04-22 00:00:00 | 1 |
| 2 | 温俊林 | 19 | 0 | 1997-04-22 00:00:00 | 1 |
| 3 | 周恒华 | 20 | 1 | 1995-04-22 00:00:00 | 1 |
| 4 | 张松涛 | 22 | 1 | 1994-04-22 00:00:00 | 2 |
| 5 | 李晖 | 20 | 0 | 1996-06-22 00:00:00 | 2 |
| 6 | 包政 | 23 | 1 | 1993-04-22 00:00:00 | 2 |
+-----+-----------+------+------+---------------------+--------+
4.查询score表中成绩在80到90之间的所有记录。
mysql> select * from score where mark>80 and mark<90;
+-----+------+------+
| sno | cno | mark |
+-----+------+------+
| 2 | 1 | 80.5 |
| 3 | 1 | 85.5 |
| 4 | 1 | 84.5 |
| 5 | 1 | 89.5 |
| 6 | 1 | 89.5 |
+-----+------+------+
5.查询score表中成绩为85.5,89.5或80.5的记录。
mysql> select * from score where mark=85.5 or mark=89.5 or mark=80.5;
+-----+------+------+
| sno | cno | mark |
+-----+------+------+
| 2 | 1 | 80.5 |
| 3 | 1 | 85.5 |
| 5 | 1 | 89.5 |
| 6 | 1 | 89.5 |
+-----+------+------+
6.查询student表中1班或性别为“女”的同学记录。
mysql> select * from student where class=1 or ssex='0';
+-----+-----------+------+------+---------------------+-------+
| sno | sname | sage | ssex | sbirthday | class |
+-----+-----------+------+------+---------------------+-------+
| 1 | 李洋 | 18 | 1 | 1998-04-22 00:00:00 | 1 |
| 2 | 温俊林 | 19 | 0 | 1997-04-22 00:00:00 | 1 |
| 3 | 周恒华 | 20 | 1 | 1995-04-22 00:00:00 | 1 |
| 5 | 李晖 | 20 | 0 | 1996-06-22 00:00:00 | 2 |
+-----+-----------+------+------+---------------------+-------+
7.以class降序查询Student表的所有记录。
mysql> select * from student order by class desc;
+-----+-----------+------+------+---------------------+-------+
| sno | sname | sage | ssex | sbirthday | class |
+-----+-----------+------+------+---------------------+-------+
| 4 | 张松涛 | 22 | 1 | 1994-04-22 00:00:00 | 2 |
| 5 | 李晖 | 20 | 0 | 1996-06-22 00:00:00 | 2 |
| 6 | 包政 | 23 | 1 | 1993-04-22 00:00:00 | 2 |
| 1 | 李洋 | 18 | 1 | 1998-04-22 00:00:00 | 1 |
| 2 | 温俊林 | 19 | 0 | 1997-04-22 00:00:00 | 1 |
| 3 | 周恒华 | 20 | 1 | 1995-04-22 00:00:00 | 1 |
+-----+-----------+------+------+---------------------+-------+
8.以cno升序、mark降序查询Score表的所有记录
mysql> select * from score order by cno asc,mark desc;
+-----+------+------+
| sno | cno | mark |
+-----+------+------+
| 1 | 1 | 99.5 |
| 5 | 1 | 89.5 |
| 6 | 1 | 89.5 |
| 3 | 1 | 85.5 |
| 4 | 1 | 84.5 |
| 2 | 1 | 80.5 |
+-----+------+------+
9.查询2班的学生人数。
mysql> select * from student where class=2;
+-----+-----------+------+------+---------------------+-------+
| sno | sname | sage | ssex | sbirthday | class |
+-----+-----------+------+------+---------------------+-------+
| 4 | 张松涛 | 22 | 1 | 1994-04-22 00:00:00 | 2 |
| 5 | 李晖 | 20 | 0 | 1996-06-22 00:00:00 | 2 |
| 6 | 包政 | 23 | 1 | 1993-04-22 00:00:00 | 2 |
+-----+-----------+------+------+---------------------+-------+
10.查询”曾志高翔“教师任课的学生成绩。
mysql> select teacher.tname,course.cname,student.sname,score.mark from teacher,course,student,score where student.sno=score.sno and course.cno=score.cno and course.tno=teacher.tno;
+--------+-------+-----------+------+
| tname | cname | sname | mark |
+--------+-------+-----------+------+
| 曾导 | linux | 李洋 | 99.5 |
| 曾导 | linux | 温俊林 | 80.5 |
| 曾导 | linux | 周恒华 | 85.5 |
| 曾导 | linux | 张松涛 | 84.5 |
| 曾导 | linux | 李晖 | 89.5 |
| 曾导 | linux | 包政 | 89.5 |
+--------+-------+-----------+------+
11.查询linux课程所有男生的成绩并且查出对应课程的教师名,职称,及所在部门。
mysql> select teacher.tname,teacher.prof,teacher.depart,course.cname,student.ssex,score.mark from teacher,course,student,score where student.sno=score.sno and course.cno=score.cno and course.tno=teacher.tno and ssex='1';
+--------+--------+--------+-------+------+------+
| tname | prof | depart | cname | ssex | mark |
+--------+--------+--------+-------+------+------+
| 曾导 | 校长 | linux | linux | 1 | 99.5 |
| 曾导 | 校长 | linux | linux | 1 | 85.5 |
| 曾导 | 校长 | linux | linux | 1 | 84.5 |
| 曾导 | 校长 | linux | linux | 1 | 89.5 |
+--------+--------+--------+-------+------+------+
12.把11题查出的成绩按照降序排序。
mysql> select teacher.tname,teacher.prof,teacher.depart,course.cname,student.ssex,score.mark from teacher,course,student,score where student.sno=score.sno and course.cno=score.cno and course.tno=teacher.tno and ssex='1' order by mark desc;
+--------+--------+--------+-------+------+------+
| tname | prof | depart | cname | ssex | mark |
+--------+--------+--------+-------+------+------+
| 曾导 | 校长 | linux | linux | 1 | 99.5 |
| 曾导 | 校长 | linux | linux | 1 | 89.5 |
| 曾导 | 校长 | linux | linux | 1 | 85.5 |
| 曾导 | 校长 | linux | linux | 1 | 84.5 |
+--------+--------+--------+-------+------+------+
MySQL连表查询练习题的更多相关文章
- MySQL多表查询练习题
一.准备数据 #创建表及插入记录 CREATE TABLE class ( cid ) NOT NULL AUTO_INCREMENT, caption ) NOT NULL, PRIMARY KEY ...
- MySQL多表查询之外键、表连接、子查询、索引
MySQL多表查询之外键.表连接.子查询.索引 一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复的,不允许为 ...
- Mysql 单表查询 子查询 关联查询
数据准备: ## 学院表create table department( d_id int primary key auto_increment, d_name varchar(20) not nul ...
- (转)Mysql 多表查询详解
MySQL 多表查询详解 一.前言 二.示例 三.注意事项 一.前言 上篇讲到mysql中关键字执行的顺序,只涉及了一张表:实际应用大部分情况下,查询语句都会涉及到多张表格 : 1.1 多表连接有 ...
- MySQL多表查询回顾
----------------------siwuxie095 MySQL 多表查询回顾 以客户和联系人为例(一对多) 1.内连接 /*内连接写法一*/ select * from t_custom ...
- python 3 mysql 单表查询
python 3 mysql 单表查询 1.准备表 company.employee 员工id id int 姓名 emp_name varchar 性别 sex enum 年龄 age int 入职 ...
- python3 mysql 多表查询
python3 mysql 多表查询 一.准备表 创建二张表: company.employee company.department #建表 create table department( id ...
- Mysql 单表查询-排序-分页-group by初识
Mysql 单表查询-排序-分页-group by初识 对于select 来说, 分组聚合(((group by; aggregation), 排序 (order by** ), 分页查询 (limi ...
- Mysql 单表查询where初识
Mysql 单表查询where初识 准备数据 -- 创建测试库 -- drop database if exists student_db; create database student_db ch ...
随机推荐
- Map构造器模式 map builder pattern
maven引入依赖 <dependency> <groupId>com.google.guava</groupId> <artifactId>guava ...
- HDFS文件目录操作代码
分布式文件系统HDFS中对文件/目录的相关操作代码,整理了一下,大概包括以下部分: 文件夹的新建.删除.重命名 文件夹中子文件和目录的统计 文件的新建及显示文件内容 文件在local和remote间的 ...
- Java日志体系(二)log4j
1.1 简介 Log4j是一个由Java编写可靠.灵活的日志框架,是Apache旗下的一个开源项目: 使用Log4j,我们更加方便的记录了日志信息,它不但能控制日志输出的目的地,也能控制日志输出的内容 ...
- Django:(04)状态保持和验证
一.Cookie 特点 Cookie是由服务器(网站)生成的,存储在浏览器端的 键值对数据(通常经过加密) 在响应请求时,服务器会把生成 Cookie数据 发给浏览器,浏览器会自动保存 (前提:浏 ...
- VMware 虚拟机安装Mac OS X 10.10
有图有真相,哈哈 一.下载以上文件 1. vm百度软件下载即可,版本都能满足需要,随意好了 2. unlocker 207 3. Mac OS X 10.10镜像 二.基本步骤 1. 虚拟机的安装 下 ...
- sql次级语句
select upper(n_id) from nrc_news;select left(n_content,1) from nrc_news;select len(n_content) from n ...
- jstack 命令
NAME jstack - Prints Java thread stack traces for a Java process, core file, or remote debug server. ...
- MySQL慢查询—开启慢查询
###一.简介 开启慢查询日志,可以让MySQL记录下查询超过指定时间的语句,通过定位分析性能的瓶颈,才能更好的优化数据库系统的性能. ###二.参数说明 slow_query_log 慢查询开启状态 ...
- python 2 和 python 3的区别
python2和python3区别 python2:源码不统一,源码(功能)重复,维护困难,除法的时候返回来的是小数点,()浮点数 python3:源码统一,源码不重复,除法的时候返回来的是整 ...
- 非常简约学生管理系统——HashSet进行编写
很小的一个练习,可以参考一下啊~~~~~~ 1:注意:学生类中进行多个重要方法的重写 package com.xt.homework; public class Student { private S ...