| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877 |
- <?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>
|