MySQL典型示例

目录

1.使用环境

2.设计表

3.创建表

4.准备数据

5.查询


1.使用环境

数据库:MySQL 8.0.30

客户端:Navicat 15.0.12

2.设计表

假设我们已经建好了一个名为test的数据库。我们添加如下几个表:教师、课程、学生、班级、成绩。实体联系图设计如下:

3.创建表

sql 复制代码
CREATE TABLE class (
  cid int(10) PRIMARY KEY AUTO_INCREMENT,
  cname varchar(50) NOT NULL
);

> OK

> 时间: 0.006s

sql 复制代码
CREATE TABLE teacher(
  tid int(10) PRIMARY KEY AUTO_INCREMENT,
  tname varchar(50) NOT NULL
);

> OK

> 时间: 0.006s

sql 复制代码
CREATE TABLE course(
  cid int(11) PRIMARY KEY AUTO_INCREMENT,
  cname varchar(32) NOT NULL,
  teacher_id int(11) NOT NULL,
  FOREIGN KEY (teacher_id) REFERENCES teacher (tid)
) ;

> OK

> 时间: 0.011s

sql 复制代码
CREATE TABLE student(
  sid int(10) PRIMARY KEY AUTO_INCREMENT,
	sname varchar(50) NOT NULL,
  gender char(1) NOT NULL,
  c_id int(10) NOT NULL,
  FOREIGN KEY (c_id) REFERENCES class (cid)
);

> OK

> 时间: 0.009s

sql 复制代码
CREATE TABLE score (
  sid int(10) PRIMARY KEY AUTO_INCREMENT,
  c_id int(10) NOT NULL,
  s_id int(10) NOT NULL,
  snum int(10) NOT NULL,
  FOREIGN KEY (c_id) REFERENCES course(cid),
  FOREIGN KEY (s_id) REFERENCES student(sid)
);

> OK

> 时间: 0.012s

通过以上的语句,就分别把表创建好了。

4.准备数据

创建表以后,只是有了结构,还没有数据,所以我们要准备一些测试数据。

sql 复制代码
INSERT INTO class VALUES
(1,'一年一班'), 
(2,'二年一班'), 
(3,'三年一班'), 
(4,'三年二班'),
(5,'三年三班');

> Affected rows: 5

> 时间: 0.001s

sql 复制代码
INSERT INTO teacher VALUES
(1, '张三老师'), 
(2, '李四老师'), 
(3, '王五老师'), 
(4, '赵六老师'), 
(5, '刘七老师');

> Affected rows: 5

> 时间: 0.001s

sql 复制代码
INSERT INTO course VALUES
(1, '语文', 1), 
(2, '物理', 2), 
(3, '英语', 3), 
(4, '化学', 2);

> Affected rows: 4

> 时间: 0.002s

sql 复制代码
INSERT INTO student VALUES
(1, '张一一', '男', 1), 
(2, '张一二', '女', 1), 
(3, '张一三', '男', 1), 
(4, '王一一', '男', 1), 
(5, '王一二', '女', 1), 
(6, '王一三', '男', 1), 
(7, '李一一', '女', 2), 
(8, '李一二', '男', 2), 
(9, '李一三', '男', 2), 
(10, '李一四', '女', 2), 
(11, '赵一', '男', 2), 
(12, '赵二', '女', 3), 
(13, '赵三', '男', 3), 
(14, '刘一一', '男', 3), 
(15, '刘一二', '女', 3), 
(16, '刘一三', '男', 3);

> Affected rows: 16

> 时间: 0.001s

