一、前言
做为实施工程师,在现场有时候是需要协助开发人员做一些简单的sql查询、更新动作的,不需要多复杂,下面就简单介绍下mysql的安装以及举一些最简单常用的sql用例。
二、安装mysql
2.1 下载安装包
第一步:在百度搜索"Mysql",我们点击第一个连接,如下图所示。
第二步:点击上图的链接后我们进入到如下图所示的界面,我们点击"Downloads"子菜单,可以看到如下图所示界面,在界面的下方点击"MySQL Community (GPL) Downloads >>"
第三步:我们点击"MySQL Community Server",如下图所示。
第四步:目前mysql的版本已经8.0.33了,我们想用5.7版本,因此这里点击"Archives",如下图所示。
第五步:版本我这里选择5.7.36,下面可以看到mysql-5.7.36-winx64.zip,点击右边的"Download"即可下载。
第六步:解压,我这里是下载到D盘的mysql目录下了,并且在这里进行了解压,大家根据自己的实际情况去解压。
2.2 安装
安装mysql,网上有很多教程,大家可以参考mysql-5.7.36-winx64.zip的安装_海风(0.0)的博客-CSDN博客这篇博客进行安装,这里不再赘述了。
三、创建表
3.1 创建员工表
员工表,包括编号、姓名、性别、年龄、出生日期、入职日期、部门编号、薪资等。
CREATE TABLE `employee` (
`employee_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '员工主键id',
`employee_code` varchar(64) NOT NULL COMMENT '员工编号',
`employee_name` varchar(128) NOT NULL COMMENT '员工姓名',
`sex` tinyint(4) NOT NULL COMMENT '员工性别(1-男, 2-女)',
`age` tinyint(4) NOT NULL COMMENT '员工年龄',
`birthday` timestamp NULL DEFAULT NULL COMMENT '出生日期',
`entry_date` timestamp NULL DEFAULT NULL COMMENT '入职日期',
`department_code` varchar(128) DEFAULT '' COMMENT '部门编号',
`salary` decimal(7,2) NOT NULL COMMENT '薪资',
`creator_username` varchar(32) NOT NULL COMMENT '创建人账号',
`creation_time` timestamp NOT NULL COMMENT '创建时间',
`updator_username` varchar(32) NOT NULL COMMENT '修改人账号',
`update_time` timestamp NOT NULL COMMENT '修改时间',
PRIMARY KEY (`employee_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='员工表';
3.2 创建部门表
部门表,包括部门编号、部门名称、父级部门编号、部门经理、部门描述等
CREATE TABLE `department` (
`department_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '部门主键id',
`department_code` varchar(64) NOT NULL COMMENT '部门编号',
`department_name` varchar(128) NOT NULL COMMENT '部门名称',
`parent_department_code` varchar(64) DEFAULT NULL COMMENT '父级部门编号',
`department_manager` varchar(128) DEFAULT '' COMMENT '部门经理',
`department_desc` varchar(128) DEFAULT '' COMMENT '部门描述',
`creator_username` varchar(32) NOT NULL COMMENT '创建人账号',
`creation_time` timestamp NOT NULL COMMENT '创建时间',
`updator_username` varchar(32) NOT NULL COMMENT '修改人账号',
`update_time` timestamp NOT NULL COMMENT '修改时间',
PRIMARY KEY (`department_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='部门表';
四、插入表数据
4.1 员工数据
INSERT INTO `employee`(`employee_id`, `employee_code`, `employee_name`, `sex`, `age`, `birthday`, `entry_date`, `department_code`, `salary`, `creator_username`, `creation_time`, `updator_username`, `update_time`) VALUES (1, 'E1001', '王茂森', 1, 37, '1986-07-16 00:00:00', '2015-01-01 00:00:00', 'Z001', 80000.00, 'admin', '2015-01-01 00:00:00', 'admin', '2015-01-01 00:00:00');
INSERT INTO `employee`(`employee_id`, `employee_code`, `employee_name`, `sex`, `age`, `birthday`, `entry_date`, `department_code`, `salary`, `creator_username`, `creation_time`, `updator_username`, `update_time`) VALUES (2, 'E1002', '赵琳萱', 2, 30, '1993-05-01 00:00:00', '2015-03-10 00:00:00', 'D002', 20000.00, 'admin', '2015-03-10 00:00:00', 'admin', '2015-03-10 00:00:00');
INSERT INTO `employee`(`employee_id`, `employee_code`, `employee_name`, `sex`, `age`, `birthday`, `entry_date`, `department_code`, `salary`, `creator_username`, `creation_time`, `updator_username`, `update_time`) VALUES (3, 'E1003', '张婧云', 2, 32, '1991-06-06 00:00:00', '2015-03-15 00:00:00', 'D003', 40000.00, 'admin', '2015-03-15 00:00:00', 'admin', '2015-03-15 00:00:00');
INSERT INTO `employee`(`employee_id`, `employee_code`, `employee_name`, `sex`, `age`, `birthday`, `entry_date`, `department_code`, `salary`, `creator_username`, `creation_time`, `updator_username`, `update_time`) VALUES (4, 'E1004', '刘明杰', 1, 35, '1988-05-11 00:00:00', '2015-03-15 00:00:00', 'D004', 30000.00, 'admin', '2015-03-15 00:00:00', 'admin', '2015-03-15 00:00:00');
INSERT INTO `employee`(`employee_id`, `employee_code`, `employee_name`, `sex`, `age`, `birthday`, `entry_date`, `department_code`, `salary`, `creator_username`, `creation_time`, `updator_username`, `update_time`) VALUES (5, 'E1005', '徐秒云', 2, 31, '1992-12-10 00:00:00', '2015-03-18 00:00:00', 'D004', 18000.00, 'admin', '2015-03-18 00:00:00', 'admin', '2015-03-18 00:00:00');
INSERT INTO `employee`(`employee_id`, `employee_code`, `employee_name`, `sex`, `age`, `birthday`, `entry_date`, `department_code`, `salary`, `creator_username`, `creation_time`, `updator_username`, `update_time`) VALUES (6, 'E1006', '钱晓彬', 1, 31, '1992-08-06 00:00:00', '2015-03-20 00:00:00', 'D001', 33000.00, 'admin', '2015-03-20 00:00:00', 'admin', '2015-03-20 00:00:00');
INSERT INTO `employee`(`employee_id`, `employee_code`, `employee_name`, `sex`, `age`, `birthday`, `entry_date`, `department_code`, `salary`, `creator_username`, `creation_time`, `updator_username`, `update_time`) VALUES (7, 'E1007', '李泽坤', 1, 35, '1988-05-15 00:00:00', '2015-05-10 00:00:00', 'D003', 36000.00, 'admin', '2015-05-10 00:00:00', 'admin', '2015-05-10 00:00:00');
INSERT INTO `employee`(`employee_id`, `employee_code`, `employee_name`, `sex`, `age`, `birthday`, `entry_date`, `department_code`, `salary`, `creator_username`, `creation_time`, `updator_username`, `update_time`) VALUES (8, 'E1008', '周彦君', 1, 33, '1990-02-14 00:00:00', '2015-05-15 00:00:00', 'D003', 38000.00, 'admin', '2015-05-15 00:00:00', 'admin', '2015-05-15 00:00:00');
INSERT INTO `employee`(`employee_id`, `employee_code`, `employee_name`, `sex`, `age`, `birthday`, `entry_date`, `department_code`, `salary`, `creator_username`, `creation_time`, `updator_username`, `update_time`) VALUES (9, 'E1009', '吴承志', 1, 30, '1993-06-18 00:00:00', '2015-05-20 00:00:00', 'D003', 30000.00, 'admin', '2015-05-20 00:00:00', 'admin', '2015-05-20 00:00:00');
INSERT INTO `employee`(`employee_id`, `employee_code`, `employee_name`, `sex`, `age`, `birthday`, `entry_date`, `department_code`, `salary`, `creator_username`, `creation_time`, `updator_username`, `update_time`) VALUES (10, 'E1010', '王冰雪', 2, 27, '1996-06-18 00:00:00', '2016-01-20 00:00:00', 'D004', 15000.00, 'admin', '2016-01-20 00:00:00', 'admin', '2016-01-20 00:00:00');
INSERT INTO `employee`(`employee_id`, `employee_code`, `employee_name`, `sex`, `age`, `birthday`, `entry_date`, `department_code`, `salary`, `creator_username`, `creation_time`, `updator_username`, `update_time`) VALUES (11, 'E1011', '王希雨', 2, 30, '1993-08-11 00:00:00', '2016-01-20 00:00:00', 'D002', 12000.00, 'admin', '2016-01-20 00:00:00', 'admin', '2016-01-20 00:00:00');
INSERT INTO `employee`(`employee_id`, `employee_code`, `employee_name`, `sex`, `age`, `birthday`, `entry_date`, `department_code`, `salary`, `creator_username`, `creation_time`, `updator_username`, `update_time`) VALUES (12, 'E1012', '王宇恒', 1, 30, '1993-08-11 00:00:00', '2016-02-15 00:00:00', 'D002', 13000.00, 'admin', '2016-02-15 00:00:00', 'admin', '2016-02-15 00:00:00');
INSERT INTO `employee`(`employee_id`, `employee_code`, `employee_name`, `sex`, `age`, `birthday`, `entry_date`, `department_code`, `salary`, `creator_username`, `creation_time`, `updator_username`, `update_time`) VALUES (13, 'E1013', '赵维星', 1, 32, '1995-03-20 00:00:00', '2016-02-15 00:00:00', 'D002', 13000.00, 'admin', '2016-02-15 00:00:00', 'admin', '2016-02-15 00:00:00');
INSERT INTO `employee`(`employee_id`, `employee_code`, `employee_name`, `sex`, `age`, `birthday`, `entry_date`, `department_code`, `salary`, `creator_username`, `creation_time`, `updator_username`, `update_time`) VALUES (14, 'E1014', '钱佳明', 1, 33, '1996-03-25 00:00:00', '2016-02-18 00:00:00', 'D001', 21000.00, 'admin', '2016-02-18 00:00:00', 'admin', '2016-02-18 00:00:00');
INSERT INTO `employee`(`employee_id`, `employee_code`, `employee_name`, `sex`, `age`, `birthday`, `entry_date`, `department_code`, `salary`, `creator_username`, `creation_time`, `updator_username`, `update_time`) VALUES (15, 'E1015', '孙佳明', 1, 31, '1992-03-26 00:00:00', '2016-03-05 00:00:00', 'D001', 20000.00, 'admin', '2016-03-05 00:00:00', 'admin', '2016-03-05 00:00:00');
INSERT INTO `employee`(`employee_id`, `employee_code`, `employee_name`, `sex`, `age`, `birthday`, `entry_date`, `department_code`, `salary`, `creator_username`, `creation_time`, `updator_username`, `update_time`) VALUES (16, 'E1016', '李世科', 1, 31, '1992-04-01 00:00:00', '2016-03-08 00:00:00', 'D001', 20000.00, 'admin', '2016-03-08 00:00:00', 'admin', '2016-03-08 00:00:00');
INSERT INTO `employee`(`employee_id`, `employee_code`, `employee_name`, `sex`, `age`, `birthday`, `entry_date`, `department_code`, `salary`, `creator_username`, `creation_time`, `updator_username`, `update_time`) VALUES (17, 'E1017', '周剑涛', 1, 33, '1990-04-03 00:00:00', '2016-03-08 00:00:00', 'D002', 15000.00, 'admin', '2016-03-08 00:00:00', 'admin', '2016-03-08 00:00:00');
INSERT INTO `employee`(`employee_id`, `employee_code`, `employee_name`, `sex`, `age`, `birthday`, `entry_date`, `department_code`, `salary`, `creator_username`, `creation_time`, `updator_username`, `update_time`) VALUES (18, 'E1018', '王满堂', 1, 29, '1994-04-03 00:00:00', '2016-03-10 00:00:00', 'D003', 25000.00, 'admin', '2016-03-10 00:00:00', 'admin', '2016-03-10 00:00:00');
INSERT INTO `employee`(`employee_id`, `employee_code`, `employee_name`, `sex`, `age`, `birthday`, `entry_date`, `department_code`, `salary`, `creator_username`, `creation_time`, `updator_username`, `update_time`) VALUES (19, 'E1019', '吴俊祺', 1, 30, '1993-07-06 00:00:00', '2016-05-20 00:00:00', 'D003', 23000.00, 'admin', '2016-05-20 00:00:00', 'admin', '2016-05-20 00:00:00');
INSERT INTO `employee`(`employee_id`, `employee_code`, `employee_name`, `sex`, `age`, `birthday`, `entry_date`, `department_code`, `salary`, `creator_username`, `creation_time`, `updator_username`, `update_time`) VALUES (20, 'E1020', '郑庆军', 1, 31, '1992-07-06 00:00:00', '2016-06-21 00:00:00', 'D003', 26000.00, 'admin', '2016-06-21 00:00:00', 'admin', '2016-06-21 00:00:00');
4.2 部门数据
INSERT INTO `department`(`department_id`, `department_code`, `department_name`, `parent_department_code`, `department_manager`, `department_desc`, `creator_username`, `creation_time`, `updator_username`, `update_time`) VALUES (1, 'Z001', '总部', '', 'E1001', '总裁办公室', 'admin', '2015-01-01 15:25:07', 'admin', '2015-01-01 15:25:07');
INSERT INTO `department`(`department_id`, `department_code`, `department_name`, `parent_department_code`, `department_manager`, `department_desc`, `creator_username`, `creation_time`, `updator_username`, `update_time`) VALUES (2, 'D001', '产品部', 'Z001', 'E1006', '产品设计', 'admin', '2015-01-01 15:28:49', 'admin', '2015-03-20 15:28:49');
INSERT INTO `department`(`department_id`, `department_code`, `department_name`, `parent_department_code`, `department_manager`, `department_desc`, `creator_username`, `creation_time`, `updator_username`, `update_time`) VALUES (3, 'D002', '前端', 'Z001', 'E1002', '前端设计开发', 'admin', '2015-01-01 15:30:50', 'admin', '2015-03-10 15:30:50');
INSERT INTO `department`(`department_id`, `department_code`, `department_name`, `parent_department_code`, `department_manager`, `department_desc`, `creator_username`, `creation_time`, `updator_username`, `update_time`) VALUES (4, 'D003', '研发部', 'Z001', 'E1003', '后端研发', 'admin', '2015-01-01 15:35:30', 'admin', '2015-03-15 15:35:30');
INSERT INTO `department`(`department_id`, `department_code`, `department_name`, `parent_department_code`, `department_manager`, `department_desc`, `creator_username`, `creation_time`, `updator_username`, `update_time`) VALUES (5, 'D004', '测试部', 'Z001', 'E1004', '测试', 'admin', '2015-01-01 15:38:25', 'admin', '2015-03-15 15:38:25');
五、常见sql学习
5.1 等于查询
查询员工编号为"E1002"员工的编号、姓名、年龄,表头用汉字显示。sql如下,其中as 后面加的是别名,不加别名的话,表头显示的是"employee_code"等这样的英文。
select employee_code as '员工编号',employee_name as '员工姓名',age as '年龄' from employee where employee_code = 'E1002';
查询结果如下,可以看到员工编号为"E1002"的员工姓名叫赵琳萱,年龄30岁了。
5.2 右模糊查询
查询姓"赵"的员工都有哪些,sql如下。这里只说了姓赵,后面的名字并不知道,因此查询就需要用到模糊查询,由于最左边已经知道是"赵"字,因此只需要右模糊查询即可,就是在"赵"后边紧着加上"%",而且是用like关键字来查询。
select * from employee where employee_name like '赵%';
查询出来的结果如下图所示,可以看到姓赵的一共有两位,分别叫赵琳萱和赵维星。
5.3 and查询
查询年龄在30到31岁之间的员工有哪些?sql如下,范围查询可以使用">="和"<=",年龄在30岁到31岁,说明不仅要大于等于30岁,而且同时要满足小于等于31岁,这是并且的关系,并且的关系用and进行关联。
select * from employee where age >= 30 and age <= 31;
查询出来的结果如下图所示:
5.4 or查询
查询年龄小于30岁或者年龄大于32岁的员工,sql如下,这里面有个关键词是"或者",也就是说年龄小于30岁或者年龄大于32岁的都是符合条件的,这时候我们用到的关键字是"or"。
select * from employee where age < 30 or age > 32;
查询结果如下:
5.5 多张表关联、分组查询
各个部门分别有多少人?sql如下,这个sql比较复杂一些,因为涉及到了两张表关联查询,为何用两张表来查?那是因为我们的部门名称不在员工表里面,员工表里面只有一个部门编号,而我们要想显示出来部门名称就得去部门表查询,员工表和部门表都有一个共同的字段,那就是部门编号,我们便需要使用left join进行表关联查询,left join后面会用on来添加两张表的关联关系,由于涉及到按部门统计人数,因此涉及到了分组,也就是group by,group by 后面就是部门编号。
select d.department_code as '部门编号',d.department_name as '部门名称', count(1) as '部门人数' from employee e left join department d on d.department_code = e.department_code group by e.department_code;
查询结果如下:
5.6 更新
假如数据库中某个字段的值需要修改,这里假如员工编号为"E1005"的员工的入职日期错了,需要将入职日期改为"2015-03-19",那么我们就使用如下sql,update用来更新,set后面加我们需要修改的字段,where后面跟修改的条件,这里我们指定的是只修改员工编号为"E1005"的员工。
我们先看看修改前E1005的入职日期,可以看到是"2015-03-18"。
sql如下所示
update employee set entry_date = '2015-03-19' where employee_code = 'E1005';
修改后结果如下图,可以看到已经修改过来了。