建库

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

mysql> create database if not exists linux5 charset utf8 collate utf8_general_ci;

Query OK, 1 row affected (0.00 sec)

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| linux5 |

| mysql |

| performance_schema |

| test |

| world |

+--------------------+

建表

表名:student(学生表)

字段

数据类型要求

是否为空

注释

sno

最多20位

学号(主键)

sname

可变长

学生姓名

sage

最小整数,非负数

学生年龄

ssex

0,1

学生性别(1是男,0是女)默认为男)

sbirthday

时间类型

默认为空

学生生日

class

可变长

学生班级

mysql> create table student(

sno bigint(20) #zerofill# not null primary key auto_increment comment '学号',

sname varchar(12) not null comment '学生姓名',

sage tinyint unsigned not null comment '学生年龄',

ssex enum('0','1') not null default '1' comment '学生性别(1是男,0是女)默认为男',

sbirthday datetime comment '学生生日',

class varchar(10) not null comment '学生班级');

mysql> desc student;

+-----------+---------------------+------+-----+---------+----------------+

| Field | Type | Null | Key | Default | Extra |

+-----------+---------------------+------+-----+---------+----------------+

| sno | bigint(20) | NO | PRI | NULL | auto_increment |

| sname | varchar(12) | NO | | NULL | |

| sage | tinyint(3) unsigned | NO | | NULL | |

| ssex | enum('0','1') | NO | | 1 | |

| sbirthday | datetime | YES | | NULL | |

| class | varchar(10) | NO | | NULL | |

+-----------+---------------------+------+-----+---------+----------------+

表名:course(课程表)

字段

数据类型要求

是否为空

注释

cno

最多20位

课程号(主键)

cname

可变长

课程名称

tno

可变长

教师编号

mysql> create table course(

cno bigint(20) not null primary key auto_increment comment '课程号',

cname varchar(20) not null comment '课程名称',

tno varchar(20) not null comment '教师编号');

mysql> desc course;

+-------+-------------+------+-----+---------+----------------+

| Field | Type | Null | Key | Default | Extra |

+-------+-------------+------+-----+---------+----------------+

| cno | bigint(20) | NO | PRI | NULL | auto_increment |

| cname | varchar(20) | NO | | NULL | |

| tno | varchar(20) | NO | | NULL | |

+-------+-------------+------+-----+---------+----------------+

表名:score(成绩表)

字段

数据类型要求

是否为空

注释

sno

最多20位

学号(主键)

cno

最多20位

课程号(主键)

mark

浮点数(4,1)

成绩

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

mysql> create table score(

sno bigint(20) comment '学号',

cno bigint(20) comment '课程号',

mark float(4,1) not null comment '成绩'),

primary key (sno,cno));

mysql> desc score;

+-------+------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+-------+------------+------+-----+---------+-------+

| sno | bigint(20) | NO | PRI | NULL | |

| cno | bigint(20) | NO | PRI | NULL | |

| mark | float(4,1) | NO | | NULL | |

+-------+------------+------+-----+---------+-------+

表名:teacher(教师表)

字段

数据类型要求

是否为空

注释

tno

最多20位

教师编号(主键)

tname

可变长

教师姓名

tage

最小整数,非负数

教师年龄

tsex

0,1

教师性别(1是男,0是女)默认为男)

prof

可变长

教师职称

depart

可变长

教师部门

mysql> create table teacher(

tno bigint(20) not null primary key auto_increment comment '教师编号',

tname varchar(12) not null comment '教师姓名',

tage tinyint unsigned not null comment '教师年龄',

tsex enum('0','1') not null default '1' comment '教师性别(1是男,0是女)默认为男',

prof varchar(20) comment '教师职称',

depart varchar(20) not null comment '教师部门');

mysql> desc teacher;

+--------+---------------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+--------+---------------------+------+-----+---------+-------+

| tno | bigint(20) | NO | PRI | NULL | |

| tname | varchar(12) | NO | | NULL | |

| tage | tinyint(3) unsigned | NO | | NULL | |

| tsex | enum('0','1') | NO | | 1 | |

| prof | varchar(20) | YES | | NULL | |

| depart | varchar(20) | NO | | NULL | |

+--------+---------------------+------+-----+---------+-------+

练习题

插入数据练习:

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

mysql> insert into student(sname,sage,ssex,class) values('张毅',18,'1',5),('李琦',18,'1',5),('王相伟',18,'1',5),('庄晨浩',18,'1',5),('黄杨浩',18,'1',5),('巩景云',18,'0',5),('陈妙',18,'0',5),('朱慧萍',18,'0',5);

