-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql-hr-schema.sql
2643 lines (2403 loc) · 30.6 KB
/
mysql-hr-schema.sql
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
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* You can copy, modify, distribute and perform the work, even for commercial purposes,
* all without asking permission.
*
* @Author: Andrei N. CIOBANU
*/
DROP SCHEMA
IF EXISTS hr;
CREATE SCHEMA hr COLLATE = utf8mb4_general_ci;
USE hr;
/* ***************************************************************
***************************CREATING TABLES************************
**************************************************************** */
CREATE TABLE regions (
region_id INT UNSIGNED NOT NULL,
region_name VARCHAR(25),
PRIMARY KEY (region_id)
);
CREATE TABLE countries (
country_id CHAR(2) NOT NULL,
country_name VARCHAR(40),
region_id INT UNSIGNED NOT NULL,
PRIMARY KEY (country_id)
);
CREATE TABLE locations (
location_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
street_address VARCHAR(40),
postal_code VARCHAR(12),
city VARCHAR(30) NOT NULL,
state_province VARCHAR(25),
country_id CHAR(2) NOT NULL,
PRIMARY KEY (location_id)
);
CREATE TABLE departments (
department_id INT UNSIGNED NOT NULL,
department_name VARCHAR(30) NOT NULL,
manager_id INT UNSIGNED,
location_id INT UNSIGNED,
PRIMARY KEY (department_id)
);
CREATE TABLE jobs (
job_id VARCHAR(10) NOT NULL,
job_title VARCHAR(35) NOT NULL,
min_salary DECIMAL(8, 0), CHECK (min_salary >= 0),
max_salary DECIMAL(8, 0), CHECK (max_salary >= 0),
PRIMARY KEY (job_id)
);
CREATE TABLE employees (
employee_id INT UNSIGNED NOT NULL,
first_name VARCHAR(20),
last_name VARCHAR(25) NOT NULL,
email VARCHAR(25) NOT NULL,
phone_number VARCHAR(20),
hire_date DATE NOT NULL,
job_id VARCHAR(10) NOT NULL,
salary DECIMAL(8, 2) NOT NULL,
commission_pct DECIMAL(2, 2),
manager_id INT UNSIGNED,
department_id INT UNSIGNED,
PRIMARY KEY (employee_id)
);
CREATE TABLE job_history (
employee_id INT UNSIGNED NOT NULL,
start_date DATE NOT NULL,
end_date DATE NOT NULL,
job_id VARCHAR(10) NOT NULL,
department_id INT UNSIGNED NOT NULL
);
ALTER TABLE job_history ADD UNIQUE INDEX (
employee_id,
start_date
);
CREATE VIEW emp_details_view
AS
SELECT e.employee_id,
e.job_id,
e.manager_id,
e.department_id,
d.location_id,
l.country_id,
e.first_name,
e.last_name,
e.salary,
e.commission_pct,
d.department_name,
j.job_title,
l.city,
l.state_province,
c.country_name,
r.region_name
FROM employees e,
departments d,
jobs j,
locations l,
countries c,
regions r
WHERE e.department_id = d.department_id
AND d.location_id = l.location_id
AND l.country_id = c.country_id
AND c.region_id = r.region_id
AND j.job_id = e.job_id;
/* ***************************************************************
***************************INSERTING DATA*************************
**************************************************************** */
INSERT INTO regions
VALUES (
1,
'Europe'
);
INSERT INTO regions
VALUES (
2,
'Americas'
);
INSERT INTO regions
VALUES (
3,
'Asia'
);
INSERT INTO regions
VALUES (
4,
'Middle East and Africa'
);
COMMIT;
INSERT INTO countries
VALUES (
'IT',
'Italy',
1
);
INSERT INTO countries
VALUES (
'JP',
'Japan',
3
);
INSERT INTO countries
VALUES (
'US',
'United States of America',
2
);
INSERT INTO countries
VALUES (
'CA',
'Canada',
2
);
INSERT INTO countries
VALUES (
'CN',
'China',
3
);
INSERT INTO countries
VALUES (
'IN',
'India',
3
);
INSERT INTO countries
VALUES (
'AU',
'Australia',
3
);
INSERT INTO countries
VALUES (
'ZW',
'Zimbabwe',
4
);
INSERT INTO countries
VALUES (
'SG',
'Singapore',
3
);
INSERT INTO countries
VALUES (
'UK',
'United Kingdom',
1
);
INSERT INTO countries
VALUES (
'FR',
'France',
1
);
INSERT INTO countries
VALUES (
'DE',
'Germany',
1
);
INSERT INTO countries
VALUES (
'ZM',
'Zambia',
4
);
INSERT INTO countries
VALUES (
'EG',
'Egypt',
4
);
INSERT INTO countries
VALUES (
'BR',
'Brazil',
2
);
INSERT INTO countries
VALUES (
'CH',
'Switzerland',
1
);
INSERT INTO countries
VALUES (
'NL',
'Netherlands',
1
);
INSERT INTO countries
VALUES (
'MX',
'Mexico',
2
);
INSERT INTO countries
VALUES (
'KW',
'Kuwait',
4
);
INSERT INTO countries
VALUES (
'IL',
'Israel',
4
);
INSERT INTO countries
VALUES (
'DK',
'Denmark',
1
);
INSERT INTO countries
VALUES (
'HK',
'HongKong',
3
);
INSERT INTO countries
VALUES (
'NG',
'Nigeria',
4
);
INSERT INTO countries
VALUES (
'AR',
'Argentina',
2
);
INSERT INTO countries
VALUES (
'BE',
'Belgium',
1
);
COMMIT;
INSERT INTO locations
VALUES (
1000,
'1297 Via Cola di Rie',
'00989',
'Roma',
NULL,
'IT'
);
INSERT INTO locations
VALUES (
1100,
'93091 Calle della Testa',
'10934',
'Venice',
NULL,
'IT'
);
INSERT INTO locations
VALUES (
1200,
'2017 Shinjuku-ku',
'1689',
'Tokyo',
'Tokyo Prefecture',
'JP'
);
INSERT INTO locations
VALUES (
1300,
'9450 Kamiya-cho',
'6823',
'Hiroshima',
NULL,
'JP'
);
INSERT INTO locations
VALUES (
1400,
'2014 Jabberwocky Rd',
'26192',
'Southlake',
'Texas',
'US'
);
INSERT INTO locations
VALUES (
1500,
'2011 Interiors Blvd',
'99236',
'South San Francisco',
'California',
'US'
);
INSERT INTO locations
VALUES (
1600,
'2007 Zagora St',
'50090',
'South Brunswick',
'New Jersey',
'US'
);
INSERT INTO locations
VALUES (
1700,
'2004 Charade Rd',
'98199',
'Seattle',
'Washington',
'US'
);
INSERT INTO locations
VALUES (
1800,
'147 Spadina Ave',
'M5V 2L7',
'Toronto',
'Ontario',
'CA'
);
INSERT INTO locations
VALUES (
1900,
'6092 Boxwood St',
'YSW 9T2',
'Whitehorse',
'Yukon',
'CA'
);
INSERT INTO locations
VALUES (
2000,
'40-5-12 Laogianggen',
'190518',
'Beijing',
NULL,
'CN'
);
INSERT INTO locations
VALUES (
2100,
'1298 Vileparle (E)',
'490231',
'Bombay',
'Maharashtra',
'IN'
);
INSERT INTO locations
VALUES (
2200,
'12-98 Victoria Street',
'2901',
'Sydney',
'New South Wales',
'AU'
);
INSERT INTO locations
VALUES (
2300,
'198 Clementi North',
'540198',
'Singapore',
NULL,
'SG'
);
INSERT INTO locations
VALUES (
2400,
'8204 Arthur St',
NULL,
'London',
NULL,
'UK'
);
INSERT INTO locations
VALUES (
2500,
'Magdalen Centre, The Oxford Science Park',
'OX9 9ZB',
'Oxford',
'Oxford',
'UK'
);
INSERT INTO locations
VALUES (
2600,
'9702 Chester Road',
'09629850293',
'Stretford',
'Manchester',
'UK'
);
INSERT INTO locations
VALUES (
2700,
'Schwanthalerstr. 7031',
'80925',
'Munich',
'Bavaria',
'DE'
);
INSERT INTO locations
VALUES (
2800,
'Rua Frei Caneca 1360 ',
'01307-002',
'Sao Paulo',
'Sao Paulo',
'BR'
);
INSERT INTO locations
VALUES (
2900,
'20 Rue des Corps-Saints',
'1730',
'Geneva',
'Geneve',
'CH'
);
INSERT INTO locations
VALUES (
3000,
'Murtenstrasse 921',
'3095',
'Bern',
'BE',
'CH'
);
INSERT INTO locations
VALUES (
3100,
'Pieter Breughelstraat 837',
'3029SK',
'Utrecht',
'Utrecht',
'NL'
);
INSERT INTO locations
VALUES (
3200,
'Mariano Escobedo 9991',
'11932',
'Mexico City',
'Distrito Federal,',
'MX'
);
COMMIT;
SET FOREIGN_KEY_CHECKS = 0;
INSERT INTO departments
VALUES (
10,
'Administration',
200,
1700
);
INSERT INTO departments
VALUES (
20,
'Marketing',
201,
1800
);
INSERT INTO departments
VALUES (
30,
'Purchasing',
114,
1700
);
INSERT INTO departments
VALUES (
40,
'Human Resources',
203,
2400
);
INSERT INTO departments
VALUES (
50,
'Shipping',
121,
1500
);
INSERT INTO departments
VALUES (
60,
'IT',
103,
1400
);
INSERT INTO departments
VALUES (
70,
'Public Relations',
204,
2700
);
INSERT INTO departments
VALUES (
80,
'Sales',
145,
2500
);
INSERT INTO departments
VALUES (
90,
'Executive',
100,
1700
);
INSERT INTO departments
VALUES (
100,
'Finance',
108,
1700
);
INSERT INTO departments
VALUES (
110,
'Accounting',
205,
1700
);
INSERT INTO departments
VALUES (
120,
'Treasury',
NULL,
1700
);
INSERT INTO departments
VALUES (
130,
'Corporate Tax',
NULL,
1700
);
INSERT INTO departments
VALUES (
140,
'Control And Credit',
NULL,
1700
);
INSERT INTO departments
VALUES (
150,
'Shareholder Services',
NULL,
1700
);
INSERT INTO departments
VALUES (
160,
'Benefits',
NULL,
1700
);
INSERT INTO departments
VALUES (
170,
'Manufacturing',
NULL,
1700
);
INSERT INTO departments
VALUES (
180,
'Construction',
NULL,
1700
);
INSERT INTO departments
VALUES (
190,
'Contracting',
NULL,
1700
);
INSERT INTO departments
VALUES (
200,
'Operations',
NULL,
1700
);
INSERT INTO departments
VALUES (
210,
'IT Support',
NULL,
1700
);
INSERT INTO departments
VALUES (
220,
'NOC',
NULL,
1700
);
INSERT INTO departments
VALUES (
230,
'IT Helpdesk',
NULL,
1700
);
INSERT INTO departments
VALUES (
240,
'Government Sales',
NULL,
1700
);
INSERT INTO departments
VALUES (
250,
'Retail Sales',
NULL,
1700
);
INSERT INTO departments
VALUES (
260,
'Recruiting',
NULL,
1700
);
INSERT INTO departments
VALUES (
270,
'Payroll',
NULL,
1700
);
SET FOREIGN_KEY_CHECKS = 1;
COMMIT;
INSERT INTO jobs
VALUES (
'AD_PRES',
'President',
20000,
40000
);
INSERT INTO jobs
VALUES (
'AD_VP',
'Administration Vice President',
15000,
30000
);
INSERT INTO jobs
VALUES (
'AD_ASST',
'Administration Assistant',
3000,
6000
);
INSERT INTO jobs
VALUES (
'FI_MGR',
'Finance Manager',
8200,
16000
);
INSERT INTO jobs
VALUES (
'FI_ACCOUNT',
'Accountant',
4200,
9000
);
INSERT INTO jobs
VALUES (
'AC_MGR',
'Accounting Manager',
8200,
16000
);
INSERT INTO jobs
VALUES (
'AC_ACCOUNT',
'Public Accountant',
4200,
9000
);
INSERT INTO jobs
VALUES (
'SA_MAN',
'Sales Manager',
10000,
20000
);
INSERT INTO jobs
VALUES (
'SA_REP',
'Sales Representative',
6000,
12000
);
INSERT INTO jobs
VALUES (
'PU_MAN',
'Purchasing Manager',
8000,
15000
);
INSERT INTO jobs
VALUES (
'PU_CLERK',
'Purchasing Clerk',
2500,
5500
);
INSERT INTO jobs
VALUES (
'ST_MAN',
'Stock Manager',
5500,
8500
);
INSERT INTO jobs
VALUES (
'ST_CLERK',
'Stock Clerk',
2000,
5000
);
INSERT INTO jobs
VALUES (
'SH_CLERK',
'Shipping Clerk',
2500,
5500
);
INSERT INTO jobs
VALUES (
'IT_PROG',
'Programmer',
4000,
10000
);
INSERT INTO jobs
VALUES (
'MK_MAN',
'Marketing Manager',
9000,
15000
);
INSERT INTO jobs
VALUES (
'MK_REP',
'Marketing Representative',
4000,
9000
);
INSERT INTO jobs
VALUES (
'HR_REP',
'Human Resources Representative',
4000,
9000
);
INSERT INTO jobs
VALUES (
'PR_REP',
'Public Relations Representative',
4500,
10500
);
COMMIT;
INSERT INTO employees
VALUES (
100,
'Steven',
'King',
'SKING',
'515.123.4567',
STR_TO_DATE('17-JUN-1987', '%d-%M-%Y'),
'AD_PRES',
24000,
NULL,
NULL,
90
);
INSERT INTO employees
VALUES (
101,
'Neena',
'Kochhar',
'NKOCHHAR',
'515.123.4568',
STR_TO_DATE('21-SEP-1989', '%d-%M-%Y'),
'AD_VP',
17000,
NULL,
100,
90
);
INSERT INTO employees
VALUES (
102,
'Lex',
'De Haan',
'LDEHAAN',
'515.123.4569',
STR_TO_DATE('13-JAN-1993', '%d-%M-%Y'),
'AD_VP',
17000,
NULL,
100,
90
);
INSERT INTO employees
VALUES (
103,
'Alexander',
'Hunold',
'AHUNOLD',
'590.423.4567',
STR_TO_DATE('03-JAN-1990', '%d-%M-%Y'),
'IT_PROG',
9000,
NULL,
102,
60
);
INSERT INTO employees
VALUES (
104,
'Bruce',
'Ernst',
'BERNST',
'590.423.4568',
STR_TO_DATE('21-MAY-1991', '%d-%M-%Y'),
'IT_PROG',
6000,