2

1.数据库

1.1分类(关系型和非关系型)

常见的数据库分为:
关系型数据库, Oracle、MySQL、SQLServer、Access
非关系型数据库, MongoDB、Redis、ElasticSearch、Hive、Solr、HBase

SQL规范非常成熟,分为下面的部分:
数据查询语言DQL:SELECT、FROM、WHERE、ORDER BY、GROUP BY
数据操纵语言DML:INSERT、UPDATE、DELETE
数据定义语言DDL:CREATE DATABASE、CREATE TABLE、CREATE INDEX
数据控制语言DCL:GRANT、ROOLBACK、COMMIT
TCL: 事务控制语言, 负责处理事务相关

1.2 CRUD的操作【创建(Create)、更新(Update)、读取(Retrieve)和删除(Delete)】

基础函数
1、大小写;lower、upper
2、长度:length
3、截串:substr:
4、拼串:concat
5、首字母大写:
6、替换:replace
7、四舍五入:round 、ceil、floor
8、唯一值:uuid

日期函数
1、现在时间:now(年月日时分秒)、current_time(时分秒)、current_date(年月日)
2、每月最大日期:last_day
3、日期转字符串:date_format
4、字符串转日期:str_to_date

条件查询
1、不重复:distinct
2、附带条件:where
3、通配符:like(%)
4、关系符:and 、or
5、空符:null、not null
6、空符替换:nvl
7、范围:between x and y
8、连接符:union
9、返回行:limit

子查询
1、单行子查询 =
2、多行子查询:in

统计查询
1、有关次数:count
2、排序:order by asc(顺序)、desc(降序)
3、薪资查询、入职查询、年薪查询

聚合
1、记录次数:count
2、最大最小值:max(最大值)、min(最小值)
3、平均值和求和值:avg(平均值)、sum(求和值)

分组
1、group
2、having

多表联查
1、左连接:left join
2、右连接:right join
3、内连接:inner

1.数据库相关SQL

1.查询所有数据库 show databases;
2.创建数据库 create database db1 character set utf8/gbk;
3.查询详情 show create database db1;
4.删除数据库 drop database db1;
5.使用数据库 use db1;

2.表相关SQL

1.查询所有 show tables;
2.创建create table t1(name varchar(10),age int) charset=utf8/gbk;
3.查询详情 show create table t1;
4.查询表字段 desc t1;
5.删除表 drop table t1;
6.修改表名 rename table t1 to t2;
7.添加表字段 alter table t1 add age int first/after xxx;
8.删除表字段 alter table t1 drop age;
9.修改表字段 alter table t1 change 原名 新名 新类型;

3.数据相关

1.插入数据 insert into t1(xxx,xxx) values(xxx,xxx),(xxx,xxx),(xxx,xxx);
2.查询数据 select 字段信息 from 表名 where 条件;
3.修改数据 update 表名 set xxx=xxxx where 条件;
4.删除数据 delete from 表名 where 条件;

1.去重 distinct

去掉重复的数据
1.查询员工表中出现的部门编号有哪几种
select distinct deptno from emp;
2.查询员工表中有哪几种不同的工作
select distinct job from emp;

2.is null和is not null

is null和is not null
当判断某个字段的值为null的时候用 is null 不为null则是is not null
1.查询有上级领导的员工姓名和领导编号
select ename,mgr from emp where mgr is not null;
2.查询没有上级领导的员工姓名
select ename from emp where mgr is null;

3.比较运算符 > < >= <= = !=和<>

1.查询工资小于等于3000的员工信息
select * from emp where sal<=3000;
2.查询不是程序员的员工姓名和工作(两种写法)
select ename,job from emp where job!='程序员';
select ename,job from emp where job<>'程序员';

4.and 和 or

and 类似java 中&&, 查询多个条件同时满足时使用and
or 类似java中的||,查询多个条件只满足一个就行的时候使用or
1.查询1号部门工资低于3000的员工信息
select * from emp where deptno=1 and sal<3000;
2.查询3号部门或工作是程序员的员工信息
select * from emp where deptno=3 or job='程序员';
3.查询有上级领导的员工中工资小于2000的员工姓名,工资和领导编号
select ename,sal,mgr from emp where mgr is not null and sal<2000;