mysql> select * from student;

+-----+------------+------+------+-----------+-------+

| sno | sname | sage | ssex | sbirthday | class |

+-----+------------+------+------+-----------+-------+

| 9 | 张毅 | 18 | 1 | NULL | 5 |

| 10 | 李琦 | 18 | 1 | NULL | 1 |

| 11 | 王相伟 | 18 | 1 | NULL | 2 |

| 12 | 庄晨浩 | 18 | 1 | NULL | 5 |

| 13 | 黄杨浩 | 18 | 1 | NULL | 5 |

| 14 | 巩景云 | 18 | 0 | NULL | 5 |

| 15 | 陈妙 | 18 | 0 | NULL | 5 |

| 16 | 朱 慧萍 | 18 | 0 | NULL | 5 |

+-----+------------+------+------+-----------+-------+

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

mysql> insert into teacher(tno,tname,tage,prof,depart) values(1,'曾导',20,'讲师','linux'),(2,'徐导',20,'讲师','linux'),(3,'李导',20,'讲师','linux')(4,'邱导',20,'讲师','python'),(5,'林导',20,'讲师','DBA');

mysql> select * from teacher;

+-----+--------+------+------+--------+--------+

| tno | tname | tage | tsex | prof | depart |

+-----+--------+------+------+--------+--------+

| 1 | 曾导 | 20 | 1 | 讲师 | linux |

| 2 | 徐导 | 20 | 1 | 讲师 | linux |

| 3 | 李导 | 20 | 1 | 讲师 | linux |

| 4 | 邱导 | 20 | 1 | 讲师 | python |

| 5 | 林导 | 20 | 1 | 讲师 | DBA |

+-----+--------+------+------+--------+--------+

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

mysql> insert into course(cno,cname,tno) values(1,'DBA',1),(2,'python',2),(3,'linux',1);

mysql> select * from course;

+-----+--------+-----+

| cno | cname | tno |

+-----+--------+-----+

| 1 | DBA | 1 |

| 2 | python | 2 |

| 3 | linux | 1 |

+-----+--------+-----+

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

mysql> insert into score(sno,cno,mark) values(9,'1',98),(10,'1',8),(11,'2',80),(12,'1',65),(13,'2',98),(14,'1',98),(15,'1',98),(16,'1',98);

mysql> select * from score;

+-----+-----+------+

| sno | cno | mark |

+-----+-----+------+

| 9 | 1 | 98.0 |

| 10 | 1 | 8.0 |

| 11 | 2 | 80.0 |

| 12 | 1 | 65.0 |

| 13 | 2 | 98.0 |

| 14 | 1 | 98.0 |

| 15 | 1 | 98.0 |

| 16 | 1 | 98.0 |

+-----+-----+------+

查询练习:

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

mysql> select sname,ssex,class from student;

+------------+------+-------+

| sname | ssex | class |

+------------+------+-------+

| 张毅 | 1 | 5 |

| 李琦 | 1 | 1 |

| 王相伟 | 1 | 2 |

| 庄晨浩 | 1 | 5 |

| 黄杨浩 | 1 | 5 |

| 巩景云 | 0 | 5 |

| 陈妙 | 0 | 5 |

| 朱 慧萍 | 0 | 5 |

+------------+------+-------+

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

mysql> select distinct(depart) from teacher;

+--------+

| depart |

+--------+

| linux |

| python |

| DBA |

+--------+

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

mysql> select * from student;

+-----+------------+------+------+-----------+-------+

| sno | sname | sage | ssex | sbirthday | class |

+-----+------------+------+------+-----------+-------+

| 9 | 张毅 | 18 | 1 | NULL | 5 |

| 10 | 李琦 | 18 | 1 | NULL | 1 |

| 11 | 王相伟 | 18 | 1 | NULL | 2 |

| 12 | 庄晨浩 | 18 | 1 | NULL | 5 |

| 13 | 黄杨浩 | 18 | 1 | NULL | 5 |

| 14 | 巩景云 | 18 | 0 | NULL | 5 |

| 15 | 陈妙 | 18 | 0 | NULL | 5 |

| 16 | 朱 慧萍 | 18 | 0 | NULL | 5 |

+-----+------------+------+------+-----------+-------+

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

mysql> select * from score where mark>60 and mark<80;

+-----+-----+------+

