MySQL笔试面试重点—增删改查,手把手解读(2)

外键用于关联其他表的主键或唯一键,语法:

foreign key (字段名) references 主表(列)

创建班级表classes,id为主键:

– 创建班级表,有使用MySQL关键字作为字段时,需要使用``来标识

drop table if exists classes;

create table classes(

id int primary key auto_increment,

name varchar(20),

desc varchar(100)

);

创建学生表student,一个学生对应一个班级,一个班级对应多个学生。使用id为主键, classes_id为外键,关联班级表id

drop table if exists student;

create table student(

id int primary key auto_increment,

sn int unique,

name varchar(20) default ‘unkown’,

qq_mail varchar(20),

classes_id int,

foreign key (classes_id) references classes(id)

);

1.7、CHECK约束(了解)


MySQL使用时不报错,但忽略该约束:

drop table if exists test_user;

create table test_user(

id int,

name varchar(20),

sex varchar(1),

check(sex = ‘男’ or sex = ‘女’)

);

二、表的设计

==========

2.1、三大范式


一对一

一对多

多对多

创建课程表

drop table if exists test_user;

create table test_user(

id int,

name varchar(20),

sex varchar(1),

check(sex = ‘男’ or sex = ‘女’)

);

创建学生课程中间表,考试成绩表

drop table if exists score(

id int primary key auto_increment,

score decimal(3,1),

student_id,

course_id,

foreign key (student_id) references student(id),

foreign key (course_id) references course(id)

);

三,新增

========

插入查询结果

语法:

insert into table_name column select…

案例:创建一张用户表,设计有name姓名、email邮箱、sex性别、mobile手机号字段。需要把已有的 学生数据复制进来,可以复制的字段为name、qq_mail

– 创建用户表

DROP TABLE IF EXISTS test_user;

CREATE TABLE test_user (

id INT primary key auto_increment,

name VARCHAR(20) comment ‘姓名’,

age INT comment ‘年龄’,

email VARCHAR(20) comment ‘邮箱’,

sex varchar(1) comment ‘性别’,

mobile varchar(20) comment ‘手机号’

);

–把学生表中的所有数据复制到用户表

insert into test_user(name,email) select name,qq_mail from student;

四、查询

========

4.1聚合查询


4.1.1、聚合函数

常见的统计总数、计算平局值等操作,可以使用聚合函数来实现,常见的聚合函数有:

函数说明
COUNT([DISTINCT] expr)返回查询到的数据的 数量
SUM([DISTINCT] expr)返回查询到的数据的 总和,不是数字没有意义
AVG([DISTINCT] expr)返回查询到的数据的 平均值,不是数字没有意义
MAX([DISTINCT] expr)返回查询到的数据的 最大值,不是数字没有意义
MIN([DISTINCT] expr)返回查询到的数据的 最小值,不是数字没有意义

COUNT

– 统计班级共有多少同学

select count(*) from student;

select count(0) from student;

– 统计班级收集的 qq_mail 有多少个,qq_mail 为 NULL 的数据不会计入结果

select count(qq_mail) from student;

SUM

– 统计数学成绩总分

select sum(math) from exam_result;

– 不及格 < 60 的总分,没有结果,返回 NULL

select sum(math) from exam_result where math < 60;

AVG

– 统计平均总分

select avg(chinese + math + english) 平均总分 from exam_result;

MAX

– 返回英语最高分

select max(english) from exam_result;

MIN

– 返回 > 70 分以上的数学最低分

select min(math) from exam_result where math > 70;

4.1.2、GROUP BY子句

SELECT 中使用 GROUP BY 子句可以对指定列进行分组查询。需要满足:使用 GROUP BY 进行分组查 询时,SELECT 指定的字段必须是“分组依据字段”,其他字段若想出现在SELECT 中则必须包含在聚合函 数中。

select column1, sum(column2), … from table group by column1,column3;

准备测试表及数据:职员表,有id(主键)、name(姓名)、role(角色)、salary(薪水)

create table emp(

id int primary key auto_increment,

name varchar(20) not null,

role varchar(20) not null,

salary numeric(11,2)

);

insert into emp(name, role, salary) values

(‘李白’,‘服务员’, 1000.20),

(‘杜甫’,‘游戏陪玩’, 2000.99),

(‘孙悟空’,‘游戏角色’, 999.11),

(‘猪无能’,‘游戏角色’, 333.5),

(‘沙和尚’,‘游戏角色’, 700.33),

(‘隔壁老王’,‘董事长’, 12000.66);

查询每个角色的最高工资、最低工资和平均工资

select role,max(salary),min(salary),avg(salary) from emp group by role;

4.1.3、HAVING

GROUP BY 子句进行分组以后,需要对分组结果再进行条件过滤时,不能使用 WHERE 语句,而需要用 HAVING

显示平均工资低于1500的角色和它的平均工资

select role,max(salary),min(salary),avg(salary) from emp group by role having avg(salary) < 1500;

4.2、联合查询


实际开发中往往数据来自不同的表,所以需要多表联合查询。多表查询是对多张表的数据取笛卡尔积:

测试示例:

insert into classes(name, desc) values

(‘计算机系2019级1班’, ‘学习了计算机原理、C和Java语言、数据结构和算法’),

(‘中文系2019级3班’,‘学习了中国传统文学’),

(‘自动化2019级5班’,‘学习了机械自动化’);

create table classes(

name varchar(20),

desc varchar(200)

);

create table student(

id int,

sn varchar(6),

name varchar(20),

qq_mail varchar(20),

classes_id int

);