5.in关键字

当查询某个字段的值为多个值得时候使用
1.查询工资为3000,5000,1500的员工姓名和工资
select ename,sal from emp where sal=3000 or sal=5000 or sal=1500;
select ename,sal from emp where sal in(3000,5000,1500);
2.查询工资不是3000和5000的员工信息 
select * from emp where sal not in(3000,5000);

6.between x and y 在两者之间 包含x和y

1.查询工资在2000到3000之间的员工信息
select * from emp where sal>=2000 and sal<=3000;
select * from emp where sal between 2000 and 3000;

7.模糊查询 like

% : 代表0或多个未知字符
_ : 代表1个未知字符
举例:
o以x开头 x%
o以x结尾 %x
o第二个字符是x _x%
o包含x %x%
o第一个是x倒数第三个是y x%y__
1.查询名字中包含"悟"字的员工姓名
select ename from emp where ename like "%悟%";
2.查询工作中第二个字是"售"的员工信息
select * from emp where job like '_售%';
3.查询名字以精结尾的员工姓名
select ename from emp where ename like '%精';
4.查询姓孙的员工信息
select * from emp where ename like '孙%';

8.排序 order by

格式: order by 字段名 asc (默认升序)/desc;
1.查询员工姓名和工资 ,按照工资升序排序
select ename,sal from emp order by sal;
2.查询员工姓名和工资 ,按照工资降序排序 select ename,sal from emp order by sal desc;
多字段排序, 在order by的后面写多个字段 通过逗号分隔即可
3.查询员工姓名,工资和部门编号 按照部门编号升序排序
select ename,sal,deptno from emp order by deptno,sal desc;
4.查询1号部门的员工信息 按照工资降序排序
select * from emp where deptno=1 order by sal desc;

9.分页查询limit

作用: 查询符合条件的某一部分数据
格式: limit x,y; x代表跳过的条数 y代表请求的条数
1.查询工资最高的前三名员工信息
select * from emp order by sal desc limit 0,3;
2.每页有4条数据,查询第二页的数据
select * from emp limit 4,4;
3.每页有3条数据查询第3页的数据
select * from emp limit 6,3;
4.查询工资最低的员工信息
select * from emp order by sal limit 0,1;

10.别名

可以给查询的字段名起一个其它名字
select ename as "员工姓名" from emp;
select ename "员工姓名" from emp;
select ename 员工姓名 from emp;

11.数值计算 + - * / %

查询数据时可以对查询的字段进行计算
1.查询每个员工的姓名,工资和年终奖(年终奖=5个月的工资)
select ename,sal,sal*5 年终奖 from emp;
2.查询一号部门的员工姓名,工资,和涨薪后工资(每人工资涨5块钱)
select ename,sal,sal+5 涨薪后 from emp where deptno=1;
3.修改2号部门的工资,每个人涨薪8块钱
update emp set sal=sal+8 where deptno=2;

12.聚合函数

聚合函数就是对查询的多条数据进行统计查询
统计方式: 1. 平均值 2. 最大值 3.最小值 4. 求和 5. 计数 
1.平均值 avg(字段名)
o查询2号部门的平均工资
select avg(sal) from emp where deptno=2;
2.最大值max(字段名)
o查询销售的最高工资
select max(sal) from emp where job='销售';
o查询1号部门有领导的员工的最高工资
select max(sal) from emp where deptno=1 and mgr is not null;
3.最小值min(字段名)
o查询有奖金的员工中的最低工资
select min(sal) from emp where comm>0;
4.求和sum(字段名)
o查询1号部门所有员工的工资总和 
select sum(sal) from emp where deptno=1;
o查询程序员的工资总和
select sum(sal) from emp where job='程序员';
5.计数count(字段名或*)
o查询1号部门的员工人数 
select count(*) from emp where deptno=1;