| sno | cno | mark |

+-----+-----+------+

| 12 | 1 | 65.0 |

+-----+-----+------+

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

mysql> select * from score where mark=98 or mark=80 or mark=65;

mysql> select * from score where mark in (98,80,65);

+-----+-----+------+

| sno | cno | mark |

+-----+-----+------+

| 9 | 1 | 98.0 |

| 11 | 2 | 80.0 |

| 12 | 1 | 65.0 |

| 13 | 2 | 98.0 |

| 14 | 1 | 98.0 |

| 15 | 1 | 98.0 |

| 16 | 1 | 98.0 |

+-----+-----+------+

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

mysql> select * from student where class='1' or ssex='0';

+-----+------------+------+------+-----------+-------+

| sno | sname | sage | ssex | sbirthday | class |

+-----+------------+------+------+-----------+-------+

| 10 | 李琦 | 18 | 1 | NULL | 1 |

| 14 | 巩景云 | 18 | 0 | NULL | 5 |

| 15 | 陈妙 | 18 | 0 | NULL | 5 |

| 16 | 朱 慧萍 | 18 | 0 | NULL | 5 |

+-----+------------+------+------+-----------+-------+

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

mysql> select * from student order by class desc;

+-----+------------+------+------+-----------+-------+

| sno | sname | sage | ssex | sbirthday | class |

+-----+------------+------+------+-----------+-------+

| 9 | 张毅 | 18 | 1 | NULL | 5 |

| 12 | 庄晨浩 | 18 | 1 | NULL | 5 |

| 13 | 黄杨浩 | 18 | 1 | NULL | 5 |

| 14 | 巩景云 | 18 | 0 | NULL | 5 |

| 15 | 陈妙 | 18 | 0 | NULL | 5 |

| 16 | 朱 慧萍 | 18 | 0 | NULL | 5 |

| 11 | 王相伟 | 18 | 1 | NULL | 2 |

| 10 | 李琦 | 18 | 1 | NULL | 1 |

+-----+------------+------+------+-----------+-------+

8 rows in set (0.00 sec)

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

mysql> select * from score order by mark desc,cno asc;

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

mysql> select count(sname) from student where class=5;

+--------------+

| count(sname) |

+--------------+

| 6 |

+--------------+

1 row in set (0.00 sec)

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

mysql> select student.sno as 学号,course.cname as 课程,score.mark as 分数

from student

join score on score.sno=student.sno

join course on score.cno=course.cno

where course.tno=1;

mysql> select teacher.tname,student.sname,course.cname,score.mark

from student

join score on score.sno=student.sno

join course on score.cno=course.cno

join teacher on teacher.tno=course.tno

where teacher.tname='曾导';

+--------+--------+--------+

| 学号 | 课程 | 分数 |

+--------+--------+--------+

| 9 | DBA | 98.0 |

| 10 | DBA | 8.0 |

| 12 | DBA | 65.0 |

| 14 | DBA | 98.0 |

| 15 | DBA | 98.0 |

| 16 | DBA | 98.0 |

+--------+--------+--------+

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

mysql> select student.sname,score.mark,course.cname,teacher.tname,teacher.prof,teacher.depart

from student

join score on score.sno=student.sno

join course on score.cno=course.cno

join teacher on teacher.tno=course.tno

where student.ssex='1' and course.cname='DBA';

+-----------+------+-------+--------+--------+--------+

| sname | mark | cname | tname | prof | depart |

+-----------+------+-------+--------+--------+--------+

| 张毅 | 98.0 | DBA | 曾导 | 讲师 | linux |

| 李琦 | 8.0 | DBA | 曾导 | 讲师 | linux |

| 庄晨浩 | 65.0 | DBA | 曾导 | 讲师 | linux |

+-----------+------+-------+--------+--------+--------+

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

mysql> select student.sname,score.mark,course.cname,teacher.tname,teacher.prof,teacher.depart

from student

join score on score.sno=student.sno

join course on score.cno=course.cno

join teacher on teacher.tno=course.tno

where student.ssex='1' and course.cname='DBA'

order by mark desc;

+-----------+------+-------+--------+--------+--------+

| sname | mark | cname | tname | prof | depart |

+-----------+------+-------+--------+--------+--------+

| 张毅 | 98.0 | DBA | 曾导 | 讲师 | linux |

| 庄晨浩 | 65.0 | DBA | 曾导 | 讲师 | linux |

| 李琦 | 8.0 | DBA | 曾导 | 讲师 | linux |

