大家好,我是云计算磊哥,从业20年的IT老鸟。运维培训15年,总结了一套从入门到精通的全运维开发宝典手册。准备用300天时间写一套博文,手把手从安装软件讲起,从行业到产品,从过去到未来,从理论到操作,从视频到文档工具,一站式。从零基础入门到20k运维开发工程师岗位诸多就业问题。多方位全方面的给你讲清楚云计算这个行业该如何做。关注我。后续AI大模型开发课程更精彩。
开源数据库MySQL DBA运维实战
第三章
DML
1 目的
在MySQL管理软件中,DDL已经定义了数据库结构。
那么如何对其中的数据进行管理呢?
可以通过SQL语句中的DML语言来实现数据的操作,包括使用
INSERT 实现数据的 插入
DELETE 实现数据的 删除以及
UPDATE 实现数据的 更新。
2 插入数据INSERT
2.1 插入完整数据(顺序插入)
语法一:
INSERT INTO 表名(字段1,字段2,字段3...字段n) VALUES (值1,值2,值3...值n);
语法二:
INSERT INTO 表名 VALUES (值1,值2,值3...值n);
2.2 指定字段插入数据
语法:
INSERT INTO 表名(字段2,字段3...) VALUES (值2,值3...);
2.3 插入多条记录
语法:
INSERT INTO 表名 VALUES
(值1,值2,值3...值n),
(值1,值2,值3...值n),
(值1,值2,值3...值n);
2.4. 插入查询结果(略)
语法:
INSERT INTO 表1(字段1,字段2,字段3...字段n)
SELECT (字段1,字段2,字段3...字段n) FROM 表2
WHERE ...;
前提,字段数和字段类型相同。
3 更新数据UPDATE
语法:
UPDATE 表名 SET
字段1=值1,
字段2=值2,
WHERE CONDITION;
示例1:
mysql> create table t1(id int, name varchar(50));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into t1 values (1,'aa');
Query OK, 1 row affected (0.01 sec)
mysql> insert into t1 values (2,'bb');
Query OK, 1 row affected (0.00 sec)
mysql> update t1 set name='cc' where id=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from t1;
4 删除数据DELETE
语法:
DELETE FROM 表名
WHERE CONITION;
示例:
需求:删除id为2 的用户记录。
mysql> delete from t1 where id=2;
请思考不加where条件会如何。
DQL
1 目的
在MySQL管理软件中,可以通过SQL语句中的DQL语言来实现数据的
SELECT 查询操作
2 MySQL单表查询
2.1 分类
- 简单查询。
- 通过条件查询。
- 查询排序。
- 限制查询记录数。
- 使用集合函数查询。
- 分组查询。
- 使用正则表达式查询。
2.2 素材
mysql> CREATE TABLE company.employee5(
id int primary key AUTO_INCREMENT not null,
name varchar(30) not null,
sex enum('male','female') default 'male' not null,
hire_date date not null,
post varchar(50) not null,
job_description varchar(100),
salary double(15,2) not null,
office int,
dep_id int
);
mysql> insert into company.employee5(name,sex,hire_date,post,job_description,salary,office,dep_id) values
('jack','male','20180202','instructor','teach',5000,501,100),
('tom','male','20180203','instructor','teach',5500,501,100),
('robin','male','20180202','instructor','teach',8000,501,100),
('alice','female','20180202','instructor','teach',7200,501,100),
('tianyun','male','20180202','hr','hrcc',600,502,101),
('harry','male','20180202','hr',NULL,6000,502,101),
('emma','female','20180206','sale','salecc',20000,503,102),
('christine','female','20180205','sale','salecc',2200,503,102),
('zhuzhu','male','20180205','sale',NULL,2200,503,102),
('gougou','male','20180205','sale','',2200,503,102);
2.2 简单查询测试
查看所有信息
SELECT * FROM employee5;
避免重复DISTINCT
SELECT DISTINCT post FROM employee5;
2.2 单条件查询where
单条件查询where :查询hr部门的员工姓名
SELECT name,post
FROM employee5
WHERE post='hr';
多条件查询AND/OR: 查询hr部门的员工,并且工资大于1000
SELECT name,salary
FROM employee5
WHERE post='hr' AND salary>10000;
关键字BETWEEN AND 在什么之间:查一查薪资在5000到15000
SELECT name,salary FROM employee5
WHERE salary BETWEEN 5000 AND 15000;
关键字IN集合查询:工资可能是4000,也可能是5000,还有可能是9000,怎么查
SELECT name, salary FROM employee5
WHERE salary IN (4000,5000,6000,9000) ;
关键字IS NULL: 没有岗位描述的
SELECT name,job_description FROM employee5
WHERE job_description IS NULL;
关键字LIKE模糊查询:好像有个员工姓阿
SELECT * FROM employee5
WHERE name LIKE 'al%';
或
SELECT * FROM employee5
WHERE name LIKE 'al___';
2.3 查询排序order by
按单列排序:工资从低到高,怎么查(升序)
SELECT name, salary FROM employee5 ORDER BY salary ASC;
工资从高到低,怎么查
SELECT name, salary FROM employee5 ORDER BY salary DESC;
2.4 限制查询的记录数limit
SELECT * FROM employee5 ORDER BY salary DESC
LIMIT 5;
SELECT * FROM employee5 ORDER BY salary DESC
LIMIT 0,5;
2.5 使用集合函数查询MAX()
多少个员工
SELECT COUNT(*) FROM employee5;
101部门多少人
SELECT COUNT(*) FROM employee5 WHERE dep_id=101;
谁的工资最高
SELECT MAX(salary) FROM employee5;
谁的工资最低
SELECT MIN(salary) FROM employee5;
平均薪资是多少
SELECT AVG(salary) FROM employee5;
全公司薪资总和是多少
SELECT SUM(salary) FROM employee5;
101部门薪资总和是多少
SELECT SUM(salary) FROM employee5 WHERE dep_id=101;
将员工名称集中起来
select group_concat(name) from employee5 ;
2.6 分组查询GROUP BY
每个部门的人员名单
SELECT dep_id,GROUP_CONCAT(name) FROM employee5 GROUP BY dep_id;
每个部门的工资总和
select dep_id,SUM(salary) from employee5 group by dep_id;
每个部门的工资平均值
select dep_id,AVG(salary) from employee5 group by dep_id;
2.7 使用正则表达式查询
SELECT * FROM employee5 WHERE name REGEXP '^ali';
SELECT * FROM employee5 WHERE name REGEXP 'yun$';
SELECT * FROM employee5 WHERE name REGEXP 'm{2}';
3 MySQL多表查询
3.1 准备工作
准备员工信息表
create table info(
name char(50),
age int,
dep_num int,
level_num int);
desc info;
insert into info values
('zhangsan',23,101,1),
('lisi',25,102,2),
('wangwu',30,102,3),
('zhaosi',30,103,4),
('sunba',35,NULL,NULL);
select * from info;
准备部门信息表
create table department(
dep_num int,
dep_name varchar(50),
dep_des varchar(100));
insert into department values
(101,'hr','recruit,training'),
(102,'tec','system,network,service'),
(103,'exp','C++,python,php'),
(104,'admin','administrator');
desc department;
select * from department;
3.2 多表的连接查询
分类
- 交叉连接:生成笛卡尔积,它不使用任何匹配条件
- 内连接: 只连接匹配的行
- 外连接左连接:会显示左边表内所有的值,不论在右边表内匹不匹配
- 外连接右连接:会显示右边表内所有的值,不论在左边表内匹不匹配
交叉连接
特点:全部组合(A表5行,B表7行,最后5*7=35行)
示范:
select info.name,info.age,info.dep_num,department.dep_name from info,department;
内连接
特点:两列相同时,才会显示
需求:查询具有部门信息的员工。
语法:
SELECT 字段列表
FROM 表1 , 表2
WHERE 表1.字段 = 表2.字段;
示例
select info.name,info.age,info.dep_num,department.dep_name from info,department where info.dep_num = department.dep_num;
外连接
特点:两列相同时显示,并以左/右表为主。
外连接(左连接 left join)
select info.name,info.age,info.dep_num,department.dep_name from info left join department on info.dep_num = department.dep_num;
外连接(右连接right join)
select info.name,info.age,info.dep_num,department.dep_name from info right join department on info.dep_num = department.dep_num;
3.3 子查询
-
带IN关键字的子查询(范围)
select dep_num,dep_name from department where dep_num in (select distinct dep_num from info where age >=35); -
带EXISTS关键字的子查询(返回值)
select * from info
where
exists
(select * from department where dep_num=102);
mysql阶段