13.分组查询 group by

分组查询可以将某个字段相同值的划分为一组 ,以每组为单位进行统计查询(聚合函数)
1.查询每个部门的平均工资
select deptno,avg(sal) from emp group by deptno;
2.查询每个部门的工资总和
select deptno,sum(sal) from emp group by deptno;
3.查询每种工作的最高工资
select job,max(sal) from emp group by job;
4.查询每种工作的人数
select job,count(*) from emp group by job;
5.查询每个部门工资高于1500的员工人数 
select deptno,count(*) from emp where sal>1500 group by deptno;

14.各个关键字的顺序

select 查询字段信息 from 表名 where 条件 group by 分组字段名 order by 排序字段 limit 跳过条数,请求条数;

15.having

where 后面只能写普通字段的条件,不能写聚合函数条件
having后面写聚合函数条件
having关键字一定要和group by分组查询结合使用
having关键字写在group by的后面
1.查询每个部门的平均工资,查询工资高于2000的员工
select deptno,avg(sal) from emp where sal>2000 group by deptno;
2.查询每个部门的平均工资,只查询平均工资高于2000的信息
select deptno,avg(sal) from emp group by deptno having avg(sal)>2000;
select deptno,avg(sal) a from emp group by deptno having a>2000;
3.查询每个部门的工资总和,只查询有领导的员工,并且过滤掉工资总和低于5400的信息
select deptno,sum(sal) s from emp 
where mgr is not null group by deptno having s>=5400 ;
4.查询每个部门的平均工资,只查询工资在1000到3000之间的,并且过滤掉平均工资低于2000的部门 
select deptno,avg(sal) a from emp 
where sal between 1000 and 3000 group by deptno having a>=2000;

16.子查询(嵌套查询)

1.查询工资高于1号部门平均工资的员工信息
o先查询1号部门平均工资
select avg(sal) from emp where deptno=1;
o查询高于1号部门平均工资的员工信息
select * from emp where sal>(select avg(sal) from emp where deptno=1);
2.查询工资最高的员工信息
select * from emp where sal=(select max(sal) from emp);
3.查询工资高于2号部门最低工资的员工信息
select * from emp where sal>(select min(sal) from emp where deptno=2);
4.查询和孙悟空相同工作的其它员工信息
oselect job from emp where ename='孙悟空';
oselect * from emp where job=(select job from emp where ename='孙悟空') and ename!='孙悟空';
5.查询拿最低工资员工的同事们的信息(同事指同一部门的员工)
oselect min(sal) from emp;
oselect deptno from emp where sal=(select min(sal) from emp);
oselect * from emp where deptno=(select deptno from emp where sal=(select min(sal) from emp)) and sal!=(select min(sal) from emp);
6.查询白骨精的部门信息 (需要用到两张表)
select * from dept where deptno=(select deptno from emp where ename='白骨精');

17.关联关系

创建表时,表和表之间存在的业务关系
有哪几种关系?
1.一对一: 有AB两张表,A表中一条数据对应B表中的一条数据,同时B表中的一条也对应A表中的一条.
2.一对多: 有AB两张表,A表中一条数据对应B表中的多条数据,同时B表中的一条对应A表中的一条.
3.多对多:有AB两张表,A表中一条数据对应B表中的多条数据,同时B表中的一条也对应A表中的多条.
表和表之间如何建立关系?
通过一个单独的字段建立关系, 这个建立关系的字段称为外键, 外键的值通常指向另外一张表的主键

18.关联查询

同时查询多张表的查询方式称为关联查询
关联查询的查询方式有: 1. 等值连接 2. 内连接 3. 外连接

19.等值连接

格式: select * from A,B where 关联关系 and 其它条件
1.查询每个员工的姓名和对应的部门
select e.ename,d.dname
from emp e ,dept d
where e.deptno=d.deptno;
2.查询工资高于2000的员工姓名,工资和部门地点
select e.ename,e.sal,d.loc
from emp e , dept d 
where e.deptno=d.deptno and e.sal>2000;

