建表
班级表
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增删查改练习的更多相关文章

  1. php mysql增删查改

    php mysql增删查改代码段 $conn=mysql_connect('localhost','root','root');  //连接数据库代码 mysql_query("set na ...

  2. mysql 增删查改

    非关系型数据库关系型数据库Oracle mysql sqlserver db2 Postgresql Sqlite access sqlserver 微软db2 ibm================ ...

  3. node.js+mysql增删查改

    数据库和表: -- -- 数据库: `test` -- -- -------------------------------------------------------- -- -- 表的结构 ` ...

  4. 后端Spring Boot+前端Android交互+MySQL增删查改(Java+Kotlin实现)

    1 前言&概述 这篇文章是基于这篇文章的更新,主要是更新了一些技术栈以及开发工具的版本,还有修复了一些Bug. 本文是SpringBoot+Android+MySQL的增删查改的简单实现,用到 ...

  5. 后端Spring Boot+前端Android交互+MySQL增删查改

    2021.1.27 更新 已更新新版本博客,更新内容很多,因此新开了一篇博客,戳这里. 1 概述 使用spring boot作为后端框架与Android端配合mysql进行基本的交互,包含了最基本的增 ...

  6. MySQL 增删查改 必知必会

    MySQL 数据库中的基础操作 3.表的修改 对表的表名.字段.字段类型.字段长度.约束等进行修改. 3.1 表的名称修改 -- 语法: ALTER TABLE 库名.表名 RENAME TO 新表名 ...

  7. python操作mysql增删查改

    # coding=utf-8 ''' python操作mysql,需安装MySQLdb驱动 安装MySQLdb,请访问 http://sourceforge.net/projects/mysql-py ...

  8. 2016/3/13 MySQL 增删查改 CRUD 用代码实现

    用代码实现数据库数据输入 T-SQL语句 查询语句分几块 ①创建表 create table Car (Code varchar(50) primary key, #primary key 主键 定义 ...

  9. MySQL增删查改语句(入门)

    目录 create alter: insert delete update select 数据库定义语句: create:创建数据库及表对象 drop:删除数据库及表对象 alter:修改数据库及表对 ...

随机推荐

  1. 将其它部分jsp代码包含进一个jsp文件

    <jsp:include page="文件路径/文件名.jsp" /> 将其它部分jsp代码包含进来.意义:将公共的部分代码抽出来,省略过多复制粘贴:只要改一个地方就O ...

  2. 算法巩固的第一天-java冒泡排序算法

    自媒体萌新一枚,不对的地方各路大神可以指点指点!个人理解: 冒泡排序算法<插入排序算法<快速排序算法 /** * 冒泡排序算法 * @author sj * */ public class ...

  3. Compress and decompress string

    You are given a string with lower case letters only. Compress it by putting the count of the letter ...

  4. Hadoop集群搭建-05安装配置YARN

    Hadoop集群搭建-04安装配置HDFS  Hadoop集群搭建-03编译安装hadoop Hadoop集群搭建-02安装配置Zookeeper Hadoop集群搭建-01前期准备 先保证集群5台虚 ...

  5. @click.prevent.self和@click.self.prevent区别

    注意:prevent 阻止的是“跳转事件”而不是“弹出警告” v-on:click.prevent.self的demo如下: <div id="box"> <di ...

  6. PATB1040/A1093 有几个PAT

    题目描述 The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th ...

  7. Python3迭代器与生成器

    迭代器 迭代是Python最强大的功能之一,是访问集合元素的一种方式. 迭代器是一个可以记住遍历的位置的对象. 迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退 ...

  8. 关于HA(2.102 -2.103 服务器排障)

    关于处理RHCA故障的报告: ,2.102 和 2.103 两台机器在重启之后拉不起来 原因是这两台服务比较怪 先要启动service rpcbind restart 然后再要起service nfs ...

  9. svn的一些使用技巧

    新项目的目录结构说明: 代码分三部分 1.01_Trunk(开发主干) 2.02_Branches(开发分支) 3.03_Tags(提交代码区) 文档区 请注意SVN的路径是区分大小写的! 与VSS的 ...

  10. LeetCode:175.组合两个表

    题目链接:https://leetcode-cn.com/problems/combine-two-tables/ 题目 表1: Person +-------------+---------+ | ...