mysql增删查改练习
建表
班级表
create table class(
cid int auto_increment unique,
caption varchar(32) not null default ''
)charset utf8;
insert into class (caption) values ('三年一班'),('三年二班'),('三年三班'),('三年四班');
mysql> select * from class;
+-----+----------+
| cid | caption |
+-----+----------+
| 1 | 三年一班 |
| 2 | 三年二班 |
| 3 | 三年三班 |
| 4 | 三年四班 |
+-----+----------+
4 rows in set (0.01 sec)
学生表
create table student(
sid int auto_increment unique,
sname varchar(32) not null default '',
gender enum('女','男'),
class_id int not null default 0,
constraint stu_cla foreign key (class_id) references class(cid)
)charset utf8;
insert into student (sname,gender,class_id) values ('张三','女',1);
insert into student (sname,gender,class_id) values ('李四','女',2);
insert into student (sname,gender,class_id) values ('钢弹','男',3);
insert into student (sname,gender,class_id) values ('张铁蛋','男',3);
insert into student (sname,gender,class_id) values ('张兰兰','女',4);
insert into student (sname,gender,class_id) values ('王二','男',1);
mysql> select * from student;
+-----+--------+--------+----------+
| sid | sname | gender | class_id |
+-----+--------+--------+----------+
| 1 | 张三 | 女 | 1 |
| 2 | 李四 | 女 | 2 |
| 3 | 钢弹 | 男 | 3 |
| 4 | 张铁蛋 | 男 | 3 |
| 5 | 张兰兰 | 女 | 4 |
| 6 | 王二 | 男 | 1 |
+-----+--------+--------+----------+
6 rows in set (0.01 sec)
老师表
create table teacher(
tid int auto_increment unique,
tname varchar(32) not null default ''
)charset utf8;
insert into teacher (tname) values ('叶平'),('李琴'),('李翠翠'),('李福贵'),('孙泉');
mysql> select * from teacher;
+-----+--------+
| tid | tname |
+-----+--------+
| 1 | 叶平 |
| 2 | 李琴 |
| 3 | 李翠翠 |
| 4 | 李福贵 |
| 5 | 孙泉 |
+-----+--------+
5 rows in set (0.00 sec)
课程表
create table course(
cid int auto_increment unique,
cname varchar(32) not null default '',
teacher_id int not null default 0,
constraint cour_tea foreign key (teacher_id) references teacher(tid)
)charset utf8;
insert into course (cname,teacher_id) values ('生物',1),('体育',5),('物理',4),('语文',2),('英语',3);
mysql> select * from course;
+-----+-------+------------+
| cid | cname | teacher_id |
+-----+-------+------------+
| 1 | 生物 | 1 |
| 2 | 体育 | 5 |
| 3 | 物理 | 4 |
| 4 | 语文 | 2 |
| 5 | 英语 | 3 |
+-----+-------+------------+
5 rows in set (0.00 sec)
成绩表
create table score(
sid int auto_increment unique,
student_id int,
corse_id int,
number int not null default 0,
constraint sco_stuid foreign key (student_id) references student(sid),
constraint sco_corid foreign key (corse_id) references course(cid)
)charset utf8;
insert into score(student_id,corse_id,number) values (1,1,60),(1,2,59),(1,3,100),(1,4,85),(1,5,78);
insert into score(student_id,corse_id,number) values (2,1,77),(2,2,47),(2,3,89),(2,4,76),(2,5,38);
insert into score(student_id,corse_id,number) values (3,1,100),(3,2,84),(3,3,60),(3,4,68),(3,5,71);
insert into score(student_id,corse_id,number) values (3,1,86),(4,2,45),(5,3,39);
mysql> select * from score;
+-----+------------+----------+--------+
| sid | student_id | corse_id | number |
+-----+------------+----------+--------+
| 1 | 1 | 1 | 60 |
| 2 | 1 | 2 | 59 |
| 3 | 1 | 3 | 100 |
| 4 | 1 | 4 | 85 |
| 5 | 1 | 5 | 78 |
| 6 | 2 | 1 | 77 |
| 7 | 2 | 2 | 47 |
| 8 | 2 | 3 | 89 |
| 9 | 2 | 4 | 76 |
| 10 | 2 | 5 | 38 |
| 11 | 3 | 1 | 100 |
| 12 | 3 | 2 | 84 |
| 13 | 3 | 3 | 60 |
| 14 | 3 | 4 | 68 |
| 15 | 3 | 5 | 71 |
| 16 | 3 | 1 | 86 |
| 17 | 4 | 2 | 45 |
| 18 | 5 | 3 | 39 |
+-----+------------+----------+--------+
20 rows in set (0.00 sec)
数据库语句练习
-- 1. 查询所有大于60分的学生的姓名和学号 (DISTINCT: 去重)
mysql> select distinct student.sid,student.sname from student left join score on student_id = student.sid where number>60;
+-----+-------+
| sid | sname |
+-----+-------+
| 1 | 张三 |
| 2 | 李四 |
| 3 | 钢弹 |
+-----+-------+
3 rows in set (0.01 sec)
-- 2.查询每个老师教授的课程数量 和 老师信息
mysql> select teacher.tid,teacher.tname,count(course.teacher_id) as course_cou from teacher left join course on teacher.tid=teacher_id group by course.teacher_id;
+-----+--------+------------+
| tid | tname | course_cou |
+-----+--------+------------+
| 5 | 孙泉 | 1 |
| 4 | 李福贵 | 1 |
| 1 | 叶平 | 1 |
| 3 | 李翠翠 | 1 |
| 2 | 李琴 | 1 |
+-----+--------+------------+
5 rows in set (0.00 sec)
-- 3. 查询学生的信息以及学生所在的班级信息
mysql> select student.sid,student.sname,student.gender,class.caption from class left join student on class_id= class.cid;
+------+--------+--------+----------+
| sid | sname | gender | caption |
+------+--------+--------+----------+
| 1 | 张三 | 女 | 三年一班 |
| 6 | 王二 | 男 | 三年一班 |
| 2 | 李四 | 女 | 三年二班 |
| 3 | 钢弹 | 男 | 三年三班 |
| 4 | 张铁蛋 | 男 | 三年三班 |
| 5 | 张兰兰 | 女 | 三年四班 |
+------+--------+--------+----------+
6 rows in set (0.00 sec)
-- 4、学生中男生的个数和女生的个数
mysql> select student.gender,count(student.gender) as gender_sum from class left join student on class_id= class.cid group by student.gender;
+--------+------------+
| gender | gender_sum |
+--------+------------+
| 女 | 3 |
| 男 | 3 |
+--------+------------+
2 rows in set (0.00 sec)
-- 5、获取所有学习'生物'的学生的学号和成绩;姓名
mysql> select student.sid,student.sname,number from student left join score on student_id= student.sid left join course on corse_id = course.cid where course.cid = 1;
+-----+-------+--------+
| sid | sname | number |
+-----+-------+--------+
| 1 | 张三 | 60 |
| 2 | 李四 | 77 |
| 3 | 钢弹 | 100 |
| 3 | 钢弹 | 86 |
+-----+-------+--------+
4 rows in set (0.00 sec)
-- 6、查询平均成绩大于60分的同学的学号和平均成绩;
mysql> select student.sid,avg(score.number) from student left join score on student_id= student.sid group by score.student_id having avg(score.number)>60;
+-----+-------------------+
| sid | avg(score.number) |
+-----+-------------------+
| 1 | 76.4000 |
| 2 | 65.4000 |
| 3 | 78.1667 |
+-----+-------------------+
3 rows in set (0.01 sec)
-- 7、查询姓“李”的老师的个数;
mysql> select count(tname) as li_scount from teacher where tname like '李%';
+-----------+
| li_scount |
+-----------+
| 3 |
+-----------+
1 row in set (0.00 sec)
-- 8、查询课程成绩小于60分的同学的学号、姓名;
mysql> select student.sid,student.sname from student left join score on student_id= student.sid left join course on corse_id = course.cid where score.number<60;
+-----+--------+
| sid | sname |
+-----+--------+
| 1 | 张三 |
| 2 | 李四 |
| 2 | 李四 |
| 4 | 张铁蛋 |
| 5 | 张兰兰 |
+-----+--------+
5 rows in set (0.00 sec)
-- 9. 删除学习“叶平”老师课的SC表记录
# 修改之前
mysql> select * from score;
+-----+------------+----------+--------+
| sid | student_id | corse_id | number |
+-----+------------+----------+--------+
| 1 | 1 | 1 | 60 |
| 2 | 1 | 2 | 59 |
| 3 | 1 | 3 | 100 |
| 4 | 1 | 4 | 85 |
| 5 | 1 | 5 | 78 |
| 7 | 2 | 2 | 47 |
| 8 | 2 | 3 | 89 |
| 9 | 2 | 4 | 76 |
| 10 | 2 | 5 | 38 |
| 11 | 3 | 1 | 100 |
| 12 | 3 | 2 | 84 |
| 13 | 3 | 3 | 60 |
| 14 | 3 | 4 | 68 |
| 15 | 3 | 5 | 71 |
| 16 | 3 | 1 | 86 |
| 17 | 4 | 2 | 45 |
| 18 | 5 | 3 | 39 |
+-----+------------+----------+--------+
17 rows in set (0.00 sec)
# 修改成功
mysql> delete from score where corse_id in (select course.cid from course left join teacher on course.teacher_id=teacher.tid where teacher.tname='叶平');
Query OK, 3 rows affected (0.01 sec)
# 修改之后
mysql> select * from score;
+-----+------------+----------+--------+
| sid | student_id | corse_id | number |
+-----+------------+----------+--------+
| 2 | 1 | 2 | 59 |
| 3 | 1 | 3 | 100 |
| 4 | 1 | 4 | 85 |
| 5 | 1 | 5 | 78 |
| 7 | 2 | 2 | 47 |
| 8 | 2 | 3 | 89 |
| 9 | 2 | 4 | 76 |
| 10 | 2 | 5 | 38 |
| 12 | 3 | 2 | 84 |
| 13 | 3 | 3 | 60 |
| 14 | 3 | 4 | 68 |
| 15 | 3 | 5 | 71 |
| 17 | 4 | 2 | 45 |
| 18 | 5 | 3 | 39 |
+-----+------------+----------+--------+
14 rows in set (0.00 sec)
-- 10.查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
mysql> select course.cid,max(number),min(number) from course left join score on corse_id= course.cid group by score.corse_id;
+-----+-------------+-------------+
| cid | max(number) | min(number) |
+-----+-------------+-------------+
| 1 | 100 | 60 |
| 2 | 84 | 45 |
| 3 | 100 | 39 |
| 4 | 85 | 68 |
| 5 | 78 | 38 |
+-----+-------------+-------------+
5 rows in set (0.00 sec)
-- 11.查询每门课程被选修的学生数
mysql> select course.cid,count(student_id) from course left join score on corse_id= course.cid group by score.corse_id;
+-----+-------------------+
| cid | count(student_id) |
+-----+-------------------+
| 1 | 4 |
| 2 | 4 |
| 3 | 4 |
| 4 | 3 |
| 5 | 3 |
+-----+-------------------+
5 rows in set (0.00 sec)
-- 12.查询姓“张”的学生名单;
mysql> select sname as zhang_count from student where sname like '张%';
+-------------+
| zhang_count |
+-------------+
| 张三 |
| 张铁蛋 |
| 张兰兰 |
+-------------+
3 rows in set (0.00 sec)
-- 13.查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
mysql> select course.cid,avg(number) from course left join score on corse_id= course.cid group by score.corse_id order by avg(number),course.cid desc;
+-----+-------------+
| cid | avg(number) |
+-----+-------------+
| 2 | 58.7500 |
| 5 | 62.3333 |
| 3 | 72.0000 |
| 4 | 76.3333 |
| 1 | 80.7500 |
+-----+-------------+
5 rows in set (0.00 sec)
-- 14.查询平均成绩大于85的所有学生的学号、姓名和平均成绩
# 因为自己录入的成绩平均分没有85以上,就降低要求 70分以上
mysql> select student.sid,student.sname,avg(number) from student left join score on student_id= student.sid group by student_id having avg(number)>70;
+-----+-------+-------------+
| sid | sname | avg(number) |
+-----+-------+-------------+
| 1 | 张三 | 76.4000 |
| 3 | 钢弹 | 78.1667 |
+-----+-------+-------------+
2 rows in set (0.00 sec)
-- 15.查询课程编号为3且课程成绩在80分以上的学生的学号和姓名;
mysql> select student.sid,student.sname from student left join score on student_id= student.sid where corse_id = 3 and number >80;
+-----+-------+
| sid | sname |
+-----+-------+
| 1 | 张三 |
| 2 | 李四 |
+-----+-------+
2 rows in set (0.00 sec)
-- 16.查询各个课程及相应的选修人数
mysql> select course.cname,count(student_id) as stu_sum from course left join score on corse_id= course.cid group by corse_id;
+-------+---------+
| cname | stu_sum |
+-------+---------+
| 生物 | 4 |
| 体育 | 4 |
| 物理 | 4 |
| 语文 | 3 |
| 英语 | 3 |
+-------+---------+
5 rows in set (0.00 sec)
-- 17.查询“4”课程分数小于60,按分数降序排列的同学学号
# 因为录入的4课程成绩分数大于60,就换成2课程
mysql> select student.sid from student left join score on student_id= student.sid where corse_id = 2 and number<60 order by student.sid desc;
+-----+
| sid |
+-----+
| 4 |
| 2 |
| 1 |
+-----+
3 rows in set (0.00 sec)
-- 18.删除学号为“2”的同学的“1”课程的成绩
# 未修改之前
mysql> select * from score;
+-----+------------+----------+--------+
| sid | student_id | corse_id | number |
+-----+------------+----------+--------+
| 1 | 1 | 1 | 60 |
| 2 | 1 | 2 | 59 |
| 3 | 1 | 3 | 100 |
| 4 | 1 | 4 | 85 |
| 5 | 1 | 5 | 78 |
| 6 | 2 | 1 | 77 |
| 7 | 2 | 2 | 47 |
| 8 | 2 | 3 | 89 |
| 9 | 2 | 4 | 76 |
| 10 | 2 | 5 | 38 |
| 11 | 3 | 1 | 100 |
| 12 | 3 | 2 | 84 |
| 13 | 3 | 3 | 60 |
| 14 | 3 | 4 | 68 |
| 15 | 3 | 5 | 71 |
| 16 | 3 | 1 | 86 |
| 17 | 4 | 2 | 45 |
| 18 | 5 | 3 | 39 |
+-----+------------+----------+--------+
18 rows in set (0.00 sec)
# 修改成功
mysql> delete from score where corse_id = 1 and student_id = 2;
Query OK, 1 row affected (0.01 sec)
# 修改成功后
mysql> select * from score;
+-----+------------+----------+--------+
| sid | student_id | corse_id | number |
+-----+------------+----------+--------+
| 1 | 1 | 1 | 60 |
| 2 | 1 | 2 | 59 |
| 3 | 1 | 3 | 100 |
| 4 | 1 | 4 | 85 |
| 5 | 1 | 5 | 78 |
| 7 | 2 | 2 | 47 |
| 8 | 2 | 3 | 89 |
| 9 | 2 | 4 | 76 |
| 10 | 2 | 5 | 38 |
| 11 | 3 | 1 | 100 |
| 12 | 3 | 2 | 84 |
| 13 | 3 | 3 | 60 |
| 14 | 3 | 4 | 68 |
| 15 | 3 | 5 | 71 |
| 16 | 3 | 1 | 86 |
| 17 | 4 | 2 | 45 |
| 18 | 5 | 3 | 39 |
+-----+------------+----------+--------+
17 rows in set (0.00 sec)
mysql增删查改练习的更多相关文章
- php mysql增删查改
php mysql增删查改代码段 $conn=mysql_connect('localhost','root','root'); //连接数据库代码 mysql_query("set na ...
- mysql 增删查改
非关系型数据库关系型数据库Oracle mysql sqlserver db2 Postgresql Sqlite access sqlserver 微软db2 ibm================ ...
- node.js+mysql增删查改
数据库和表: -- -- 数据库: `test` -- -- -------------------------------------------------------- -- -- 表的结构 ` ...
- 后端Spring Boot+前端Android交互+MySQL增删查改(Java+Kotlin实现)
1 前言&概述 这篇文章是基于这篇文章的更新,主要是更新了一些技术栈以及开发工具的版本,还有修复了一些Bug. 本文是SpringBoot+Android+MySQL的增删查改的简单实现,用到 ...
- 后端Spring Boot+前端Android交互+MySQL增删查改
2021.1.27 更新 已更新新版本博客,更新内容很多,因此新开了一篇博客,戳这里. 1 概述 使用spring boot作为后端框架与Android端配合mysql进行基本的交互,包含了最基本的增 ...
- MySQL 增删查改 必知必会
MySQL 数据库中的基础操作 3.表的修改 对表的表名.字段.字段类型.字段长度.约束等进行修改. 3.1 表的名称修改 -- 语法: ALTER TABLE 库名.表名 RENAME TO 新表名 ...
- python操作mysql增删查改
# coding=utf-8 ''' python操作mysql,需安装MySQLdb驱动 安装MySQLdb,请访问 http://sourceforge.net/projects/mysql-py ...
- 2016/3/13 MySQL 增删查改 CRUD 用代码实现
用代码实现数据库数据输入 T-SQL语句 查询语句分几块 ①创建表 create table Car (Code varchar(50) primary key, #primary key 主键 定义 ...
- MySQL增删查改语句(入门)
目录 create alter: insert delete update select 数据库定义语句: create:创建数据库及表对象 drop:删除数据库及表对象 alter:修改数据库及表对 ...
随机推荐
- 深入理解C语言-深入理解内存四区
数组与指针 当数组做函数参数的时候,会退化为一个指针 此时在函数内是得不到数组大小的 因此,数组做函数参数的时候需要传递数组大小,也就是多传递一个参数 void func(int arr[], int ...
- Springboot使用launch.script打包后解压缩
今天拿到一个SpringBoot的jar需要反编译看一下.直接用工具试了下提示报错. 于是用文本工具打开看了下,发现此JAR包在打包时候引入了启动脚本.如下图: 为了反编译,可以直接将所有启动脚本相关 ...
- [转帖]k8s.gcr.io/pause的作用
k8s.gcr.io/pause的作用 https://blog.51cto.com/liuzhengwei521/2422120 weilovepan520关注0人评论196人阅读2019-07-2 ...
- 【一个蒟蒻的挣扎】最小生成树—Kruskal算法
济南集训第五天的东西,这篇可能有点讲不明白提前抱歉(我把笔记忘到别的地方了 最小生成树 概念:一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的 ...
- python 2 和 python 3的区别
python2和python3区别 python2:源码不统一,源码(功能)重复,维护困难,除法的时候返回来的是小数点,()浮点数 python3:源码统一,源码不重复,除法的时候返回来的是整 ...
- Python格式化输出的三种方式
Python格式化输出的三种方式 一.占位符 程序中经常会有这样场景:要求用户输入信息,然后打印成固定的格式比如要求用户输入用户名和年龄,然后打印如下格式:My name is xxx,my age ...
- 进阶Java编程(9)反射与类操作
1,反射获取类结构信息 在反射机制的处理过程之中不仅仅只是一个实例化对象的处理操作,更多的情况下还有类的组成结构操作,任何一个类的基本组成结构:父类(父接口).包.属性.方法(构造方法与普通方法). ...
- [C#.net]使用Thread.Sleep界面卡死的问题解决方法
很多初学者在写C#程序的时候,需要程序等待某个时间,但是又不想用比较繁琐的线程等操作,因此用Thread.Sleep()函数,但是这个函数在等待过程中会操作界面的卡死,那么,如何能保证既不卡死又能达到 ...
- 【zhifu】web开发中的支付宝支付和微信支付
一.支付类型: 支付宝支付: 支付宝app内的网页支付: app外(即普通浏览器)网页支付: 微信支付: 微信app内的支付(在这里叫公众号支付) app外的支付(微信H5支付): 微信公众号的支付宝 ...
- 深入探讨java的类加载器
类加载器是 Java 语言的一个创新,也是 Java 语言流行的重要原因之一.它使得 Java 类可以被动态加载到 Java 虚拟机中并执行.类加载器从 JDK 1.0 就出现了,最初是为了满足 Ja ...