数据库按照特定的数据结构,来进行数据的组织,存储,管理和查询,数据库软件。可以长期存储,有安全限制,数据恢复,并且数据存储可拓展
数据库分类:
网状结构数据库
层次结构数据库
关系结构数据库【重点】
Oracle,MySQL,DB2,SQL Server
通过表结构方式来进行数据的存储操作。--> XLS表格
非关系型数据库
MongDB Redis
使用哈希表结构方式,Key-Value数据存储方式
ES
ElastecSearch
3. 数据库管理系统
数据库管理系统
DataBase Management System
DBMS
操作和管理数据大型软件,用于管理,建立,使用和查询数据。
Oracle
贼牛皮,安全性,稳定性,数据存储复杂程度....可以完全符合工业要求
贵
DB2
IBM公司,不是很多见
SQL Server
MS公司推出的关系型数据库
SQLite
轻量级数据库,源码1W行C语言,手机通讯录
4. MySQL
4.1 MySQL历史简介
MySQL数据库默认编码集 Latin1 ==> 西欧
瑞典的一个公司开发的!! MySQL AB公司
MySQL支持插件!!!
社区
InnoDB跨时代的存储引擎
MySQL性能是非常不错的!!!
美国小鹰号航空母舰数据存储都是使用MySQL
MySQL 隶属于Oracle公司,免费提供使用的数据库软件。目前流行的版本是 MySQL 5.5 5.6 5.7,最新版本是8.0
实际开发中我们要考虑稳定性,兼容性,普适性
5. SQL
5.1 SQL语句概述
SQL是数据库通用查询数据,不管是MySQL,Oracle,SQL Server都是支持标准SQL语句。不同数据库都会有自己特定的一些SQL语言。
SQL简单来分类
C Create
R Read
U Update
D Delete
5.2 MySQL基本操作
cmd > mysql -hlocalhost -uroot -p123456
# mysql指目前需要操作连接的数据库
# -hlocalhost -h host端口,当前数据库所处的服务器ip地址,域名或者主机名
# localhost 表示本地,如果是本地连接可以省略
# -uroot -u user用户 root表示用户
# -p -password 密码
# 推荐方式
cmd > mysql -uroot -p
Enter password: ******
-- 展示当前数据库中所有的数据表
mysql > show databases;
-- 创建数据库
-- CREATE DATABASE DBName;
mysql > create database nzgp2001;
-- 创建数据库过程修改编码集
mysql > create database nzgp2002 character set latin1;
mysql > create database nzgp2003 character set gbk;
-- 查看创建数据库的详细内容
mysql > show create database nzgp2001;
-- 修改数据库的编码集
mysql > alter database nzgp2002 character set gbk;
-- 删除对应数据库
mysql > drop database nzgp2002;
-- 选择使用数据库
mysql > use nzgp2001;
-- 让MySQL告知当前使用的数据库是哪一个
# 让MySQL告知当前使用的数据库是哪一个
mysql > select database();
6. MySQL开发工具使用
Navivcat for MySQL 非常不错
SQLyog 小海豚
MySQL WorkBench 免费,MySQL官网提供
IDEA Java IDE提供了数据库操作支持
7. 查询语句【重点】DQL
7.1 基本格式
select 查询内容 from 从哪里查;
-- 查询内容 字段名 列明
-- 从哪里查 表名
7.1.1 基本查询,字段表名
-- * 所有字段全部展示
-- 不建议,如果采用select * 方式整个数据库数据的时间非常长,浪费资源
select * from t_employees;
-- 查询指定的字段
select EMPLOYEE_ID, FIRST_NAME, LAST_NAME from t_employees;
-- 查询结果中存在相同内容,第二个数据不要
select distinct MANAGER_ID
from t_employees;
-- 不去重
select MANAGER_ID
from t_employees;
7.1.4 字段别名
-- 字段名 as '别名'
select EMPLOYEE_ID as 'ID', FIRST_NAME as '名', LAST_NAME as '姓', SALARY * 12 as '年薪'
from t_employees;
7.2 排序查询 order by
规则 效果 asc 指定条件升序 desc 指定条件降序
-- 基本格式
select fieldName from tbName order by fieldName asc/desc;
-- 在查询结果展示中,按照执行的字段作为标记完成升序和降序
7.2.1 单一条件排序
select EMPLOYEE_ID, FIRST_NAME, SALARY -- 查询展示的字段有哪些
from t_employees -- 从哪里查询
order by SALARY asc ; -- order by 指定字段要求排序 升序
select EMPLOYEE_ID, FIRST_NAME, SALARY -- 查询展示的字段有哪些
from t_employees -- 从哪里查询
order by SALARY desc ; -- order by 指定字段要求排序 降序
7.2.2 多字段条件排序
-- 第一条件是工资降序,当第一条件出现一致情况下,使用第二条件进行二次排序
select EMPLOYEE_ID, FIRST_NAME, SALARY -- 查询展示的字段有哪些
from t_employees -- 从哪里查询
order by SALARY desc, EMPLOYEE_ID desc; -- order by 指定字段要求排序 工资降序 ID降序
7.3 条件查询 where
7.3.1 基本格式
select fieldName from tbName where condition;
-- 从指定数据表中,按照where之后指定条件,查询对应的字段数据
-- where条件是一个是一个boolean类型结果
-- 查询在员工表内容,工资大于10000 对应的id号,名,和工资
select EMPLOYEE_ID, FIRST_NAME, SALARY
from t_employees
where SALARY > 10000;
-- 查询在员工表内容,工资大于10000 对应的id号,名,和工资
select EMPLOYEE_ID, FIRST_NAME, SALARY
from t_employees
where SALARY >= 10000;
-- 查询在员工表内容,部门ID不等于80 对应的id号,名,工资和部门ID
select EMPLOYEE_ID, FIRST_NAME, SALARY, DEPARTMENT_ID
from t_employees
where DEPARTMENT_ID <> 80;
-- 查询在员工表内容,部门ID不等于80 对应的id号,名,工资和部门ID
select EMPLOYEE_ID, FIRST_NAME, SALARY, DEPARTMENT_ID
from t_employees
where DEPARTMENT_ID != 80;
7.3.4 逻辑判断(and, or, not)
-- 查询在员工表内容,要求工资大于10000并且部门编号为80 对应的ID号,名,工资和部门ID
select EMPLOYEE_ID, FIRST_NAME, SALARY, DEPARTMENT_ID
from t_employees
where SALARY > 10000 and DEPARTMENT_ID = 80;
-- 查询在员工表内容,要求工资小于2500或者部门编号为90 对应的ID号,名,工资和部门ID
select EMPLOYEE_ID, FIRST_NAME, SALARY, DEPARTMENT_ID
from t_employees
where SALARY < 2500 or DEPARTMENT_ID = 90;
7.3.5 区间 between and
-- 要求between min and max 在min <==> max范围以内,而且要求小值之前,大值不然会报
-- 错,这里区间范围是包含指定的边界
-- 区间范围
select EMPLOYEE_ID, FIRST_NAME, SALARY
from t_employees
where SALARY between 8000 and 10000;
7.3.6 NULL值判断
-- is null 指定当前的字段是null
-- is not null 指定当前字段不是null
-- 找出所有提成为null的数据
select FIRST_NAME, COMMISSION_PCT
from t_employees
where COMMISSION_PCT is null;
-- 找出所有提成不是null的数据
select FIRST_NAME, COMMISSION_PCT
from t_employees
where COMMISSION_PCT is not null;
7.3.7 枚举查询 in
-- 查询部门编号为60, 70, 90员工名字和对应部门编号
-- in查询效率较低,推荐使用多条件拼接完成查询操作
select FIRST_NAME, DEPARTMENT_ID
from t_employees
where DEPARTMENT_ID in(70, 60, 90);
7.3.8 模糊查询 like
-- LIKE
-- _ 匹配一个字符
-- % 匹配任意长度字符
-- 查询FIRST_NAME,要求FIRST_NAME字段D字母开头后面有4个字符
select FIRST_NAME
from t_employees
where FIRST_NAME like 'D____';
-- -- 查询FIRST_NAME,要求FIRST_NAME字段带有D字母就可以,而且不区分大小写
select FIRST_NAME
from t_employees
where FIRST_NAME like '%D%';
7.3.9 分支结构查询
case
when condition1 then ret1
when condition2 then ret2
when condition3 then ret3
end
-- 从case开始,到end结束。满足条件对应一个结果,类似于Java中的switch case
-- 查询姓名,工资已经对应工资划分的等级LEVEL
select FIRST_NAME, SALARY,
case
when SALARY >= 10000 then 'A'
when SALARY >= 8000 and SALARY < 10000 then 'B'
when SALARY >= 6000 and SALARY < 8000 then 'C'
when SALARY >= 4000 and SALARY < 6000 then 'D'
else 'E'
end as 'LEVEL'
from t_employees;
-- 工资总和
select sum(SALARY)
from t_employees;
-- 工资平均数
select avg(SALARY)
from t_employees;
-- 工资最大值
select max(SALARY)
from t_employees;
-- 工资最小值
select min(SALARY)
from t_employees;
-- 当前有多少员工计数
select count(*)
from t_employees;
-- 当前有多少员工计数
-- count(1) 蜜汁比count(*) 快一点
select count(1)
from t_employees;
-- 统计有多少人有绩效
select count(COMMISSION_PCT)
from t_employees;
7.7 分组查询
select fieldName
from tbName
where condition_
group by 分组要求;
-- group by是一个分组关键字
-- 查询各部门人数是多少
-- 1. 需要按照department_id进行分组
-- 2. 计数需要使用count, 根据用户的employee_id进行计数操作
select department_id, count(employee_id)
from t_employees
group by department_id;
-- 查询各部门的平均工资
-- 1. 需要按照department_id进行分组
-- 2. 平均工资使用avg方法计算
select department_id, avg(salary)
from t_employees
group by department_id;
-- 查询各部门,各岗位的人数
-- 1. 需要按照department_id进行分组
-- 2. 需要按照岗位job_id进行分组
-- 3. 记录人数,count(employee_id)
select department_id, job_id, count(employee_id)
from t_employees
group by department_id, job_id;
-- [42000][1055] Expression #1 of SELECT list is not in GROUP BY
-- clause and contains nonaggregated column 'company.t_employees.department_id'
-- which is not functionally dependent on columns in GROUP BY clause;
-- this is incompatible with sql_mode=only_full_group_by
-- 如果使用group by要求分组字段一定是查询要求字段,这里需要根据查询结果进行分组
select department_id
from t_employees
group by job_id;
7.8 分组过滤查询
select fieldName
from tbName
where condition_
group by 分组要求
having 过滤规则;
-- having是在 group by 之后的条件过滤
-- 查询指定100,50,30,80最高工资
-- 1. 需要按照department_id进行分组
-- 2. 最高工资
-- 3. 限制manager_id = 100
-- 4. 限制department_id号为100,50,30,80
select department_id, max(salary)
from t_employees
where manager_id = 100
group by department_id
having department_id in (100, 50, 30, 80);
select fieldName
from tbName
where condition_
group by 分组
having 分组过滤
order by 排序 [asc/desc]
limit offset, count;
-- from 数据来源,从那张表中查询数据
-- where 查询数据的条件
-- group by 分组
-- having 分组之后条件约束
-- select 查询指定的字段
-- order by 排序要求
-- limit 限制结果行数
7.11 子查询[重点,难点]
7.11.1 基本格式
select fieldName
from tbName
where (子查询结果);
7.11.2 子查询结果作为条件判断约束
-- 查询工资高于Jack的员工id和姓名
-- 1. 找出Jack的工资
-- 2. 得到Jack工资,作为条件查询对应的员工信息
select salary
from t_employees
where first_name = 'Jack';
select employee_id, first_name
from t_employees
where salary > 8400;
-- 整合为子查询
-- 条件判断
select employee_id, first_name
from t_employees
where salary > (select salary
from t_employees
where first_name = 'Jack');
7.11.3 子查询结果作为枚举限制 in
-- 查询和Jack同部门的员工信息
-- 1. 找出Jack的部门编号
select department_id
from t_employees
where first_name = 'Jack';
-- 2. 根据Jack的部门编号,使用in枚举查询,限制条件
select employee_id, first_name
from t_employees
where department_id in (80);
-- 整合为子查询
select employee_id, first_name
from t_employees
where department_id in (select department_id
from t_employees
where first_name = 'Jack');
7.11.4 子查询结果作为一张表,从表内查询指定数据
-- 查询员工表中工资前五名的员工信息
-- 1. 找到员工的id,first_name,工资降序
select employee_id, first_name
from t_employees
order by salary desc;
select employee_id, first_name
from (select employee_id, first_name
from t_employees
order by salary desc) as temp
limit 5;
7.12 合并查询[仅了解]
union
-- 合并,要求查询要求的字段个数一致
-- 去重
select employee_id, first_name
from t_employees
union
select job_id, job_title
from t_jobs;
-- 合并,要求查询要求的字段个数一致
-- 不去重
select department_id, first_name
from t_employees
union all
select manager_id, department_name
from t_departments;
-- 查询所有员工对应的ID号,名字,部门名称,和国家对应名字
select te.employee_id, te.first_name, td.department_name, tc.country_name
from t_employees te
inner join t_departments td on te.department_id = td.department_id
inner join t_locations tl on td.location_id = tl.location_id
inner join t_countries tc on tl.country_id = tc.country_id;
7.13.5 内连接查询 inner join on 五张表
-- 查询所有员工对应的ID号,名字,工作职称,部门名称,和国家对应名字
select te.employee_id, te.first_name, tj.job_title, td.department_name, tc.country_name
from t_employees te
inner join t_jobs tj on te.job_id = tj.job_id
inner join t_departments td on te.department_id = td.department_id
inner join t_locations tl on td.location_id = tl.location_id
inner join t_countries tc on tl.country_id = tc.country_id;
7.13.6 左外连接 left join on
-- 左外连接 左表是主表,要求左表完整显示,右表匹配左表数据,如果右表没有数据匹配,显示null
-- 查询所有的员工信息ID,first_ame,已经对应的部门名字
select te.employee_id, te.first_name, td.department_name
from t_employees te
left join t_departments td on te.department_id = td.department_id;
7.13.7 右外连接 right join on
-- 右外连接查询,右表是主表,要求右表完整展示,左表匹配右表数据,如果左表没有数据匹配,显示null
-- 查询所有部门对应员工信息,员工信息没有显示null
select td.department_name,te.employee_id, te.first_name
from t_employees te
right join t_departments td on te.department_id = td.department_id;
8. DML语句
8.1 增加 insert
create table person(
id int,
name varchar(30),
salary float(8, 2),
age int
);
-- 规矩插入
insert into person(id, name, salary, age) VALUE (1, '骚磊', 100.5, 16);
-- 省略所有的字段名,按照字段顺序添加
insert into person value (2, '茂林', 20.5, 50);
-- 指定字段数据插入
insert into person(name, age) value ('骚杰', 66);
-- 禁止字段数据类型和插入数据类型不一致!!!
insert into person(name, age) value ('66', '23333333');
insert into person(name, age) value ('66', '你在整一个试试???');
8.2 修改 update 【慎用】
-- Unsafe query: 'Update' statement without 'where' updates all table rows at once
-- 没有任何约束的情况下,当前指定数据表中的所有数据行全部执行修改操作
-- 一定要带有where条件约束
update person set id = 10;
-- OK
update person set id = 10 where name = '骚磊';
-- 可以同时修改多个数据,不同的字段赋值操作使用 逗号隔开
update person set id = 20, age = 10, salary = 200000.01 where name = '骚磊';
8.3 删除 delete【慎用】
-- Unsafe query: 'Delete' statement without 'where' clears all data in the table
delete from person;
-- 根据条件约束删除是允许的
delete from person where name = '66';
-- 创建数据表的过程中,指定字段可以带有默认值,如果用户没有指定数据的情况下,当前
-- 字段会采用默认值方式来进行数据赋值操作。
-- default
create table person1
(
id int,
name varchar(50),
country varchar(50) default 'PRC' -- 默认值字段
);
desc person1;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| country | varchar(50) | YES | | PRC | |
+---------+-------------+------+-----+---------+-------+
-- 不给予带有默认值字段对应的数据,会采用默认值方式赋值当前子弹
insert into person1(id, name) value (1, '骚磊');
-- 给予当前带有默认值字段数据赋值操作,会采用给予的数据赋值当前字段
insert into person1(id, name, country) value (2, '航海中路彭于晏', '中华人民共和国');
10.2 非空 not null
-- not null 非空,要求当前字段必须有对应的数据,如果没有赋值报错
-- NN
-- 实际使用中必要字段!!!
create table person2
(
id int,
name varchar(50) not null,
country varchar(50) default 'PRC' -- 默认值字段
);
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | NO | | NULL | |
| country | varchar(50) | YES | | PRC | |
+---------+-------------+------+-----+---------+-------+
insert into person2(id, name) VALUE (1, '46号技师');
-- ERROR Field 'name' doesn't have a default value
insert into person2(id) VALUE (2);
10.3 唯一 unique
-- 字段使用unique约束,当前字段中保存的数据在当前表内不可以重复
create table person3
(
id int unique,
name varchar(50) not null,
country varchar(50) default 'PRC' -- 默认值字段
);
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | YES | UNI | NULL | |
| name | varchar(50) | NO | | NULL | |
| country | varchar(50) | YES | | PRC | |
+---------+-------------+------+-----+---------+-------+
insert into person3(id, name) value (1, '哆啦A磊');
-- ERROR Duplicate entry '1' for key 'id'
-- 对应当前ID值已经存在,不能再次插入相同id数据
insert into person3(id, name) value (1, '老骚');
-- id使用unique限制唯一,但是null不作为唯一判断范围以内
insert into person3(name) value ('老骚');
insert into person3(name) value ('超超');
insert into person3(id, name) value (null, '贱贱的我就长大了');
insert into person3(id, name, country) value (null, '贱贱的我就长大了',null);
10.4 主键 primary key
-- 主要要求唯一,非空!!!
-- primary key 主键
-- 主键一般用于在开发中涉及到数据的唯一性参照物,但是不能使用带有业务逻辑要求数据作为
-- 主键,例如 性别 年龄 工资....
create table person4
(
id int primary key,
name varchar(50) not null,
country varchar(50) default 'PRC' -- 默认值字段
);
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(50) | NO | | NULL | |
| country | varchar(50) | YES | | PRC | |
+---------+-------------+------+-----+---------+-------+
insert into person4(id, name) value (1, '骚磊');
-- ERROR Duplicate entry '1' for key 'PRIMARY'
-- 对应使用primary key修饰的主键id已存在。
insert into person4(id, name) value (1, '骚磊');
-- ERROR Field 'id' doesn't have a default value
insert into person4(name) value ('骚磊');
-- ERROR Column 'id' cannot be null
insert into person4(id, name) value (null, '骚磊');
10.5 自增长 auto_increment
-- 自增长修饰字段会在数据添加的过程中自动赋值叠加操作
-- auto_increment修饰的字段必须是一个Key
-- AI ==> auto_increment
create table person5
(
id int primary key auto_increment,
name varchar(50) not null,
country varchar(50) default 'PRC' -- 默认值字段
);
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(50) | NO | | NULL | |
| country | varchar(50) | YES | | PRC | |
+---------+-------------+------+-----+---------+----------------+
-- 自增长字段会从1开始
insert into person5(name) value ('郭德纲'); -- id = 1
insert into person5(name) value ('于谦'); -- id = 2
-- 自增长可以指定数值,而且存在影响自增长计数情况
insert into person5(id, name) value (5, '高峰'); -- id = 5
insert into person5(name) value ('栾云平'); -- id = 6
-- 自增长可以指定数值,而且存在影响自增长计数情况
insert into person5(id, name) value (8,'岳云鹏'); -- id = 8
insert into person5(name) value ('孙越'); -- id = 9
-- 指定id为7,但是当前id数据小于自增长,不影响自增长结果 数据保存排序
insert into person5(id, name) value (7, '郭麒麟'); -- id = 7
insert into person5(name) value ('阎鹤祥'); -- id = 10;
-- 删除数据
delete from person5 where id = 7;
delete from person5 where id = 10;
-- 被删除的id可以指定使用
insert into person5(id, name) value (7, '郭麒麟'); -- id = 7
-- delete删除不会印象自增长计数
insert into person5(name) value ('阎鹤祥'); -- id = 11
delete from person5 where id = 11;
-- 清空数据表,同时会重置自增长
truncate person5;
-- 数据库彻底关闭,会影响到自增长保存