sql 复制代码
INSERT INTO score VALUES
(1, 1, 1, 70),
(2, 2, 1, 90),
(3, 3, 1, 87),
(4, 4, 1, 69),
(5, 1, 2, 66),
(6, 2, 2, 98),
(8, 3, 2, 68),
(9, 4, 2, 99),
(10, 1, 3, 77),
(11, 2, 3, 66),
(12, 3, 3, 87),
(13, 4, 3, 99),
(14, 1, 4, 79),
(15, 2, 4, 11),
(16, 3, 4, 67),
(17, 4, 4, 100),
(18, 1, 5, 79),
(19, 2, 5, 11),
(20, 3, 5, 67),
(21, 4, 5, 100),
(22, 1, 6, 29),
(23, 2, 6, 100),
(24, 3, 6, 67),
(25, 4, 6, 100),
(26, 1, 7, 91),
(27, 2, 7, 100),
(28, 3, 7, 67),
(29, 4, 7, 88),
(30, 1, 8, 69),
(31, 2, 8, 100),
(32, 3, 8, 67),
(33, 4, 8, 88),
(34, 1, 9, 91),
(35, 2, 9, 88),
(36, 3, 9, 67),
(37, 4, 9, 62),
(38, 1, 10, 90),
(39, 2, 10, 77),
(40, 3, 10, 43),
(41, 4, 10, 87),
(42, 1, 11, 90),
(43, 2, 11, 77),
(44, 3, 11, 43),
(45, 4, 11, 87),
(46, 1, 12, 90),
(47, 2, 12, 77),
(48, 3, 12, 43),
(49, 4, 12, 97),
(50, 1, 13, 63),
(51, 2, 13, 98),
(52, 3, 13, 65),
(53, 4, 13, 79);

> Affected rows: 52

> 时间: 0.003s

5.查询

查询所有课程的名称以及对应的任课老师姓名:

sql 复制代码
select c.cname,t.tname from course as c left join teacher as t on c.t_id = t.tid;

查询男女学生各有多少人:

sql 复制代码
select gender,count(1) as count from student group by gender;

查询姓李老师的数量:

sql 复制代码
select count(1) as count from teacher where tname like '李%';

查询物理成绩等于100的学生姓名:

sql 复制代码
select t.sname from student as t inner join score as s on s.s_id = t.sid
inner join course as c on c.cid = s.c_id where s.snum = 100 and c.cname = '物理';

查询平均成绩大于八十分的同学姓名和平均成绩:

sql 复制代码
select t.sname,avg(snum) as savg from student as t inner join score as s on s.s_id = t.sid group by t.sid having avg(snum) > 80;

查询报了物理也报了化学这两门课程的学生学号和对应科目的分数:

sql 复制代码
select * from
(select s_id,snum as wulisum  
from score as s inner join course as c on s.c_id = c.cid
where cname = '物理') as wuli inner join
(select s_id,snum as huaxuesum from score as s inner join course as c on s.c_id = c.cid
where cname = '化学') as huaxue on wuli.s_id = huaxue.s_id;

查询物理成绩不及格的学生姓名和对应分数:

sql 复制代码
select t.sname,snum from student as t inner join score as s on s.s_id = t.sid 
inner join course as c on c.cid = s.c_id where c.cname = '物理' and snum < 60;
相关推荐
SkyWalking中文站8 小时前
BanyanDB 0.11.0:新特性与升级指南
数据库
₍˄·͈༝·͈˄*₎◞ ̑̑码10 小时前
MyBatis操作数据库
数据库·mybatis
刃神太酷啦12 小时前
Redis 进阶核心:持久化 (RDB/AOF)、事务与主从复制全解析----《Hello Redis!》(5)
linux·c语言·数据库·c++·redis·缓存·bootstrap
丁丁点灯o12 小时前
Oracle中使用外键的场景及不适用外键的情况
数据库·oracle
天天进步201512 小时前
Pixelle-Video 源码解析 #18:声音克隆功能:参考音频如何影响解说效果?
数据库·音视频
不懂的浪漫13 小时前
ToDesk 连接 Linux 后分辨率过低的解决方法
linux·运维·数据库
布莱克60513 小时前
Redis 详解:从核心数据结构到高可用架构
数据库
冰暮流星14 小时前
mysql之左外连接与右外连接
数据库·sql
jyOverQ14 小时前
MySQL 联合索引怎么用?最左匹配原则到底是什么意思?
数据库·mysql
没文化的阿浩14 小时前
【MySQL】用户管理
android·mysql·adb