+-----------+------+-------+--------+--------+--------+

mysql score表_MySQL连表查询练习题相关推荐

  1. mysql course表_MySQL多表查询初探

    今天是我时隔五年再次动起键盘写文章,距离我上一次写文章,初中作文......所以我的文章并没有雕章琢句,更不会惊天地泣鬼神,只要大家能学到知识,我就很开心了.呀丫丫......又开始罗嗦一大堆没用的了 ...

  2. mysql带账号联查表_MySQL联表查询的简单示例

    MySql会用到联表查询,对于刚学习的新手来说,可能会理解起来有难度.下面这篇文章就来给大家详细介绍MySQL联表查询的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧 关系型 ...

  3. mysql中调用多个表_MySQL 多表查询

    文章转载的:http://www.cnblogs.com/BeginMan/p/3754322.html 一.多表查询方法分类 1.交叉连接查询(得到的是两个表的乘积,类似于矩阵乘积) select ...

  4. mysql左驱动表_MySQL多表联表查询驱动表选择

    联表查询做开发的小伙伴会经常使用,但是可以大家都比较少去深入了解MySQL是怎么执行多表联表查询的,比如怎么选择驱动表(第一个被处理的表),是先联表还是说先根据where条件(前提是有where条件) ...

  5. mysql子查询存到另一张表_MySQL多表查询与子查询

    多表查询 多表查询实际上根据查询要求先将两个表连接起来,形成一张新表,再在新表中查询出满足条件的记录多表查询可分为连接查询和子查询. 一. 连接查询(可分为外连接和内连接)关于外链接的几点说明: A. ...

  6. mysql历史表_MySQL历史表设计和查询

    TL; DR:这个设计是否正确,我应该如何查询? 我们假设我们有city和地址的历史记录表,其设计如下: CREATE TABLE city_history ( id BIGINT UNSIGNED ...

  7. mysql 实务操作_mysql多表操作

    多表查询 内连接:过滤为空的数据(查询的实际上是两张表数据的交集部分) select * from 表1,表2 where 表1.字段=表2.字段; //隐式内连接,使用where条件消除笛卡尔积 s ...

  8. mysql类型设计_mysql设计表结构数据类型的选择

    选择合适的数据类型 在使用MySQL创建数据表的时候会遇到一个问题,如何为字段选择合适的数据类型.比如创建一个员工信息表,每个字段都可以用很多种类型来定义, int,char,float等等. cha ...

  9. mysql分析表_MySQL分析表有什么用

    MySQL中使用ANALYZE TABLE语句来分析表,该语句的基本语法如下: ANALYZE TABLE 表名1 [,表名2-] ; 使用ANALYZE TABLE分析表的过程中,数据库系统会对表加 ...

最新文章

  1. java 折半插入排序_[Java代码] Java实现直接插入排序和折半插入排序算法示例
  2. java百度地图地名定位地址_百度地图定位显示省市区街道名称,非常实用
  3. 讲解Linux服务器被黑解决方法
  4. oracle打补丁到11.2.0.3.4
  5. python正则表达式使用实例_正则表达式的基础知识,以及Python爬虫中的使用方法...
  6. 转:html id与name区别
  7. oracle 表约束非空,oracle--约束(主键、非空、检查)
  8. 网络协议:TCP流量控制
  9. Neo4j-import导入CSV的数据
  10. linux Vi操作和使用方法详解
  11. WindowsNT/2000的系统日志文件
  12. mac homebrew加速
  13. 【latex】.tex文件去tracked changes
  14. @Value注入静态变量(static)
  15. 暂时性死区(TDZ)
  16. 【Arcpy】批量合并重叠拓扑
  17. 生于安乐,死于忧患.
  18. unity 下载文件到存放本地
  19. 【转】利用Windows API调用摄像头
  20. java 图片 白边_java 去除图片白边 两种方法的比较

热门文章

  1. oracle文件系统挂载点,挂载和取消挂载文件系统概述
  2. 取反!和按位取反~的区别
  3. 网络通信——ISO7层协议与TCP/IP 四层模型
  4. FL Studio水果最新版2023安装图文详细教程
  5. Linux下Subclipse的JavaHL
  6. 对数似然函数值/最大近然估计/log likelihood
  7. 虚拟机的迁移和复制分发
  8. 方差分析/卡方/fisher精确检验
  9. Java 以任意数量空格分割字符串方式
  10. git克隆指定分支的代码