數據庫按照特定的數據結構,來進行數據的組織,存儲,管理和查詢,數據庫軟件。可以長期存儲,有安全限制,數據恢復,並且數據存儲可拓展
數據庫分類:
網狀結構數據庫
層次結構數據庫
關係結構數據庫【重點】
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
實際開發中我們要考慮穩定性,兼容性,普適性
4.2 MySQL獲取和安裝
【詳見】 Windows下安裝MySQL5.7
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.3 子查詢結果作爲一張表,從表內查詢指定數據
-- 查詢員工表中工資前五名的員工信息
-- 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.11 合併查詢[僅瞭解]
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.12.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.12.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.12.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;
-- 數據庫徹底關閉,會影響到自增長保存