insert into student(id,sn, name, qq_mail, classes_id) values

(1,‘09982’,‘黑旋风李逵’,‘xuanfeng@qq.com’,1),

(2,‘00835’,‘菩提老祖’,null,1),

(3,‘00391’,‘白素贞’,null,1),

(4,‘00031’,‘许仙’,‘xuxian@qq.com’,1),

(5,‘00054’,‘不想毕业’,null,1),

(6,‘51234’,‘好好说话’,‘say@qq.com’,2),

(7,‘83223’,‘tellme’,null,2),

(8,‘09527’,‘老外学中文’,‘foreigner@qq.com’,2);

create table course(

id int primary key auto_increment,

name varchar(20)

);

insert into course(id,name) values

(1,‘Java’),(2,‘中国传统文化’),(3,‘计算机原理’),(4,‘语文’),(5,‘高阶数学’),(6,‘英文’);

create table score(

score decimal(3,1),

student_id int,

course_id int

);

insert into score(score, student_id, course_id) values

– 黑旋风李逵

(70.5, 1, 1),(98.5, 1, 3),(33, 1, 5),(98, 1, 6),

– 菩提老祖

(60, 2, 1),(59.5, 2, 5),

– 白素贞

(33, 3, 1),(68, 3, 3),(99, 3, 5),

– 许仙

(67, 4, 1),(23, 4, 3),(56, 4, 5),(72, 4, 6),

– 不想毕业

(81, 5, 1),(37, 5, 5),

– 好好说话

(56, 6, 2),(43, 6, 4),(79, 6, 6),

– tellme

(80, 7, 2),(92, 7, 6);

4.2.1、内连接

语法:

select 字段 from 表1 别名1 [inner] join 表2 别名2 on 连接条件 and 其他条件;

select 字段 from 表1 别名1,表2 别名2 where 连接条件 and 其他条件;

案例:

查询“许仙”同学的 成绩

select sco.score from student stu inner join score sco on stu.id=sco.student_id

and stu.name=‘许仙’;

– 或者

select sco.score from student stu, score sco where stu.id=sco.student_id and

stu.name=‘许仙’;

初学者建议一步一步来

select student.id,student.name ,score.student_id,score.score from student,score;

select student.id,student.name ,score.student_id,score.score from

student,score where student.id = score.student_id;

select student.id,student.name ,score.student_id,score.score from

student,score where student.id = score.student_id and student.name = ‘许仙’;

(2)查询所有同学的总成绩,及同学的个人信息:

SELECT

stu.sn,

stu.NAME,

stu.qq_mail,

sum( sco.score )

FROM

student stu

JOIN score sco ON stu.id = sco.student_id

GROUP BY

sco.student_id;

select student.id ,student.name,score.student_id,score.score from student,score;

select student.id ,student.name,score.student_id,score.score from student,score

where student.id = score.student_id;

select student.id ,student.name,score.student_id,score.score from student,score

where student.id = score.student_id group by student.id;

select student.id ,student.name,sum(score.score) from student,score

where student.id = score.student_id group by student.id;

总结:绘上一张Kakfa架构思维大纲脑图(xmind)

image

其实关于Kafka,能问的问题实在是太多了,扒了几天,最终筛选出44问:基础篇17问、进阶篇15问、高级篇12问,个个直戳痛点,不知道如果你不着急看答案,又能答出几个呢?

若是对Kafka的知识还回忆不起来,不妨先看我手绘的知识总结脑图(xmind不能上传,文章里用的是图片版)进行整体架构的梳理

梳理了知识,刷完了面试,如若你还想进一步的深入学习解读kafka以及源码,那么接下来的这份《手写“kafka”》将会是个不错的选择。

  • Kafka入门

  • 为什么选择Kafka

  • Kafka的安装、管理和配置

  • Kafka的集群

  • 第一个Kafka程序

  • Kafka的生产者

  • Kafka的消费者

  • 深入理解Kafka

  • 可靠的数据传递

  • Spring和Kafka的整合

  • SpringBoot和Kafka的整合

  • Kafka实战之削峰填谷

  • 数据管道和流式处理(了解即可)

image

image

color_FFFFFF,t_70,g_se,x_16)

select student.id ,student.name,sum(score.score) from student,score

where student.id = score.student_id group by student.id;

总结:绘上一张Kakfa架构思维大纲脑图(xmind)

[外链图片转存中…(img-q3rUP8r4-1714427432629)]

其实关于Kafka,能问的问题实在是太多了,扒了几天,最终筛选出44问:基础篇17问、进阶篇15问、高级篇12问,个个直戳痛点,不知道如果你不着急看答案,又能答出几个呢?

若是对Kafka的知识还回忆不起来,不妨先看我手绘的知识总结脑图(xmind不能上传,文章里用的是图片版)进行整体架构的梳理

梳理了知识,刷完了面试,如若你还想进一步的深入学习解读kafka以及源码,那么接下来的这份《手写“kafka”》将会是个不错的选择。

  • Kafka入门

  • 为什么选择Kafka

  • Kafka的安装、管理和配置

  • Kafka的集群

  • 第一个Kafka程序

  • Kafka的生产者

  • Kafka的消费者

  • 深入理解Kafka

  • 可靠的数据传递

  • Spring和Kafka的整合

  • SpringBoot和Kafka的整合

  • Kafka实战之削峰填谷

  • 数据管道和流式处理(了解即可)

[外链图片转存中…(img-7JF0VMXz-1714427432630)]

[外链图片转存中…(img-v0THqlab-1714427432630)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

  • 21
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值