20.内连接

等值连接和内连接查询到的数据是一样的,只不过格式不同
格式: select * from A join B on 关联关系 where 其它条件
1.查询每个员工的姓名和对应的部门
select e.ename,d.dname
from emp e join dept d
on e.deptno=d.deptno;
2.查询工资高于2000的员工姓名,工资和部门名称,部门地点
select e.ename,e.sal,d.loc
from emp e join dept d
on e.deptno=d.deptno
where e.sal>2000;

21.外连接

等值连接和内连接查询到的都是两张表的交集数据
外连接查询到的是一张表的全部和另外一张表的交集
格式: select * from A left/right join B on 关联关系 where 其它条件
1.查询所有的部门名和对应的员工名
select d.dname,e.ename
from emp e right join dept d
on e.deptno=d.deptno;
2.查询所有的员工姓名和对应的部门名还有地点.
先往员工表中插入一条数据
insert into emp(empno,ename)values(20,'灭霸');

2.jdbc

3.mybatis和mybatis-plus

4.数据库备份(冷、热)

4.1 冷备份

1.说明:

通过数据库工具,**定期将数据库文件进行转储**,保证数据的安全性. (一般2-3天)

2.缺点:

1.备份时由于突发的情况,可能导致备份失败.需要反复备份.
2.由于冷备份是定期备份.所以可能导致数据的丢失.

3.核心:

数据必须备份.备份的数据是恢复的最后有效的手段.

4.2 热备份

1.说明:

可以通过数据库机制,自动的实现数据的备份操作.

2.优点:

可以实现自动化的操作,并且是实时备份.

3.备份实现原理:
image.png

1.当数据库主机的数据发现变化时,会将修改的数据写入二进制日志文件中.
2.从库通过IO线程,读取主库的二进制日志文件,获取之后,将数据保存到中继(临时存储)日志中.
3.从库中开启Sql线程,之后读取中继日志中的数据,之后将数据同步到从库中.

5.数据库主从搭建、数据库读写分离/负载均衡实现

5.1 安装数据库(主从库)

https://blog.csdn.net/qq_1680...

5.2 数据库主从搭建

5.2.1 开启主库二进制文件

1.说明:

主库的二进制日志文件,默认条件下是关闭的,需要手动的开启.

2.命令:

vim /etc/my.cnf

3.主从二进制配置文件

编辑二进制日志文件:
主:server.id=1    log.bin=mysql.bin
从:server.id=1    log.bin=mysql.bin
重启数据库,检查二进制日志文件,是否可用.systemctl restart mariadb
重启之后,生成二进制文件

5.2.2 数据库主从搭建

1).检查主库的状态
image.png
2). 实现数据库主从挂载

/*我是130 我是从库*/
/*1.实现数据库主从挂载 host/port/user/password/二进制日志/pos*/
CHANGE MASTER TO MASTER_HOST="192.168.126.129",
MASTER_PORT=3306,
MASTER_USER="root",
MASTER_PASSWORD="root",
MASTER_LOG_FILE="mysql-bin.000001",
MASTER_LOG_POS=245;

/*2.启动数据库主从服务*/
START SLAVE;

/*3.检查数据库启动状态*/
SHOW SLAVE STATUS;

/*4.如果出现数据库问题  1.关闭主从服务, 2.检查报错状态  3.重新搭建服务*/
STOP SLAVE    
/*检查报错信息 根据报错修改记录*/
/*重新搭建主从关系*/

3).主从状态的校验
image.png

5.2.3 数据库主从测试

注意事项:
1.修改主库的数据,从库会跟着同步数据.
2.如果修改从库数据,则主从的关系将会终止.

5.3 数据库读写分离/负载均衡实现

说明:通过代理数据库可以实现数据库的读写分离/数据库负载均衡操作,进一步的提升了整体架构的能力.
image.png

6.数据库高可用

7.数据库的优化


早起的鸟儿
7 声望2 粉丝

« 上一篇
jt-day10
下一篇 »
Redis缓存学习