|
@@ -0,0 +1,877 @@
|
|
|
|
|
+<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
|
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
|
|
|
+<plist version="1.0">
|
|
|
|
|
+<dict>
|
|
|
|
|
+ <key>ContentFilters</key>
|
|
|
|
|
+ <dict/>
|
|
|
|
|
+ <key>auto_connect</key>
|
|
|
|
|
+ <true/>
|
|
|
|
|
+ <key>data</key>
|
|
|
|
|
+ <dict>
|
|
|
|
|
+ <key>connection</key>
|
|
|
|
|
+ <dict>
|
|
|
|
|
+ <key>database</key>
|
|
|
|
|
+ <string>hrs</string>
|
|
|
|
|
+ <key>host</key>
|
|
|
|
|
+ <string>120.77.222.217</string>
|
|
|
|
|
+ <key>kcid</key>
|
|
|
|
|
+ <string>6157604644212181126</string>
|
|
|
|
|
+ <key>name</key>
|
|
|
|
|
+ <string>MySQL@Aliyun</string>
|
|
|
|
|
+ <key>rdbms_type</key>
|
|
|
|
|
+ <string>mysql</string>
|
|
|
|
|
+ <key>sslCACertFileLocation</key>
|
|
|
|
|
+ <string></string>
|
|
|
|
|
+ <key>sslCACertFileLocationEnabled</key>
|
|
|
|
|
+ <integer>0</integer>
|
|
|
|
|
+ <key>sslCertificateFileLocation</key>
|
|
|
|
|
+ <string></string>
|
|
|
|
|
+ <key>sslCertificateFileLocationEnabled</key>
|
|
|
|
|
+ <integer>0</integer>
|
|
|
|
|
+ <key>sslKeyFileLocation</key>
|
|
|
|
|
+ <string></string>
|
|
|
|
|
+ <key>sslKeyFileLocationEnabled</key>
|
|
|
|
|
+ <integer>0</integer>
|
|
|
|
|
+ <key>type</key>
|
|
|
|
|
+ <string>SPTCPIPConnection</string>
|
|
|
|
|
+ <key>useSSL</key>
|
|
|
|
|
+ <integer>0</integer>
|
|
|
|
|
+ <key>user</key>
|
|
|
|
|
+ <string>root</string>
|
|
|
|
|
+ </dict>
|
|
|
|
|
+ <key>session</key>
|
|
|
|
|
+ <dict>
|
|
|
|
|
+ <key>connectionEncoding</key>
|
|
|
|
|
+ <string>utf8</string>
|
|
|
|
|
+ <key>contentPageNumber</key>
|
|
|
|
|
+ <integer>1</integer>
|
|
|
|
|
+ <key>contentSelection</key>
|
|
|
|
|
+ <data>
|
|
|
|
|
+ YnBsaXN0MDDUAQIDBAUGOTpYJHZlcnNpb25YJG9iamVjdHNZJGFy
|
|
|
|
|
+ Y2hpdmVyVCR0b3ASAAGGoK0HCBUWFxgZHSQoLDE2VSRudWxs0wkK
|
|
|
|
|
+ CwwQFFdOUy5rZXlzWk5TLm9iamVjdHNWJGNsYXNzow0OD4ACgAOA
|
|
|
|
|
+ BKMREhOABYAGgAiADFR0eXBlVHJvd3NUa2V5c18QJlNlbGVjdGlv
|
|
|
|
|
+ bkRldGFpbFR5cGVQcmltYXJ5S2V5ZWREZXRhaWxz0wkKCxobHKCg
|
|
|
|
|
+ gAfSHh8gIVokY2xhc3NuYW1lWCRjbGFzc2VzXxATTlNNdXRhYmxl
|
|
|
|
|
+ RGljdGlvbmFyeaMgIiNcTlNEaWN0aW9uYXJ5WE5TT2JqZWN00goL
|
|
|
|
|
+ JSehJoAJgAvSCykqK1lOUy5zdHJpbmeAClNlbm/SHh8tLl8QD05T
|
|
|
|
|
+ TXV0YWJsZVN0cmluZ6MtLzBYTlNTdHJpbmdYTlNPYmplY3TSHh8y
|
|
|
|
|
+ M15OU011dGFibGVBcnJheaMyNDVXTlNBcnJheVhOU09iamVjdNIe
|
|
|
|
|
+ HyI3oiI4WE5TT2JqZWN0XxAPTlNLZXllZEFyY2hpdmVy0Ts8VGRh
|
|
|
|
|
+ dGGAAQAIABEAGgAjAC0AMgA3AEUASwBSAFoAZQBsAHAAcgB0AHYA
|
|
|
|
|
+ egB8AH4AgACCAIcAjACRALoAwQDCAMMAxQDKANUA3gD0APgBBQEO
|
|
|
|
|
+ ARMBFQEXARkBHgEoASoBLgEzAUUBSQFSAVsBYAFvAXMBewGEAYkB
|
|
|
|
|
+ jAGVAacBqgGvAAAAAAAAAgEAAAAAAAAAPQAAAAAAAAAAAAAAAAAA
|
|
|
|
|
+ AbE=
|
|
|
|
|
+ </data>
|
|
|
|
|
+ <key>contentSortColIsAsc</key>
|
|
|
|
|
+ <true/>
|
|
|
|
|
+ <key>contentViewport</key>
|
|
|
|
|
+ <string>{{0, 0}, {694, 448}}</string>
|
|
|
|
|
+ <key>isToolbarVisible</key>
|
|
|
|
|
+ <true/>
|
|
|
|
|
+ <key>queries</key>
|
|
|
|
|
+ <string>-- 注意事项:
|
|
|
|
|
+-- 1. 给数据库和表命名的时候尽量使用全小写
|
|
|
|
|
+-- 2. 作为筛选条件的字符串是否区分大小看设置的校对规则
|
|
|
|
|
+drop database if exists hrs;
|
|
|
|
|
+create database hrs default charset utf8 collate utf8_general_ci;
|
|
|
|
|
+
|
|
|
|
|
+use hrs;
|
|
|
|
|
+
|
|
|
|
|
+drop table if exists tb_emp;
|
|
|
|
|
+drop table if exists tb_dept;
|
|
|
|
|
+
|
|
|
|
|
+-- 3. 数据库中的对象通常会用前缀加以区分
|
|
|
|
|
+-- table / view / index / function / procedure / trigger
|
|
|
|
|
+create table tb_dept
|
|
|
|
|
+(
|
|
|
|
|
+dno int not null comment '编号',
|
|
|
|
|
+dname varchar(10) not null comment '名称',
|
|
|
|
|
+dloc varchar(20) not null comment '所在地',
|
|
|
|
|
+primary key (dno)
|
|
|
|
|
+);
|
|
|
|
|
+
|
|
|
|
|
+-- 批量插入操作
|
|
|
|
|
+insert into tb_dept values
|
|
|
|
|
+ (10, '会计部', '北京'),
|
|
|
|
|
+ (20, '研发部', '成都'),
|
|
|
|
|
+ (30, '销售部', '重庆'),
|
|
|
|
|
+ (40, '运维部', '深圳');
|
|
|
|
|
+
|
|
|
|
|
+create table tb_emp
|
|
|
|
|
+(
|
|
|
|
|
+eno int not null comment '员工编号',
|
|
|
|
|
+ename varchar(20) not null comment '员工姓名',
|
|
|
|
|
+job varchar(20) not null comment '员工职位',
|
|
|
|
|
+mgr int comment '主管编号',
|
|
|
|
|
+sal int not null comment '员工月薪',
|
|
|
|
|
+comm int comment '每月补贴',
|
|
|
|
|
+dno int comment '所在部门编号',
|
|
|
|
|
+primary key (eno)
|
|
|
|
|
+);
|
|
|
|
|
+
|
|
|
|
|
+-- 修改表添加一个列到mgr列的后面
|
|
|
|
|
+-- alter table tb_emp add column hiredate date after mgr;
|
|
|
|
|
+
|
|
|
|
|
+-- 修改表添加一个自参照的外键约束
|
|
|
|
|
+-- alter table tb_emp add constraint fk_emp_mgr foreign key (mgr) references tb_emp (eno);
|
|
|
|
|
+
|
|
|
|
|
+-- alter table tb_emp drop foreign key fk_emp_mgr;
|
|
|
|
|
+
|
|
|
|
|
+-- alter table tb_emp drop foreign key fk_emp_dno;
|
|
|
|
|
+
|
|
|
|
|
+-- restrict - 不允许操作
|
|
|
|
|
+-- cascade - 级联操作
|
|
|
|
|
+-- set null - 设置为null
|
|
|
|
|
+alter table tb_emp add constraint fk_emp_dno foreign key (dno) references tb_dept (dno);
|
|
|
|
|
+-- on delete restrict on update cascade;
|
|
|
|
|
+
|
|
|
|
|
+insert into tb_emp values
|
|
|
|
|
+ (7800, '张三丰', '总裁', null, 9000, 1200, 20),
|
|
|
|
|
+ (2056, '乔峰', '分析师', 7800, 5000, 1500, 20),
|
|
|
|
|
+ (3088, '李莫愁', '设计师', 2056, 3500, 800, 20),
|
|
|
|
|
+ (3211, '张无忌', '程序员', 2056, 3200, null, 20),
|
|
|
|
|
+ (3233, '丘处机', '程序员', 2056, 3400, null, 20),
|
|
|
|
|
+ (3251, '张翠山', '程序员', 2056, 4000, null, 20),
|
|
|
|
|
+ (5566, '宋远桥', '会计师', 7800, 4000, 1000, 10),
|
|
|
|
|
+ (5234, '郭靖', '出纳', 5566, 2000, null, 10),
|
|
|
|
|
+ (3344, '黄蓉', '销售主管', 7800, 3000, 800, 30),
|
|
|
|
|
+ (1359, '胡一刀', '销售员', 3344, 1800, 200, 30),
|
|
|
|
|
+ (4466, '苗人凤', '销售员', 3344, 2500, null, 30),
|
|
|
|
|
+ (3244, '欧阳锋', '程序员', 3088, 3200, null, 20),
|
|
|
|
|
+ (3577, '杨过', '会计', 5566, 2200, null, 10),
|
|
|
|
|
+ (3588, '朱九真', '会计', 5566, 2500, null, 10);
|
|
|
|
|
+
|
|
|
|
|
+-- 查询月薪最高的员工姓名和工资 - 子查询/嵌套查询+聚合函数
|
|
|
|
|
+-- select ename, sal from tb_emp order by sal desc limit 1;
|
|
|
|
|
+select ename, sal from tb_emp where sal=(
|
|
|
|
|
+ select max(sal) from tb_emp
|
|
|
|
|
+);
|
|
|
|
|
+
|
|
|
|
|
+-- 查询员工的姓名和年薪((月薪+补贴)*12)
|
|
|
|
|
+select ename, (sal+ifnull(comm, 0))*12 as 年薪 from tb_emp order by 年薪 desc;
|
|
|
|
|
+
|
|
|
|
|
+-- 查询有员工的部门的编号和人数 - 分组查询+聚合函数
|
|
|
|
|
+select dno, count(dno) from tb_emp group by dno with rollup;
|
|
|
|
|
+
|
|
|
|
|
+-- 查询所有部门的名称和人数 - 子查询+连接查询(左外)
|
|
|
|
|
+select dname as 部门名称, ifnull(total, 0) as 人数 from tb_dept t1 left join (select dno, count(dno) as total from tb_emp group by dno) t2 on t1.dno=t2.dno;
|
|
|
|
|
+
|
|
|
|
|
+-- 查询月薪最高的员工(Boss除外)的姓名和工资 - 空值判断
|
|
|
|
|
+select ename, sal from tb_emp where sal=(select max(sal) from tb_emp where mgr is not null);
|
|
|
|
|
+
|
|
|
|
|
+-- 查询月薪超过平均薪水的员工的姓名和工资
|
|
|
|
|
+select ename, sal from tb_emp where sal>(select avg(sal) as avgsal from tb_emp);
|
|
|
|
|
+
|
|
|
|
|
+-- 查询月薪超过其所在部门平均薪水的员工的姓名、部门编号和工资
|
|
|
|
|
+select ename, t1.dno, sal, round(avgsal, 2) from tb_emp t1 inner join (select dno, avg(sal) as avgsal from tb_emp group by dno) t2 on t1.dno=t2.dno where sal>avgsal;
|
|
|
|
|
+
|
|
|
|
|
+-- 查询部门中薪水最高的人姓名、工资和所在部门名称
|
|
|
|
|
+select ename, sal, dname from (select ename, sal, t1.dno from tb_emp t1 inner join (select dno, max(sal) as maxsal from tb_emp group by dno) t2 on t1.dno=t2.dno where sal=maxsal) t3 inner join tb_dept t4 on t3.dno=t4.dno;
|
|
|
|
|
+
|
|
|
|
|
+-- 查询主管的姓名和职位
|
|
|
|
|
+select ename, job from tb_emp where eno in (select distinct mgr from tb_emp where mgr is not null);
|
|
|
|
|
+
|
|
|
|
|
+-- 通常不推荐使用in或者not in集合运算和distinct去重操作
|
|
|
|
|
+-- 可以考虑用exists或not exists替代掉集合运算和去重操作
|
|
|
|
|
+select ename, job from tb_emp t1 where exists (select 'x' from tb_emp t2 where t1.eno=t2.mgr);
|
|
|
|
|
+
|
|
|
|
|
+-- 查询月薪排名4~6名的员工姓名和工资
|
|
|
|
|
+select ename, sal from tb_emp order by sal desc limit 3, 3;
|
|
|
|
|
+select ename, sal from tb_emp order by sal desc limit 3 offset 3;
|
|
|
|
|
+
|
|
|
|
|
+-- explain生成执行计划
|
|
|
|
|
+explain select eno, ename from tb_emp where eno=7800;
|
|
|
|
|
+explain select eno, ename from tb_emp where eno<>7900;
|
|
|
|
|
+explain select eno, ename from tb_emp where ename='张三丰';
|
|
|
|
|
+explain select eno, ename from tb_emp where ename like '张%';
|
|
|
|
|
+explain select eno, ename from tb_emp where ename like '%张';
|
|
|
|
|
+explain select eno, ename from tb_emp where ename<>'张三丰';
|
|
|
|
|
+
|
|
|
|
|
+-- 视图:查询的快照(简化查询操作)
|
|
|
|
|
+-- 通过视图可以将用户的访问权限限制到某些指定的列上
|
|
|
|
|
+create view vw_emp_dept as
|
|
|
|
|
+select eno, ename, dname from tb_emp t1 inner join tb_dept t2 on t1.dno=t2.dno;
|
|
|
|
|
+
|
|
|
|
|
+select ename, dname from vw_emp_dept;
|
|
|
|
|
+
|
|
|
|
|
+drop view vw_emp_dept;
|
|
|
|
|
+
|
|
|
|
|
+-- 索引(index)
|
|
|
|
|
+-- 索引可以加速查询所以应该在经常用于查询筛选条件的列上建立索引
|
|
|
|
|
+-- 索引会使用额外的存储空间而且会让增删改变得更慢(因为要更新索引)
|
|
|
|
|
+-- 所以不能够滥用索引
|
|
|
|
|
+create index idx_emp_ename on tb_emp (ename);
|
|
|
|
|
+drop index idx_emp_ename on tb_emp;
|
|
|
|
|
+
|
|
|
|
|
+-- (存储)过程/函数:把一系列的SQL可以封装到一个过程中,而且可以加上分支和循环,将来通过过程的名字直接调用过程即可,因为创建过程时已经提前编译了SQL语句,所以比直接执行SQL语句性能更好
|
|
|
|
|
+
|
|
|
|
|
+-- 重新定义定界符为$$
|
|
|
|
|
+delimiter $$
|
|
|
|
|
+
|
|
|
|
|
+-- 创建存储过程
|
|
|
|
|
+create procedure sp_dept_avg_sal(deptno int, out avgsal float)
|
|
|
|
|
+begin
|
|
|
|
|
+ select avg(sal) into avgsal from tb_emp where dno=deptno;
|
|
|
|
|
+end$$
|
|
|
|
|
+
|
|
|
|
|
+-- 将定界符还原回;
|
|
|
|
|
+delimiter ;
|
|
|
|
|
+
|
|
|
|
|
+-- 调用存储过程
|
|
|
|
|
+call sp_dept_avg_sal(20, @a);
|
|
|
|
|
+
|
|
|
|
|
+-- 通过输出参数取出部门平均工资
|
|
|
|
|
+select @a;
|
|
|
|
|
+
|
|
|
|
|
+-- 删除存储过程
|
|
|
|
|
+drop procedure sp_dept_avg_sal;
|
|
|
|
|
+
|
|
|
|
|
+-- 触发器:在执行增删改操作时可以触发其他的级联操作,但是有可能导致“锁表”现象,实际开发中应该尽量避免使用触发器
|
|
|
|
|
+-- update tb_dept set dno=11 where dno=10;
|
|
|
|
|
+-- delete from tb_dept where dno=11;
|
|
|
|
|
+
|
|
|
|
|
+delimiter $$
|
|
|
|
|
+
|
|
|
|
|
+create trigger tr_dept_update
|
|
|
|
|
+after update on tb_dept for each row
|
|
|
|
|
+begin
|
|
|
|
|
+ update tb_emp set dno=new.dno where dno=old.dno;
|
|
|
|
|
+end$$
|
|
|
|
|
+
|
|
|
|
|
+delimiter ;
|
|
|
|
|
+
|
|
|
|
|
+drop trigger tr_dept_update;
|
|
|
|
|
+
|
|
|
|
|
+-- DCL:授予权限(grant to)和召回权限(revoke from)
|
|
|
|
|
+create user 'hellokitty'@'%' identified by '123123';
|
|
|
|
|
+
|
|
|
|
|
+grant all privileges on hrs.* to 'hellokitty'@'%';
|
|
|
|
|
+
|
|
|
|
|
+revoke insert, delete, update on hrs.* from 'hellokitty'@'%';
|
|
|
|
|
+
|
|
|
|
|
+drop user 'hellokitty'@'%';
|
|
|
|
|
+
|
|
|
|
|
+-- 事务(transaction)- 把多个增删改的操作做成不可分割的原子性操作
|
|
|
|
|
+-- 要么全部都做,要么全都不做
|
|
|
|
|
+
|
|
|
|
|
+-- start transaction;
|
|
|
|
|
+begin;
|
|
|
|
|
+
|
|
|
|
|
+delete from tb_emp;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+</string>
|
|
|
|
|
+ <key>table</key>
|
|
|
|
|
+ <string>tb_emp</string>
|
|
|
|
|
+ <key>view</key>
|
|
|
|
|
+ <string>SP_VIEW_CUSTOMQUERY</string>
|
|
|
|
|
+ <key>windowVerticalDividerPosition</key>
|
|
|
|
|
+ <real>163</real>
|
|
|
|
|
+ </dict>
|
|
|
|
|
+ </dict>
|
|
|
|
|
+ <key>encrypted</key>
|
|
|
|
|
+ <false/>
|
|
|
|
|
+ <key>format</key>
|
|
|
|
|
+ <string>connection</string>
|
|
|
|
|
+ <key>queryFavorites</key>
|
|
|
|
|
+ <array/>
|
|
|
|
|
+ <key>queryHistory</key>
|
|
|
|
|
+ <array>
|
|
|
|
|
+ <string>rollback</string>
|
|
|
|
|
+ <string>begin;
|
|
|
|
|
+delete from tb_emp</string>
|
|
|
|
|
+ <string>-- 注意事项:
|
|
|
|
|
+-- 1. 给数据库和表命名的时候尽量使用全小写
|
|
|
|
|
+-- 2. 作为筛选条件的字符串是否区分大小看设置的校对规则
|
|
|
|
|
+drop database if exists hrs;
|
|
|
|
|
+create database hrs default charset utf8 collate utf8_general_ci;
|
|
|
|
|
+use hrs;
|
|
|
|
|
+drop table if exists tb_emp;
|
|
|
|
|
+drop table if exists tb_dept;
|
|
|
|
|
+-- 3. 数据库中的对象通常会用前缀加以区分
|
|
|
|
|
+-- table / view / index / function / procedure / trigger
|
|
|
|
|
+create table tb_dept
|
|
|
|
|
+(
|
|
|
|
|
+dno int not null comment '编号',
|
|
|
|
|
+dname varchar(10) not null comment '名称',
|
|
|
|
|
+dloc varchar(20) not null comment '所在地',
|
|
|
|
|
+primary key (dno)
|
|
|
|
|
+);
|
|
|
|
|
+-- 批量插入操作
|
|
|
|
|
+insert into tb_dept values
|
|
|
|
|
+ (10, '会计部', '北京'),
|
|
|
|
|
+ (20, '研发部', '成都'),
|
|
|
|
|
+ (30, '销售部', '重庆'),
|
|
|
|
|
+ (40, '运维部', '深圳');
|
|
|
|
|
+create table tb_emp
|
|
|
|
|
+(
|
|
|
|
|
+eno int not null comment '员工编号',
|
|
|
|
|
+ename varchar(20) not null comment '员工姓名',
|
|
|
|
|
+job varchar(20) not null comment '员工职位',
|
|
|
|
|
+mgr int comment '主管编号',
|
|
|
|
|
+sal int not null comment '员工月薪',
|
|
|
|
|
+comm int comment '每月补贴',
|
|
|
|
|
+dno int comment '所在部门编号',
|
|
|
|
|
+primary key (eno)
|
|
|
|
|
+);
|
|
|
|
|
+-- 修改表添加一个列到mgr列的后面
|
|
|
|
|
+-- alter table tb_emp add column hiredate date after mgr;
|
|
|
|
|
+
|
|
|
|
|
+-- 修改表添加一个自参照的外键约束
|
|
|
|
|
+-- alter table tb_emp add constraint fk_emp_mgr foreign key (mgr) references tb_emp (eno);
|
|
|
|
|
+
|
|
|
|
|
+-- alter table tb_emp drop foreign key fk_emp_mgr;
|
|
|
|
|
+
|
|
|
|
|
+-- alter table tb_emp drop foreign key fk_emp_dno;
|
|
|
|
|
+
|
|
|
|
|
+-- restrict - 不允许操作
|
|
|
|
|
+-- cascade - 级联操作
|
|
|
|
|
+-- set null - 设置为null
|
|
|
|
|
+alter table tb_emp add constraint fk_emp_dno foreign key (dno) references tb_dept (dno);
|
|
|
|
|
+-- on delete restrict on update cascade;
|
|
|
|
|
+
|
|
|
|
|
+insert into tb_emp values
|
|
|
|
|
+ (7800, '张三丰', '总裁', null, 9000, 1200, 20),
|
|
|
|
|
+ (2056, '乔峰', '分析师', 7800, 5000, 1500, 20),
|
|
|
|
|
+ (3088, '李莫愁', '设计师', 2056, 3500, 800, 20),
|
|
|
|
|
+ (3211, '张无忌', '程序员', 2056, 3200, null, 20),
|
|
|
|
|
+ (3233, '丘处机', '程序员', 2056, 3400, null, 20),
|
|
|
|
|
+ (3251, '张翠山', '程序员', 2056, 4000, null, 20),
|
|
|
|
|
+ (5566, '宋远桥', '会计师', 7800, 4000, 1000, 10),
|
|
|
|
|
+ (5234, '郭靖', '出纳', 5566, 2000, null, 10),
|
|
|
|
|
+ (3344, '黄蓉', '销售主管', 7800, 3000, 800, 30),
|
|
|
|
|
+ (1359, '胡一刀', '销售员', 3344, 1800, 200, 30),
|
|
|
|
|
+ (4466, '苗人凤', '销售员', 3344, 2500, null, 30),
|
|
|
|
|
+ (3244, '欧阳锋', '程序员', 3088, 3200, null, 20),
|
|
|
|
|
+ (3577, '杨过', '会计', 5566, 2200, null, 10),
|
|
|
|
|
+ (3588, '朱九真', '会计', 5566, 2500, null, 10);
|
|
|
|
|
+-- 查询月薪最高的员工姓名和工资 - 子查询/嵌套查询+聚合函数
|
|
|
|
|
+-- select ename, sal from tb_emp order by sal desc limit 1;
|
|
|
|
|
+select ename, sal from tb_emp where sal=(
|
|
|
|
|
+ select max(sal) from tb_emp
|
|
|
|
|
+);
|
|
|
|
|
+-- 查询员工的姓名和年薪((月薪+补贴)*12)
|
|
|
|
|
+select ename, (sal+ifnull(comm, 0))*12 as 年薪 from tb_emp order by 年薪 desc;
|
|
|
|
|
+-- 查询有员工的部门的编号和人数 - 分组查询+聚合函数
|
|
|
|
|
+select dno, count(dno) from tb_emp group by dno with rollup;
|
|
|
|
|
+-- 查询所有部门的名称和人数 - 子查询+连接查询(左外)
|
|
|
|
|
+select dname as 部门名称, ifnull(total, 0) as 人数 from tb_dept t1 left join (select dno, count(dno) as total from tb_emp group by dno) t2 on t1.dno=t2.dno;
|
|
|
|
|
+-- 查询月薪最高的员工(Boss除外)的姓名和工资 - 空值判断
|
|
|
|
|
+select ename, sal from tb_emp where sal=(select max(sal) from tb_emp where mgr is not null);
|
|
|
|
|
+-- 查询月薪超过平均薪水的员工的姓名和工资
|
|
|
|
|
+select ename, sal from tb_emp where sal>(select avg(sal) as avgsal from tb_emp);
|
|
|
|
|
+-- 查询月薪超过其所在部门平均薪水的员工的姓名、部门编号和工资
|
|
|
|
|
+select ename, t1.dno, sal, round(avgsal, 2) from tb_emp t1 inner join (select dno, avg(sal) as avgsal from tb_emp group by dno) t2 on t1.dno=t2.dno where sal>avgsal;
|
|
|
|
|
+-- 查询部门中薪水最高的人姓名、工资和所在部门名称
|
|
|
|
|
+select ename, sal, dname from (select ename, sal, t1.dno from tb_emp t1 inner join (select dno, max(sal) as maxsal from tb_emp group by dno) t2 on t1.dno=t2.dno where sal=maxsal) t3 inner join tb_dept t4 on t3.dno=t4.dno;
|
|
|
|
|
+-- 查询主管的姓名和职位
|
|
|
|
|
+select ename, job from tb_emp where eno in (select distinct mgr from tb_emp where mgr is not null);
|
|
|
|
|
+-- 通常不推荐使用in或者not in集合运算和distinct去重操作
|
|
|
|
|
+-- 可以考虑用exists或not exists替代掉集合运算和去重操作
|
|
|
|
|
+select ename, job from tb_emp t1 where exists (select 'x' from tb_emp t2 where t1.eno=t2.mgr);
|
|
|
|
|
+-- 查询月薪排名4~6名的员工姓名和工资
|
|
|
|
|
+select ename, sal from tb_emp order by sal desc limit 3, 3;
|
|
|
|
|
+select ename, sal from tb_emp order by sal desc limit 3 offset 3;
|
|
|
|
|
+-- explain生成执行计划
|
|
|
|
|
+explain select eno, ename from tb_emp where eno=7800;
|
|
|
|
|
+explain select eno, ename from tb_emp where eno<>7900;
|
|
|
|
|
+explain select eno, ename from tb_emp where ename='张三丰';
|
|
|
|
|
+explain select eno, ename from tb_emp where ename like '张%';
|
|
|
|
|
+explain select eno, ename from tb_emp where ename like '%张';
|
|
|
|
|
+explain select eno, ename from tb_emp where ename<>'张三丰';
|
|
|
|
|
+-- 视图:查询的快照(简化查询操作)
|
|
|
|
|
+-- 通过视图可以将用户的访问权限限制到某些指定的列上
|
|
|
|
|
+create view vw_emp_dept as
|
|
|
|
|
+select eno, ename, dname from tb_emp t1 inner join tb_dept t2 on t1.dno=t2.dno;
|
|
|
|
|
+select ename, dname from vw_emp_dept;
|
|
|
|
|
+drop view vw_emp_dept;
|
|
|
|
|
+-- 索引(index)
|
|
|
|
|
+-- 索引可以加速查询所以应该在经常用于查询筛选条件的列上建立索引
|
|
|
|
|
+-- 索引会使用额外的存储空间而且会让增删改变得更慢(因为要更新索引)
|
|
|
|
|
+-- 所以不能够滥用索引
|
|
|
|
|
+create index idx_emp_ename on tb_emp (ename);
|
|
|
|
|
+drop index idx_emp_ename on tb_emp;
|
|
|
|
|
+-- 创建存储过程
|
|
|
|
|
+create procedure sp_dept_avg_sal(deptno int, out avgsal float)
|
|
|
|
|
+begin
|
|
|
|
|
+ select avg(sal) into avgsal from tb_emp where dno=deptno;
|
|
|
|
|
+end;
|
|
|
|
|
+-- 调用存储过程
|
|
|
|
|
+call sp_dept_avg_sal(20, @a);
|
|
|
|
|
+-- 通过输出参数取出部门平均工资
|
|
|
|
|
+select @a;
|
|
|
|
|
+-- 删除存储过程
|
|
|
|
|
+drop procedure sp_dept_avg_sal;
|
|
|
|
|
+create trigger tr_dept_update
|
|
|
|
|
+after update on tb_dept for each row
|
|
|
|
|
+begin
|
|
|
|
|
+ update tb_emp set dno=new.dno where dno=old.dno;
|
|
|
|
|
+end;
|
|
|
|
|
+drop trigger tr_dept_update;
|
|
|
|
|
+-- DCL:授予权限(grant to)和召回权限(revoke from)
|
|
|
|
|
+create user 'hellokitty'@'%' identified by '123123';
|
|
|
|
|
+grant all privileges on hrs.* to 'hellokitty'@'%';
|
|
|
|
|
+revoke insert, delete, update on hrs.* from 'hellokitty'@'%';
|
|
|
|
|
+drop user 'hellokitty'@'%';
|
|
|
|
|
+-- 事务(transaction)- 把多个增删改的操作做成不可分割的原子性操作
|
|
|
|
|
+-- 要么全部都做,要么全都不做
|
|
|
|
|
+
|
|
|
|
|
+-- start transaction;
|
|
|
|
|
+-- begin;
|
|
|
|
|
+
|
|
|
|
|
+-- delete from tb_emp;
|
|
|
|
|
+
|
|
|
|
|
+-- commit;
|
|
|
|
|
+-- rollback;</string>
|
|
|
|
|
+ <string>-- 事务(transaction)- 把多个增删改的操作做成不可分割的原子性操作
|
|
|
|
|
+-- 要么全部都做,要么全都不做
|
|
|
|
|
+
|
|
|
|
|
+-- start transaction;
|
|
|
|
|
+-- begin;
|
|
|
|
|
+
|
|
|
|
|
+-- delete from tb_emp;
|
|
|
|
|
+
|
|
|
|
|
+-- commit;
|
|
|
|
|
+-- rollback;</string>
|
|
|
|
|
+ <string>delete from tb_emp</string>
|
|
|
|
|
+ <string>alter table tb_emp drop foreign key fk_emp_mgr</string>
|
|
|
|
|
+ <string>begin</string>
|
|
|
|
|
+ <string>-- 注意事项:
|
|
|
|
|
+-- 1. 给数据库和表命名的时候尽量使用全小写
|
|
|
|
|
+-- 2. 作为筛选条件的字符串是否区分大小看设置的校对规则
|
|
|
|
|
+drop database if exists hrs;
|
|
|
|
|
+create database hrs default charset utf8 collate utf8_general_ci;
|
|
|
|
|
+use hrs;
|
|
|
|
|
+drop table if exists tb_emp;
|
|
|
|
|
+drop table if exists tb_dept;
|
|
|
|
|
+-- 3. 数据库中的对象通常会用前缀加以区分
|
|
|
|
|
+-- table / view / index / function / procedure / trigger
|
|
|
|
|
+create table tb_dept
|
|
|
|
|
+(
|
|
|
|
|
+dno int not null comment '编号',
|
|
|
|
|
+dname varchar(10) not null comment '名称',
|
|
|
|
|
+dloc varchar(20) not null comment '所在地',
|
|
|
|
|
+primary key (dno)
|
|
|
|
|
+);
|
|
|
|
|
+-- 批量插入操作
|
|
|
|
|
+insert into tb_dept values
|
|
|
|
|
+ (10, '会计部', '北京'),
|
|
|
|
|
+ (20, '研发部', '成都'),
|
|
|
|
|
+ (30, '销售部', '重庆'),
|
|
|
|
|
+ (40, '运维部', '深圳');
|
|
|
|
|
+create table tb_emp
|
|
|
|
|
+(
|
|
|
|
|
+eno int not null comment '员工编号',
|
|
|
|
|
+ename varchar(20) not null comment '员工姓名',
|
|
|
|
|
+job varchar(20) not null comment '员工职位',
|
|
|
|
|
+mgr int comment '主管编号',
|
|
|
|
|
+sal int not null comment '员工月薪',
|
|
|
|
|
+comm int comment '每月补贴',
|
|
|
|
|
+dno int comment '所在部门编号',
|
|
|
|
|
+primary key (eno)
|
|
|
|
|
+);
|
|
|
|
|
+-- 修改表添加一个列到mgr列的后面
|
|
|
|
|
+-- alter table tb_emp add column hiredate date after mgr;
|
|
|
|
|
+
|
|
|
|
|
+-- 修改表添加一个自参照的外键约束
|
|
|
|
|
+alter table tb_emp add constraint fk_emp_mgr foreign key (mgr) references tb_emp (eno);
|
|
|
|
|
+-- alter table tb_emp drop foreign key fk_emp_dno;
|
|
|
|
|
+
|
|
|
|
|
+-- restrict - 不允许操作
|
|
|
|
|
+-- cascade - 级联操作
|
|
|
|
|
+-- set null - 设置为null
|
|
|
|
|
+alter table tb_emp add constraint fk_emp_dno foreign key (dno) references tb_dept (dno);
|
|
|
|
|
+-- on delete restrict on update cascade;
|
|
|
|
|
+
|
|
|
|
|
+insert into tb_emp values
|
|
|
|
|
+ (7800, '张三丰', '总裁', null, 9000, 1200, 20),
|
|
|
|
|
+ (2056, '乔峰', '分析师', 7800, 5000, 1500, 20),
|
|
|
|
|
+ (3088, '李莫愁', '设计师', 2056, 3500, 800, 20),
|
|
|
|
|
+ (3211, '张无忌', '程序员', 2056, 3200, null, 20),
|
|
|
|
|
+ (3233, '丘处机', '程序员', 2056, 3400, null, 20),
|
|
|
|
|
+ (3251, '张翠山', '程序员', 2056, 4000, null, 20),
|
|
|
|
|
+ (5566, '宋远桥', '会计师', 7800, 4000, 1000, 10),
|
|
|
|
|
+ (5234, '郭靖', '出纳', 5566, 2000, null, 10),
|
|
|
|
|
+ (3344, '黄蓉', '销售主管', 7800, 3000, 800, 30),
|
|
|
|
|
+ (1359, '胡一刀', '销售员', 3344, 1800, 200, 30),
|
|
|
|
|
+ (4466, '苗人凤', '销售员', 3344, 2500, null, 30),
|
|
|
|
|
+ (3244, '欧阳锋', '程序员', 3088, 3200, null, 20),
|
|
|
|
|
+ (3577, '杨过', '会计', 5566, 2200, null, 10),
|
|
|
|
|
+ (3588, '朱九真', '会计', 5566, 2500, null, 10);
|
|
|
|
|
+-- 查询月薪最高的员工姓名和工资 - 子查询/嵌套查询+聚合函数
|
|
|
|
|
+-- select ename, sal from tb_emp order by sal desc limit 1;
|
|
|
|
|
+select ename, sal from tb_emp where sal=(
|
|
|
|
|
+ select max(sal) from tb_emp
|
|
|
|
|
+);
|
|
|
|
|
+-- 查询员工的姓名和年薪((月薪+补贴)*12)
|
|
|
|
|
+select ename, (sal+ifnull(comm, 0))*12 as 年薪 from tb_emp order by 年薪 desc;
|
|
|
|
|
+-- 查询有员工的部门的编号和人数 - 分组查询+聚合函数
|
|
|
|
|
+select dno, count(dno) from tb_emp group by dno with rollup;
|
|
|
|
|
+-- 查询所有部门的名称和人数 - 子查询+连接查询(左外)
|
|
|
|
|
+select dname as 部门名称, ifnull(total, 0) as 人数 from tb_dept t1 left join (select dno, count(dno) as total from tb_emp group by dno) t2 on t1.dno=t2.dno;
|
|
|
|
|
+-- 查询月薪最高的员工(Boss除外)的姓名和工资 - 空值判断
|
|
|
|
|
+select ename, sal from tb_emp where sal=(select max(sal) from tb_emp where mgr is not null);
|
|
|
|
|
+-- 查询月薪超过平均薪水的员工的姓名和工资
|
|
|
|
|
+select ename, sal from tb_emp where sal>(select avg(sal) as avgsal from tb_emp);
|
|
|
|
|
+-- 查询月薪超过其所在部门平均薪水的员工的姓名、部门编号和工资
|
|
|
|
|
+select ename, t1.dno, sal, round(avgsal, 2) from tb_emp t1 inner join (select dno, avg(sal) as avgsal from tb_emp group by dno) t2 on t1.dno=t2.dno where sal>avgsal;
|
|
|
|
|
+-- 查询部门中薪水最高的人姓名、工资和所在部门名称
|
|
|
|
|
+select ename, sal, dname from (select ename, sal, t1.dno from tb_emp t1 inner join (select dno, max(sal) as maxsal from tb_emp group by dno) t2 on t1.dno=t2.dno where sal=maxsal) t3 inner join tb_dept t4 on t3.dno=t4.dno;
|
|
|
|
|
+-- 查询主管的姓名和职位
|
|
|
|
|
+select ename, job from tb_emp where eno in (select distinct mgr from tb_emp where mgr is not null);
|
|
|
|
|
+-- 通常不推荐使用in或者not in集合运算和distinct去重操作
|
|
|
|
|
+-- 可以考虑用exists或not exists替代掉集合运算和去重操作
|
|
|
|
|
+select ename, job from tb_emp t1 where exists (select 'x' from tb_emp t2 where t1.eno=t2.mgr);
|
|
|
|
|
+-- 查询月薪排名4~6名的员工姓名和工资
|
|
|
|
|
+select ename, sal from tb_emp order by sal desc limit 3, 3;
|
|
|
|
|
+select ename, sal from tb_emp order by sal desc limit 3 offset 3;
|
|
|
|
|
+-- explain生成执行计划
|
|
|
|
|
+explain select eno, ename from tb_emp where eno=7800;
|
|
|
|
|
+explain select eno, ename from tb_emp where eno<>7900;
|
|
|
|
|
+explain select eno, ename from tb_emp where ename='张三丰';
|
|
|
|
|
+explain select eno, ename from tb_emp where ename like '张%';
|
|
|
|
|
+explain select eno, ename from tb_emp where ename like '%张';
|
|
|
|
|
+explain select eno, ename from tb_emp where ename<>'张三丰';
|
|
|
|
|
+-- 视图:查询的快照(简化查询操作)
|
|
|
|
|
+-- 通过视图可以将用户的访问权限限制到某些指定的列上
|
|
|
|
|
+create view vw_emp_dept as
|
|
|
|
|
+select eno, ename, dname from tb_emp t1 inner join tb_dept t2 on t1.dno=t2.dno;
|
|
|
|
|
+select ename, dname from vw_emp_dept;
|
|
|
|
|
+drop view vw_emp_dept;
|
|
|
|
|
+-- 索引(index)
|
|
|
|
|
+-- 索引可以加速查询所以应该在经常用于查询筛选条件的列上建立索引
|
|
|
|
|
+-- 索引会使用额外的存储空间而且会让增删改变得更慢(因为要更新索引)
|
|
|
|
|
+-- 所以不能够滥用索引
|
|
|
|
|
+create index idx_emp_ename on tb_emp (ename);
|
|
|
|
|
+drop index idx_emp_ename on tb_emp;
|
|
|
|
|
+-- 创建存储过程
|
|
|
|
|
+create procedure sp_dept_avg_sal(deptno int, out avgsal float)
|
|
|
|
|
+begin
|
|
|
|
|
+ select avg(sal) into avgsal from tb_emp where dno=deptno;
|
|
|
|
|
+end;
|
|
|
|
|
+-- 调用存储过程
|
|
|
|
|
+call sp_dept_avg_sal(20, @a);
|
|
|
|
|
+-- 通过输出参数取出部门平均工资
|
|
|
|
|
+select @a;
|
|
|
|
|
+-- 删除存储过程
|
|
|
|
|
+drop procedure sp_dept_avg_sal;
|
|
|
|
|
+create trigger tr_dept_update
|
|
|
|
|
+after update on tb_dept for each row
|
|
|
|
|
+begin
|
|
|
|
|
+ update tb_emp set dno=new.dno where dno=old.dno;
|
|
|
|
|
+end;
|
|
|
|
|
+drop trigger tr_dept_update;
|
|
|
|
|
+-- DCL:授予权限(grant to)和召回权限(revoke from)
|
|
|
|
|
+create user 'hellokitty'@'%' identified by '123123';
|
|
|
|
|
+grant all privileges on hrs.* to 'hellokitty'@'%';
|
|
|
|
|
+revoke insert, delete, update on hrs.* from 'hellokitty'@'%';
|
|
|
|
|
+drop user 'hellokitty'@'%'</string>
|
|
|
|
|
+ <string>drop user 'hellokitty'@'%'</string>
|
|
|
|
|
+ <string>-- 注意事项:
|
|
|
|
|
+-- 1. 给数据库和表命名的时候尽量使用全小写
|
|
|
|
|
+-- 2. 作为筛选条件的字符串是否区分大小看设置的校对规则
|
|
|
|
|
+drop database if exists hrs;
|
|
|
|
|
+create database hrs default charset utf8 collate utf8_general_ci;
|
|
|
|
|
+use hrs;
|
|
|
|
|
+drop table if exists tb_emp;
|
|
|
|
|
+drop table if exists tb_dept;
|
|
|
|
|
+-- 3. 数据库中的对象通常会用前缀加以区分
|
|
|
|
|
+-- table / view / index / function / procedure / trigger
|
|
|
|
|
+create table tb_dept
|
|
|
|
|
+(
|
|
|
|
|
+dno int not null comment '编号',
|
|
|
|
|
+dname varchar(10) not null comment '名称',
|
|
|
|
|
+dloc varchar(20) not null comment '所在地',
|
|
|
|
|
+primary key (dno)
|
|
|
|
|
+);
|
|
|
|
|
+-- 批量插入操作
|
|
|
|
|
+insert into tb_dept values
|
|
|
|
|
+ (10, '会计部', '北京'),
|
|
|
|
|
+ (20, '研发部', '成都'),
|
|
|
|
|
+ (30, '销售部', '重庆'),
|
|
|
|
|
+ (40, '运维部', '深圳');
|
|
|
|
|
+create table tb_emp
|
|
|
|
|
+(
|
|
|
|
|
+eno int not null comment '员工编号',
|
|
|
|
|
+ename varchar(20) not null comment '员工姓名',
|
|
|
|
|
+job varchar(20) not null comment '员工职位',
|
|
|
|
|
+mgr int comment '主管编号',
|
|
|
|
|
+sal int not null comment '员工月薪',
|
|
|
|
|
+comm int comment '每月补贴',
|
|
|
|
|
+dno int comment '所在部门编号',
|
|
|
|
|
+primary key (eno)
|
|
|
|
|
+);
|
|
|
|
|
+-- 修改表添加一个列到mgr列的后面
|
|
|
|
|
+-- alter table tb_emp add column hiredate date after mgr;
|
|
|
|
|
+
|
|
|
|
|
+-- 修改表添加一个自参照的外键约束
|
|
|
|
|
+alter table tb_emp add constraint fk_emp_mgr foreign key (mgr) references tb_emp (eno);
|
|
|
|
|
+-- alter table tb_emp drop foreign key fk_emp_dno;
|
|
|
|
|
+
|
|
|
|
|
+-- restrict - 不允许操作
|
|
|
|
|
+-- cascade - 级联操作
|
|
|
|
|
+-- set null - 设置为null
|
|
|
|
|
+alter table tb_emp add constraint fk_emp_dno foreign key (dno) references tb_dept (dno);
|
|
|
|
|
+-- on delete restrict on update cascade;
|
|
|
|
|
+
|
|
|
|
|
+insert into tb_emp values
|
|
|
|
|
+ (7800, '张三丰', '总裁', null, 9000, 1200, 20),
|
|
|
|
|
+ (2056, '乔峰', '分析师', 7800, 5000, 1500, 20),
|
|
|
|
|
+ (3088, '李莫愁', '设计师', 2056, 3500, 800, 20),
|
|
|
|
|
+ (3211, '张无忌', '程序员', 2056, 3200, null, 20),
|
|
|
|
|
+ (3233, '丘处机', '程序员', 2056, 3400, null, 20),
|
|
|
|
|
+ (3251, '张翠山', '程序员', 2056, 4000, null, 20),
|
|
|
|
|
+ (5566, '宋远桥', '会计师', 7800, 4000, 1000, 10),
|
|
|
|
|
+ (5234, '郭靖', '出纳', 5566, 2000, null, 10),
|
|
|
|
|
+ (3344, '黄蓉', '销售主管', 7800, 3000, 800, 30),
|
|
|
|
|
+ (1359, '胡一刀', '销售员', 3344, 1800, 200, 30),
|
|
|
|
|
+ (4466, '苗人凤', '销售员', 3344, 2500, null, 30),
|
|
|
|
|
+ (3244, '欧阳锋', '程序员', 3088, 3200, null, 20),
|
|
|
|
|
+ (3577, '杨过', '会计', 5566, 2200, null, 10),
|
|
|
|
|
+ (3588, '朱九真', '会计', 5566, 2500, null, 10);
|
|
|
|
|
+-- 查询月薪最高的员工姓名和工资 - 子查询/嵌套查询+聚合函数
|
|
|
|
|
+-- select ename, sal from tb_emp order by sal desc limit 1;
|
|
|
|
|
+select ename, sal from tb_emp where sal=(
|
|
|
|
|
+ select max(sal) from tb_emp
|
|
|
|
|
+);
|
|
|
|
|
+-- 查询员工的姓名和年薪((月薪+补贴)*12)
|
|
|
|
|
+select ename, (sal+ifnull(comm, 0))*12 as 年薪 from tb_emp order by 年薪 desc;
|
|
|
|
|
+-- 查询有员工的部门的编号和人数 - 分组查询+聚合函数
|
|
|
|
|
+select dno, count(dno) from tb_emp group by dno with rollup;
|
|
|
|
|
+-- 查询所有部门的名称和人数 - 子查询+连接查询(左外)
|
|
|
|
|
+select dname as 部门名称, ifnull(total, 0) as 人数 from tb_dept t1 left join (select dno, count(dno) as total from tb_emp group by dno) t2 on t1.dno=t2.dno;
|
|
|
|
|
+-- 查询月薪最高的员工(Boss除外)的姓名和工资 - 空值判断
|
|
|
|
|
+select ename, sal from tb_emp where sal=(select max(sal) from tb_emp where mgr is not null);
|
|
|
|
|
+-- 查询月薪超过平均薪水的员工的姓名和工资
|
|
|
|
|
+select ename, sal from tb_emp where sal>(select avg(sal) as avgsal from tb_emp);
|
|
|
|
|
+-- 查询月薪超过其所在部门平均薪水的员工的姓名、部门编号和工资
|
|
|
|
|
+select ename, t1.dno, sal, round(avgsal, 2) from tb_emp t1 inner join (select dno, avg(sal) as avgsal from tb_emp group by dno) t2 on t1.dno=t2.dno where sal>avgsal;
|
|
|
|
|
+-- 查询部门中薪水最高的人姓名、工资和所在部门名称
|
|
|
|
|
+select ename, sal, dname from (select ename, sal, t1.dno from tb_emp t1 inner join (select dno, max(sal) as maxsal from tb_emp group by dno) t2 on t1.dno=t2.dno where sal=maxsal) t3 inner join tb_dept t4 on t3.dno=t4.dno;
|
|
|
|
|
+-- 查询主管的姓名和职位
|
|
|
|
|
+select ename, job from tb_emp where eno in (select distinct mgr from tb_emp where mgr is not null);
|
|
|
|
|
+-- 通常不推荐使用in或者not in集合运算和distinct去重操作
|
|
|
|
|
+-- 可以考虑用exists或not exists替代掉集合运算和去重操作
|
|
|
|
|
+select ename, job from tb_emp t1 where exists (select 'x' from tb_emp t2 where t1.eno=t2.mgr);
|
|
|
|
|
+-- 查询月薪排名4~6名的员工姓名和工资
|
|
|
|
|
+select ename, sal from tb_emp order by sal desc limit 3, 3;
|
|
|
|
|
+select ename, sal from tb_emp order by sal desc limit 3 offset 3;
|
|
|
|
|
+-- explain生成执行计划
|
|
|
|
|
+explain select eno, ename from tb_emp where eno=7800;
|
|
|
|
|
+explain select eno, ename from tb_emp where eno<>7900;
|
|
|
|
|
+explain select eno, ename from tb_emp where ename='张三丰';
|
|
|
|
|
+explain select eno, ename from tb_emp where ename like '张%';
|
|
|
|
|
+explain select eno, ename from tb_emp where ename like '%张';
|
|
|
|
|
+explain select eno, ename from tb_emp where ename<>'张三丰';
|
|
|
|
|
+-- 视图:查询的快照(简化查询操作)
|
|
|
|
|
+-- 通过视图可以将用户的访问权限限制到某些指定的列上
|
|
|
|
|
+create view vw_emp_dept as
|
|
|
|
|
+select eno, ename, dname from tb_emp t1 inner join tb_dept t2 on t1.dno=t2.dno;
|
|
|
|
|
+select ename, dname from vw_emp_dept;
|
|
|
|
|
+drop view vw_emp_dept;
|
|
|
|
|
+-- 索引(index)
|
|
|
|
|
+-- 索引可以加速查询所以应该在经常用于查询筛选条件的列上建立索引
|
|
|
|
|
+-- 索引会使用额外的存储空间而且会让增删改变得更慢(因为要更新索引)
|
|
|
|
|
+-- 所以不能够滥用索引
|
|
|
|
|
+create index idx_emp_ename on tb_emp (ename);
|
|
|
|
|
+drop index idx_emp_ename on tb_emp;
|
|
|
|
|
+-- 创建存储过程
|
|
|
|
|
+create procedure sp_dept_avg_sal(deptno int, out avgsal float)
|
|
|
|
|
+begin
|
|
|
|
|
+ select avg(sal) into avgsal from tb_emp where dno=deptno;
|
|
|
|
|
+end;
|
|
|
|
|
+-- 调用存储过程
|
|
|
|
|
+call sp_dept_avg_sal(20, @a);
|
|
|
|
|
+-- 通过输出参数取出部门平均工资
|
|
|
|
|
+select @a;
|
|
|
|
|
+-- 删除存储过程
|
|
|
|
|
+drop procedure sp_dept_avg_sal;
|
|
|
|
|
+create trigger tr_dept_update
|
|
|
|
|
+after update on tb_dept for each row
|
|
|
|
|
+begin
|
|
|
|
|
+ update tb_emp set dno=new.dno where dno=old.dno;
|
|
|
|
|
+end;
|
|
|
|
|
+drop trigger tr_dept_update;
|
|
|
|
|
+-- DCL:授予权限(grant to)和召回权限(revoke from)
|
|
|
|
|
+create user 'hellokitty'@'%' identified by '123123'</string>
|
|
|
|
|
+ <string>-- 注意事项:
|
|
|
|
|
+-- 1. 给数据库和表命名的时候尽量使用全小写
|
|
|
|
|
+-- 2. 作为筛选条件的字符串是否区分大小看设置的校对规则
|
|
|
|
|
+drop database if exists hrs;
|
|
|
|
|
+create database hrs default charset utf8 collate utf8_general_ci;
|
|
|
|
|
+use hrs;
|
|
|
|
|
+drop table if exists tb_emp;
|
|
|
|
|
+drop table if exists tb_dept;
|
|
|
|
|
+-- 3. 数据库中的对象通常会用前缀加以区分
|
|
|
|
|
+-- table / view / index / function / procedure / trigger
|
|
|
|
|
+create table tb_dept
|
|
|
|
|
+(
|
|
|
|
|
+dno int not null comment '编号',
|
|
|
|
|
+dname varchar(10) not null comment '名称',
|
|
|
|
|
+dloc varchar(20) not null comment '所在地',
|
|
|
|
|
+primary key (dno)
|
|
|
|
|
+);
|
|
|
|
|
+-- 批量插入操作
|
|
|
|
|
+insert into tb_dept values
|
|
|
|
|
+ (10, '会计部', '北京'),
|
|
|
|
|
+ (20, '研发部', '成都'),
|
|
|
|
|
+ (30, '销售部', '重庆'),
|
|
|
|
|
+ (40, '运维部', '深圳');
|
|
|
|
|
+create table tb_emp
|
|
|
|
|
+(
|
|
|
|
|
+eno int not null comment '员工编号',
|
|
|
|
|
+ename varchar(20) not null comment '员工姓名',
|
|
|
|
|
+job varchar(20) not null comment '员工职位',
|
|
|
|
|
+mgr int comment '主管编号',
|
|
|
|
|
+sal int not null comment '员工月薪',
|
|
|
|
|
+comm int comment '每月补贴',
|
|
|
|
|
+dno int comment '所在部门编号',
|
|
|
|
|
+primary key (eno)
|
|
|
|
|
+);
|
|
|
|
|
+-- 修改表添加一个列到mgr列的后面
|
|
|
|
|
+-- alter table tb_emp add column hiredate date after mgr;
|
|
|
|
|
+
|
|
|
|
|
+-- 修改表添加一个自参照的外键约束
|
|
|
|
|
+alter table tb_emp add constraint fk_emp_mgr foreign key (mgr) references tb_emp (eno);
|
|
|
|
|
+-- alter table tb_emp drop foreign key fk_emp_dno;
|
|
|
|
|
+
|
|
|
|
|
+-- restrict - 不允许操作
|
|
|
|
|
+-- cascade - 级联操作
|
|
|
|
|
+-- set null - 设置为null
|
|
|
|
|
+alter table tb_emp add constraint fk_emp_dno foreign key (dno) references tb_dept (dno);
|
|
|
|
|
+-- on delete restrict on update cascade;
|
|
|
|
|
+
|
|
|
|
|
+insert into tb_emp values
|
|
|
|
|
+ (7800, '张三丰', '总裁', null, 9000, 1200, 20),
|
|
|
|
|
+ (2056, '乔峰', '分析师', 7800, 5000, 1500, 20),
|
|
|
|
|
+ (3088, '李莫愁', '设计师', 2056, 3500, 800, 20),
|
|
|
|
|
+ (3211, '张无忌', '程序员', 2056, 3200, null, 20),
|
|
|
|
|
+ (3233, '丘处机', '程序员', 2056, 3400, null, 20),
|
|
|
|
|
+ (3251, '张翠山', '程序员', 2056, 4000, null, 20),
|
|
|
|
|
+ (5566, '宋远桥', '会计师', 7800, 4000, 1000, 10),
|
|
|
|
|
+ (5234, '郭靖', '出纳', 5566, 2000, null, 10),
|
|
|
|
|
+ (3344, '黄蓉', '销售主管', 7800, 3000, 800, 30),
|
|
|
|
|
+ (1359, '胡一刀', '销售员', 3344, 1800, 200, 30),
|
|
|
|
|
+ (4466, '苗人凤', '销售员', 3344, 2500, null, 30),
|
|
|
|
|
+ (3244, '欧阳锋', '程序员', 3088, 3200, null, 20),
|
|
|
|
|
+ (3577, '杨过', '会计', 5566, 2200, null, 10),
|
|
|
|
|
+ (3588, '朱九真', '会计', 5566, 2500, null, 10);
|
|
|
|
|
+-- 查询月薪最高的员工姓名和工资 - 子查询/嵌套查询+聚合函数
|
|
|
|
|
+-- select ename, sal from tb_emp order by sal desc limit 1;
|
|
|
|
|
+select ename, sal from tb_emp where sal=(
|
|
|
|
|
+ select max(sal) from tb_emp
|
|
|
|
|
+);
|
|
|
|
|
+-- 查询员工的姓名和年薪((月薪+补贴)*12)
|
|
|
|
|
+select ename, (sal+ifnull(comm, 0))*12 as 年薪 from tb_emp order by 年薪 desc;
|
|
|
|
|
+-- 查询有员工的部门的编号和人数 - 分组查询+聚合函数
|
|
|
|
|
+select dno, count(dno) from tb_emp group by dno with rollup;
|
|
|
|
|
+-- 查询所有部门的名称和人数 - 子查询+连接查询(左外)
|
|
|
|
|
+select dname as 部门名称, ifnull(total, 0) as 人数 from tb_dept t1 left join (select dno, count(dno) as total from tb_emp group by dno) t2 on t1.dno=t2.dno;
|
|
|
|
|
+-- 查询月薪最高的员工(Boss除外)的姓名和工资 - 空值判断
|
|
|
|
|
+select ename, sal from tb_emp where sal=(select max(sal) from tb_emp where mgr is not null);
|
|
|
|
|
+-- 查询月薪超过平均薪水的员工的姓名和工资
|
|
|
|
|
+select ename, sal from tb_emp where sal>(select avg(sal) as avgsal from tb_emp);
|
|
|
|
|
+-- 查询月薪超过其所在部门平均薪水的员工的姓名、部门编号和工资
|
|
|
|
|
+select ename, t1.dno, sal, round(avgsal, 2) from tb_emp t1 inner join (select dno, avg(sal) as avgsal from tb_emp group by dno) t2 on t1.dno=t2.dno where sal>avgsal;
|
|
|
|
|
+-- 查询部门中薪水最高的人姓名、工资和所在部门名称
|
|
|
|
|
+select ename, sal, dname from (select ename, sal, t1.dno from tb_emp t1 inner join (select dno, max(sal) as maxsal from tb_emp group by dno) t2 on t1.dno=t2.dno where sal=maxsal) t3 inner join tb_dept t4 on t3.dno=t4.dno;
|
|
|
|
|
+-- 查询主管的姓名和职位
|
|
|
|
|
+select ename, job from tb_emp where eno in (select distinct mgr from tb_emp where mgr is not null);
|
|
|
|
|
+-- 通常不推荐使用in或者not in集合运算和distinct去重操作
|
|
|
|
|
+-- 可以考虑用exists或not exists替代掉集合运算和去重操作
|
|
|
|
|
+select ename, job from tb_emp t1 where exists (select 'x' from tb_emp t2 where t1.eno=t2.mgr);
|
|
|
|
|
+-- 查询月薪排名4~6名的员工姓名和工资
|
|
|
|
|
+select ename, sal from tb_emp order by sal desc limit 3, 3;
|
|
|
|
|
+select ename, sal from tb_emp order by sal desc limit 3 offset 3;
|
|
|
|
|
+-- explain生成执行计划
|
|
|
|
|
+explain select eno, ename from tb_emp where eno=7800;
|
|
|
|
|
+explain select eno, ename from tb_emp where eno<>7900;
|
|
|
|
|
+explain select eno, ename from tb_emp where ename='张三丰';
|
|
|
|
|
+explain select eno, ename from tb_emp where ename like '张%';
|
|
|
|
|
+explain select eno, ename from tb_emp where ename like '%张';
|
|
|
|
|
+explain select eno, ename from tb_emp where ename<>'张三丰';
|
|
|
|
|
+-- 视图:查询的快照(简化查询操作)
|
|
|
|
|
+-- 通过视图可以将用户的访问权限限制到某些指定的列上
|
|
|
|
|
+create view vw_emp_dept as
|
|
|
|
|
+select eno, ename, dname from tb_emp t1 inner join tb_dept t2 on t1.dno=t2.dno;
|
|
|
|
|
+select ename, dname from vw_emp_dept;
|
|
|
|
|
+drop view vw_emp_dept;
|
|
|
|
|
+-- 索引(index)
|
|
|
|
|
+-- 索引可以加速查询所以应该在经常用于查询筛选条件的列上建立索引
|
|
|
|
|
+-- 索引会使用额外的存储空间而且会让增删改变得更慢(因为要更新索引)
|
|
|
|
|
+-- 所以不能够滥用索引
|
|
|
|
|
+create index idx_emp_ename on tb_emp (ename);
|
|
|
|
|
+drop index idx_emp_ename on tb_emp;
|
|
|
|
|
+-- 创建存储过程
|
|
|
|
|
+create procedure sp_dept_avg_sal(deptno int, out avgsal float)
|
|
|
|
|
+begin
|
|
|
|
|
+ select avg(sal) into avgsal from tb_emp where dno=deptno;
|
|
|
|
|
+end;
|
|
|
|
|
+-- 调用存储过程
|
|
|
|
|
+call sp_dept_avg_sal(20, @a);
|
|
|
|
|
+-- 通过输出参数取出部门平均工资
|
|
|
|
|
+select @a;
|
|
|
|
|
+-- 删除存储过程
|
|
|
|
|
+drop procedure sp_dept_avg_sal;
|
|
|
|
|
+-- 触发器:在执行增删改操作时可以触发其他的级联操作,但是有可能导致“锁表”现象,实际开发中应该尽量避免使用触发器
|
|
|
|
|
+update tb_dept set dno=11 where dno=10</string>
|
|
|
|
|
+ <string>-- 注意事项:
|
|
|
|
|
+-- 1. 给数据库和表命名的时候尽量使用全小写
|
|
|
|
|
+-- 2. 作为筛选条件的字符串是否区分大小看设置的校对规则
|
|
|
|
|
+drop database if exists hrs;
|
|
|
|
|
+create database hrs default charset utf8 collate utf8_general_ci;
|
|
|
|
|
+use hrs;
|
|
|
|
|
+drop table if exists tb_emp;
|
|
|
|
|
+drop table if exists tb_dept;
|
|
|
|
|
+-- 3. 数据库中的对象通常会用前缀加以区分
|
|
|
|
|
+-- table / view / index / function / procedure / trigger
|
|
|
|
|
+create table tb_dept
|
|
|
|
|
+(
|
|
|
|
|
+dno int not null comment '编号',
|
|
|
|
|
+dname varchar(10) not null comment '名称',
|
|
|
|
|
+dloc varchar(20) not null comment '所在地',
|
|
|
|
|
+primary key (dno)
|
|
|
|
|
+);
|
|
|
|
|
+-- 批量插入操作
|
|
|
|
|
+insert into tb_dept values
|
|
|
|
|
+ (10, '会计部', '北京'),
|
|
|
|
|
+ (20, '研发部', '成都'),
|
|
|
|
|
+ (30, '销售部', '重庆'),
|
|
|
|
|
+ (40, '运维部', '深圳');
|
|
|
|
|
+create table tb_emp
|
|
|
|
|
+(
|
|
|
|
|
+eno int not null comment '员工编号',
|
|
|
|
|
+ename varchar(20) not null comment '员工姓名',
|
|
|
|
|
+job varchar(20) not null comment '员工职位',
|
|
|
|
|
+mgr int comment '主管编号',
|
|
|
|
|
+sal int not null comment '员工月薪',
|
|
|
|
|
+comm int comment '每月补贴',
|
|
|
|
|
+dno int comment '所在部门编号',
|
|
|
|
|
+primary key (eno)
|
|
|
|
|
+);
|
|
|
|
|
+-- 修改表添加一个列到mgr列的后面
|
|
|
|
|
+-- alter table tb_emp add column hiredate date after mgr;
|
|
|
|
|
+
|
|
|
|
|
+-- 修改表添加一个自参照的外键约束
|
|
|
|
|
+alter table tb_emp add constraint fk_emp_mgr foreign key (mgr) references tb_emp (eno);
|
|
|
|
|
+alter table tb_emp drop foreign key fk_emp_dno</string>
|
|
|
|
|
+ <string>revoke insert, delete, update on hrs.* from 'hellokitty'@'%'</string>
|
|
|
|
|
+ <string>grant all privileges on hrs.* to 'hellokitty'@'%'</string>
|
|
|
|
|
+ <string>-- DCL:授予权限(grant to)和召回权限(revoke from)
|
|
|
|
|
+create user 'hellokitty'@'%' identified by '123123'</string>
|
|
|
|
|
+ <string>delete from tb_dept where dno=11</string>
|
|
|
|
|
+ <string>update tb_dept set dno=11 where dno=10</string>
|
|
|
|
|
+ <string>alter table tb_emp add constraint fk_emp_dno foreign key (dno) references tb_dept (dno) on delete cascade on update cascade</string>
|
|
|
|
|
+ <string>alter table tb_emp drop foreign key fk_emp_dno</string>
|
|
|
|
|
+ <string>delete from tb_dept where dno=10</string>
|
|
|
|
|
+ </array>
|
|
|
|
|
+ <key>rdbms_type</key>
|
|
|
|
|
+ <string>mysql</string>
|
|
|
|
|
+ <key>rdbms_version</key>
|
|
|
|
|
+ <string>5.5.60-MariaDB</string>
|
|
|
|
|
+ <key>version</key>
|
|
|
|
|
+ <integer>1</integer>
|
|
|
|
|
+</dict>
|
|
|
|
|
+</plist>
|