人力资源管理.spf 37.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 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&gt;(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&gt;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&lt;&gt;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&lt;&gt;'张三丰';

-- 视图:查询的快照(简化查询操作)
-- 通过视图可以将用户的访问权限限制到某些指定的列上
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&gt;(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&gt;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&lt;&gt;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&lt;&gt;'张三丰';
-- 视图:查询的快照(简化查询操作)
-- 通过视图可以将用户的访问权限限制到某些指定的列上
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&gt;(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&gt;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&lt;&gt;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&lt;&gt;'张三丰';
-- 视图:查询的快照(简化查询操作)
-- 通过视图可以将用户的访问权限限制到某些指定的列上
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&gt;(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&gt;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&lt;&gt;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&lt;&gt;'张三丰';
-- 视图:查询的快照(简化查询操作)
-- 通过视图可以将用户的访问权限限制到某些指定的列上
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&gt;(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&gt;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&lt;&gt;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&lt;&gt;'张三丰';
-- 视图:查询的快照(简化查询操作)
-- 通过视图可以将用户的访问权限限制到某些指定的列上
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>