-
Notifications
You must be signed in to change notification settings - Fork 0
/
GarageScript.sql
2413 lines (1723 loc) · 77.5 KB
/
GarageScript.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
-----------------------------------------------
------------------/* SUBJECT INFO */-----------
-----------------------------------------------
--Subject: CSY2038 Assignment 2: Roddys Garage
-----------------------------------------------
------------------/* DATABASE INFO */----------
-----------------------------------------------
--TYPES: 5
--VARRAYS: 7
--NESTED TABLES: 1
--TABLES: 6
--QUERIES: 16
--FUNCTIONS: 12
--PROCEDURES: 13 + 8 (with Cursors)
--TRIGGERS: 10 + 3 (with Cursors)
--CURSORS: 11
-----------------------------------------------
------------------/* DROP CONSTRAINTS */-------
-----------------------------------------------
ALTER TABLE customers
DROP CONSTRAINT ck_customer_firstname;
ALTER TABLE customers
DROP CONSTRAINT ck_customer_surname;
ALTER TABLE mechanics
DROP CONSTRAINT ck_mechanic_firstname;
ALTER TABLE mechanics
DROP CONSTRAINT ck_mechanic_surname;
ALTER TABLE parts
DROP CONSTRAINT ck_part_name;
ALTER TABLE customers
DROP CONSTRAINT ck_customer_gender;
ALTER TABLE bookings
DROP CONSTRAINT ck_bookings_fixed;
ALTER TABLE parts
DROP CONSTRAINT ck_ordered_status;
ALTER TABLE bookings
DROP CONSTRAINT fk_b_customer_cars;
ALTER TABLE bookings
DROP CONSTRAINT fk_b_mechanics;
ALTER TABLE customer_cars
DROP CONSTRAINT fk_c_customers;
ALTER TABLE replaced_parts
DROP CONSTRAINT fk_r_bookings;
ALTER TABLE replaced_parts
DROP CONSTRAINT fk_r_parts;
ALTER TABLE customers
DROP CONSTRAINT pk_customers;
ALTER TABLE mechanics
DROP CONSTRAINT pk_mechanics;
ALTER TABLE bookings
DROP CONSTRAINT pk_bookings;
ALTER TABLE customer_cars
DROP CONSTRAINT pk_customer_cars;
ALTER TABLE parts
DROP CONSTRAINT pk_parts;
ALTER TABLE replaced_parts
DROP CONSTRAINT pk_replaced_parts;
-----------------------------------------------
------------------/* DROP TABLES */------------
-----------------------------------------------
DROP SEQUENCE customers_seq;
DROP SEQUENCE mechanics_seq;
DROP SEQUENCE bookings_seq;
DROP SEQUENCE customer_cars_seq;
DROP SEQUENCE parts_seq;
DROP TABLE customers;
DROP TABLE mechanics;
DROP TABLE bookings;
DROP TABLE customer_cars;
DROP TABLE parts;
DROP TABLE replaced_parts;
DROP TABLE addresses;
DROP TABLE log_table_users;
DROP TABLE log_table_mechanics;
DROP TABLE log_table_creations;
-----------------------------------------------
------------------/* DROP TYPES */-------------
-----------------------------------------------
DROP TYPE compatibility_table_type;
DROP TYPE address_type;
DROP TYPE diagnosis_type;
DROP TYPE characteristics_type;
DROP TYPE part_characteristics_type;
DROP TYPE manufacturers_varray_type;
DROP TYPE models_varray_type;
DROP TYPE years_made_varray_type;
DROP TYPE displacements_varray_type;
DROP TYPE colors_varray_type;
DROP TYPE part_manufacturers_varray_type;
DROP TYPE suppliers_varray_type;
-----------------------------------------------
------------------/* DROP FUNCTIONS */---------
-----------------------------------------------
DROP FUNCTION func_bookings;
DROP FUNCTION func_username;
DROP FUNCTION func_salary;
DROP FUNCTION func_commission;
DROP FUNCTION func_total_cost;
DROP FUNCTION func_totalcost_byname;
DROP FUNCTION func_rating;
DROP FUNCTION func_car_manufacturer;
DROP FUNCTION func_car_model;
DROP FUNCTION func_car_yearmade;
DROP FUNCTION func_car_displacements;
DROP FUNCTION func_booking_cost;
-----------------------------------------------
------------------/* DROP PROCEDURES */--------
-----------------------------------------------
DROP PROCEDURE proc_bookings_func;
DROP PROCEDURE proc_showinfo_func;
DROP PROCEDURE proc_add_customer;
DROP PROCEDURE proc_evaluation_func;
DROP PROCEDURE proc_evaluationbyname_func;
DROP PROCEDURE proc_notfixed;
DROP PROCEDURE proc_parts_car;
DROP PROCEDURE proc_manufacturers;
DROP PROCEDURE proc_bad_manufacturers;
DROP PROCEDURE proc_parts_notordered;
DROP PROCEDURE proc_booking_cost_func;
DROP PROCEDURE proc_mechanics_car;
DROP PROCEDURE proc_car_characteristics_func;
DROP PROCEDURE proc_mechanics_comm_cur;
DROP PROCEDURE proc_notfixed_cur;
DROP PROCEDURE proc_parts_car_cur;
DROP PROCEDURE proc_manufacturers_cur;
DROP PROCEDURE proc_bad_manufacturers_cur;
DROP PROCEDURE proc_car_characteristics_cur;
DROP PROCEDURE proc_parts_cur;
DROP PROCEDURE proc_foreign_mechanics_cur;
-----------------------------------------------
------------------/* DROP TRIGGERS */----------
-----------------------------------------------
DROP TRIGGER logon_trigg;
DROP TRIGGER logoff_trigg;
DROP TRIGGER create_trigg;
DROP TRIGGER prevent_drop_trigg;
DROP TRIGGER secure_insert_trigg;
DROP TRIGGER customers_trigg;
DROP TRIGGER check_salary_trigg;
DROP TRIGGER mechanics_trigg;
DROP TRIGGER date_constraint_trigg;
DROP TRIGGER bookings_trigg;
DROP TRIGGER check_customers_ins_trigg;
DROP TRIGGER check_mechanics_ins_trigg;
DROP TRIGGER check_replacedparts_ins_trigg;
PURGE RECYCLEBIN;
-----------------------------------------------
------------------/* CREATE VARRAYS */---------
-----------------------------------------------
CREATE TYPE manufacturers_varray_type AS VARRAY(20) OF VARCHAR2(40);
/
CREATE TYPE models_varray_type AS VARRAY(80) OF VARCHAR2(40);
/
CREATE TYPE years_made_varray_type AS VARRAY(40) OF VARCHAR2(4);
/
CREATE TYPE displacements_varray_type AS VARRAY(30) OF VARCHAR2(6);
/
CREATE TYPE colors_varray_type AS VARRAY(20) OF VARCHAR2(40);
/
CREATE TYPE part_manufacturers_varray_type AS VARRAY(80) OF VARCHAR2(40);
/
CREATE TYPE suppliers_varray_type AS VARRAY(40) OF VARCHAR2(40);
/
-----------------------------------------------
------------------/* CREATE TYPES */-----------
-----------------------------------------------
CREATE OR REPLACE TYPE address_type AS OBJECT (
street VARCHAR2(25),
street_no VARCHAR2(15),
city VARCHAR2(25),
country VARCHAR2(25));
/
CREATE OR REPLACE TYPE diagnosis_type AS OBJECT (
description VARCHAR2(200),
delivery_date DATE);
/
CREATE OR REPLACE TYPE characteristics_type AS OBJECT (
manufacturers manufacturers_varray_type,
model models_varray_type,
year_made years_made_varray_type,
displacements displacements_varray_type,
colors colors_varray_type);
/
CREATE OR REPLACE TYPE part_characteristics_type AS OBJECT (
part_manufacturer part_manufacturers_varray_type,
suppliers suppliers_varray_type);
/
CREATE TYPE compatibility_table_type AS TABLE OF manufacturers_varray_type;
/
-----------------------------------------------
------------------/* CREATE TABLES */----------
-----------------------------------------------
CREATE TABLE addresses OF address_type;
CREATE TABLE customers (
customer_id NUMBER(6),
firstname VARCHAR2(25) NOT NULL,
surname VARCHAR2(25) NOT NULL,
gender CHAR,
address REF address_type SCOPE IS addresses,
phone_number VARCHAR2(15) NOT NULL,
date_of_birth DATE);
CREATE TABLE mechanics (
mechanic_id NUMBER(6),
firstname VARCHAR2(25) NOT NULL,
surname VARCHAR2(25) NOT NULL,
gender CHAR,
address address_type,
phone_number VARCHAR2(15) NOT NULL,
salary NUMBER(10,2) NOT NULL,
commission NUMBER(10,2) NOT NULL,
date_hired DATE NOT NULL,
profile CLOB);
CREATE TABLE bookings (
booking_id NUMBER(6),
customer_car_id NUMBER(6),
mechanic_id NUMBER(6),
complaint VARCHAR2(300) NOT NULL,
booking_date DATE NOT NULL,
fixed CHAR,
diagnosis diagnosis_type);
CREATE TABLE customer_cars (
customer_car_id NUMBER(6),
customer_id NUMBER(6),
characteristics characteristics_type);
CREATE TABLE parts (
part_id NUMBER(6),
part_number NUMBER(6),
name VARCHAR2(25) NOT NULL,
price NUMBER(10,2) NOT NULL,
stock NUMBER(6) NOT NULL,
ordered_status CHAR NOT NULL,
part_characteristics part_characteristics_type,
compatibility compatibility_table_type)
NESTED TABLE compatibility STORE AS compatibility_table;
CREATE TABLE replaced_parts (
booking_id NUMBER(6),
part_id NUMBER(6),
quantity NUMBER(2) NOT NULL);
-----------------------------------------------
------------------/* PRIMARY KEYS */-----------
-----------------------------------------------
ALTER TABLE customers
ADD CONSTRAINT pk_customers
PRIMARY KEY (customer_id);
ALTER TABLE mechanics
ADD CONSTRAINT pk_mechanics
PRIMARY KEY (mechanic_id);
ALTER TABLE bookings
ADD CONSTRAINT pk_bookings
PRIMARY KEY (booking_id);
ALTER TABLE customer_cars
ADD CONSTRAINT pk_customer_cars
PRIMARY KEY (customer_car_id);
ALTER TABLE parts
ADD CONSTRAINT pk_parts
PRIMARY KEY (part_id);
ALTER TABLE replaced_parts
ADD CONSTRAINT pk_replaced_parts
PRIMARY KEY (booking_id, part_id);
-----------------------------------------------
------------------/* FOREIGN KEYS */-----------
-----------------------------------------------
ALTER TABLE bookings
ADD CONSTRAINT fk_b_customer_cars
FOREIGN KEY (customer_car_id)
REFERENCES customer_cars(customer_car_id);
ALTER TABLE bookings
ADD CONSTRAINT fk_b_mechanics
FOREIGN KEY (mechanic_id)
REFERENCES mechanics(mechanic_id);
ALTER TABLE customer_cars
ADD CONSTRAINT fk_c_customers
FOREIGN KEY (customer_id)
REFERENCES customers(customer_id);
ALTER TABLE replaced_parts
ADD CONSTRAINT fk_r_bookings
FOREIGN KEY (booking_id)
REFERENCES bookings(booking_id);
ALTER TABLE replaced_parts
ADD CONSTRAINT fk_r_parts
FOREIGN KEY (part_id)
REFERENCES parts(part_id);
-----------------------------------------------
------------------/* CHECK CONSTRAINTS */------
-----------------------------------------------
ALTER TABLE customers
ADD CONSTRAINT ck_customer_firstname
CHECK (firstname = upper(firstname));
ALTER TABLE customers
ADD CONSTRAINT ck_customer_surname
CHECK (surname = upper(surname));
ALTER TABLE mechanics
ADD CONSTRAINT ck_mechanic_firstname
CHECK (firstname = upper(firstname));
ALTER TABLE mechanics
ADD CONSTRAINT ck_mechanic_surname
CHECK (surname = upper(surname));
ALTER TABLE parts
ADD CONSTRAINT ck_part_name
CHECK (name = upper(name));
ALTER TABLE customers
ADD CONSTRAINT ck_customer_gender
CHECK (gender IN ('M','F'));
ALTER TABLE bookings
ADD CONSTRAINT ck_bookings_fixed
CHECK (fixed IN ('Y','N'));
ALTER TABLE parts
ADD CONSTRAINT ck_ordered_status
CHECK (ordered_status IN ('Y','N'));
-----------------------------------------------
------------------/* CREATE SEQUENCES */-------
-----------------------------------------------
CREATE SEQUENCE customers_seq
START WITH 1 INCREMENT BY 1
NOMAXVALUE
NOCACHE;
CREATE SEQUENCE mechanics_seq
START WITH 1 INCREMENT BY 1
NOMAXVALUE
NOCACHE;
CREATE SEQUENCE bookings_seq
START WITH 1 INCREMENT BY 1
NOMAXVALUE
NOCACHE;
CREATE SEQUENCE customer_cars_seq
START WITH 1 INCREMENT BY 1
NOMAXVALUE
NOCACHE;
CREATE SEQUENCE parts_seq
START WITH 1 INCREMENT BY 1
NOMAXVALUE
NOCACHE;
-----------------------------------------------
------------------/* INSERT DATA */------------
-----------------------------------------------
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MM-YYYY'
NLS_DATE_LANGUAGE = 'ENGLISH';
-- Insert into object table addresses
INSERT INTO addresses(street,street_no,city,country)
VALUES('EGNATIA','18', 'THESSALONIKI','GREECE');
INSERT INTO addresses(street,street_no,city,country)
VALUES('EGNATIA','108', 'THESSALONIKI','GREECE');
INSERT INTO addresses(street,street_no,city,country)
VALUES('EGNATIA','207', 'THESSALONIKI','GREECE');
INSERT INTO addresses(street,street_no,city,country)
VALUES('TSIMISKI','32', 'THESSALONIKI','GREECE');
INSERT INTO addresses(street,street_no,city,country)
VALUES('TSIMISKI','152', 'THESSALONIKI','GREECE');
INSERT INTO addresses(street,street_no,city,country)
VALUES('OLYMPIADOS','59', 'THESSALONIKI','GREECE');
INSERT INTO addresses(street,street_no,city,country)
VALUES('ERMOU','167', 'ATHENS','GREECE');
INSERT INTO addresses(street,street_no,city,country)
VALUES('FILIPPOU','69', 'ATHENS','GREECE');
INSERT INTO addresses(street,street_no,city,country)
VALUES('KASSANDROU','175', 'ATHENS','GREECE');
INSERT INTO addresses(street,street_no,city,country)
VALUES('DODEKANHSOU','92', 'THESSALONIKI','GREECE');
INSERT INTO addresses(street,street_no,city,country)
VALUES('KASSANDROU','48', 'DRAMA','GREECE');
INSERT INTO addresses(street,street_no,city,country)
VALUES('METEORON','12', 'THESSALONIKI','GREECE');
INSERT INTO addresses(street,street_no,city,country)
VALUES('EGNATIA','20', 'THESSALONIKI','GREECE');
-- Insert into table customers, we get address data from object table
INSERT INTO customers (customer_id, firstname, surname, gender, phone_number, date_of_birth, address)
SELECT customers_seq.NEXTVAL, 'LINUS', 'TORVALDS','M','46123487','14-07-1990', REF(a)
FROM addresses a
WHERE street = 'EGNATIA' AND street_no = '18';
INSERT INTO customers (customer_id, firstname, surname, gender, phone_number, date_of_birth, address)
SELECT customers_seq.NEXTVAL, 'LIN', 'ROBERTS','F','46852277','14-06-1987', REF(a)
FROM addresses a
WHERE street = 'EGNATIA' AND street_no = '108';
INSERT INTO customers (customer_id, firstname, surname, gender, phone_number, date_of_birth, address)
SELECT customers_seq.NEXTVAL, 'TOVE', 'TORVALDS','F','46158487','28-12-1985', REF(a)
FROM addresses a
WHERE street = 'EGNATIA' AND street_no = '207';
INSERT INTO customers (customer_id, firstname, surname, gender, phone_number, date_of_birth, address)
SELECT customers_seq.NEXTVAL, 'JANE', 'DOE','F','46649787','6-11-1979', REF(a)
FROM addresses a
WHERE street = 'TSIMISKI' AND street_no = '32';
INSERT INTO customers (customer_id, firstname, surname, gender, phone_number, date_of_birth, address)
SELECT customers_seq.NEXTVAL, 'JOHN', 'DOE','M','41624487','29-10-1978', REF(a)
FROM addresses a
WHERE street = 'TSIMISKI' AND street_no = '152';
INSERT INTO customers (customer_id, firstname, surname, gender, phone_number, date_of_birth, address)
SELECT customers_seq.NEXTVAL, 'JULIA', 'ROBERTS','M','44857487','14-01-1979', REF(a)
FROM addresses a
WHERE street = 'OLYMPIADOS' AND street_no = '59';
INSERT INTO customers (customer_id, firstname, surname, gender, phone_number, date_of_birth, address)
SELECT customers_seq.NEXTVAL, 'SHEYLA', 'SMITH','F','46123333','11-02-1982', REF(a)
FROM addresses a
WHERE street = 'ERMOU' AND street_no = '167';
INSERT INTO customers (customer_id, firstname, surname, gender, phone_number, date_of_birth, address)
SELECT customers_seq.NEXTVAL, 'RUDYARD', 'NATHANSON','M','46121827','10-05-1988', REF(a)
FROM addresses a
WHERE street = 'FILIPPOU' AND street_no = '69';
INSERT INTO customers (customer_id, firstname, surname, gender, phone_number, date_of_birth, address)
SELECT customers_seq.NEXTVAL, 'ASHER', 'ABRAHAMS','M','46126477','7-09-1986', REF(a)
FROM addresses a
WHERE street = 'KASSANDROU' AND street_no = '175';
INSERT INTO customers (customer_id, firstname, surname, gender, phone_number, date_of_birth, address)
SELECT customers_seq.NEXTVAL, 'ARIC', 'MORSE','M','46432187','26-08-1984', REF(a)
FROM addresses a
WHERE street = 'DODEKANHSOU' AND street_no = '92';
-- Insert into table mechanics, without accessing object table
INSERT INTO mechanics (mechanic_id, firstname, surname, gender, phone_number, salary, commission, date_hired, address)
VALUES (mechanics_seq.NEXTVAL, 'FRANKIE', 'FAY','F','46000487', 15000, 8.21, '25-02-1992',
address_type('KASSANDROU', '48', 'DRAMA','GREECE')
);
INSERT INTO mechanics (mechanic_id, firstname, surname, gender, phone_number, salary, commission, date_hired, address)
VALUES (mechanics_seq.NEXTVAL, 'LLOYD', 'KEVINSON','M','46144487', 16000, 9.24, '15-11-1989',
address_type('METEORON', '12', 'THESSALONIKI','GREECE')
);
INSERT INTO mechanics (mechanic_id, firstname, surname, gender, phone_number, salary, commission, date_hired, address)
VALUES (mechanics_seq.NEXTVAL, 'GORDON', 'MARSHALL','M','46123337', 18000, 15.95, '9-05-1989',
address_type('EGNATIA', '20', 'THESSALONIKI','GREECE')
);
-- Insert into table customer_cars
INSERT INTO customer_cars (customer_car_id, customer_id, characteristics)
VALUES (customer_cars_seq.NEXTVAL, 1, characteristics_type (manufacturers_varray_type ('MERCEDES'),
models_varray_type('BENZ'),
years_made_varray_type('2016'),
displacements_varray_type('2800'),
colors_varray_type('BLACK')
)
);
INSERT INTO customer_cars (customer_car_id, customer_id, characteristics)
VALUES (customer_cars_seq.NEXTVAL, 2, characteristics_type (manufacturers_varray_type ('LEXUS'),
models_varray_type('LS HYBRID'),
years_made_varray_type('2016'),
displacements_varray_type('3000'),
colors_varray_type('SILVER')
)
);
INSERT INTO customer_cars (customer_car_id, customer_id, characteristics)
VALUES (customer_cars_seq.NEXTVAL, 3, characteristics_type (manufacturers_varray_type ('LEXUS'),
models_varray_type('LS HYBRID'),
years_made_varray_type('2016'),
displacements_varray_type('3400'),
colors_varray_type('BLACK')
)
);
INSERT INTO customer_cars (customer_car_id, customer_id, characteristics)
VALUES (customer_cars_seq.NEXTVAL, 4, characteristics_type (manufacturers_varray_type ('MAZDA'),
models_varray_type('RX-7'),
years_made_varray_type('2007'),
displacements_varray_type('2600'),
colors_varray_type('RED')
)
);
INSERT INTO customer_cars (customer_car_id, customer_id, characteristics)
VALUES (customer_cars_seq.NEXTVAL, 5, characteristics_type (manufacturers_varray_type ('NISSAN'),
models_varray_type('SKYLINE'),
years_made_varray_type('2010'),
displacements_varray_type('2800'),
colors_varray_type('SILVER')
)
);
INSERT INTO customer_cars (customer_car_id, customer_id, characteristics)
VALUES (customer_cars_seq.NEXTVAL, 6, characteristics_type (manufacturers_varray_type ('MERCEDES'),
models_varray_type('BENZ'),
years_made_varray_type('2016'),
displacements_varray_type('2800'),
colors_varray_type('BLACK')
)
);
INSERT INTO customer_cars (customer_car_id, customer_id, characteristics)
VALUES (customer_cars_seq.NEXTVAL, 7, characteristics_type (manufacturers_varray_type ('BMW'),
models_varray_type('M3'),
years_made_varray_type('2008'),
displacements_varray_type('2200'),
colors_varray_type('YELLOW')
)
);
INSERT INTO customer_cars (customer_car_id, customer_id, characteristics)
VALUES (customer_cars_seq.NEXTVAL, 8, characteristics_type (manufacturers_varray_type ('MAZDA'),
models_varray_type('RX-8'),
years_made_varray_type('2008'),
displacements_varray_type('2800'),
colors_varray_type('BLACK')
)
);
INSERT INTO customer_cars (customer_car_id, customer_id, characteristics)
VALUES (customer_cars_seq.NEXTVAL, 9, characteristics_type (manufacturers_varray_type ('HONDA'),
models_varray_type('CIVIC TYPE-R'),
years_made_varray_type('2010'),
displacements_varray_type('2800'),
colors_varray_type('BLACK')
)
);
INSERT INTO customer_cars (customer_car_id, customer_id, characteristics)
VALUES (customer_cars_seq.NEXTVAL, 10, characteristics_type (manufacturers_varray_type ('MITSUBISHI'),
models_varray_type('LANCER EVOLUTION'),
years_made_varray_type('2007'),
displacements_varray_type('2400'),
colors_varray_type('BLACK')
)
);
-- Insert into table parts
INSERT INTO parts(part_id, part_number, name, price, stock, ordered_status, part_characteristics, compatibility)
VALUES (parts_seq.NEXTVAL, 111102, 'FRONT TIRES', 2000, 4, 'Y', part_characteristics_type (
part_manufacturers_varray_type('ACTRON TECHNOLOGY'),
suppliers_varray_type('THESSALONIKI MERCEDES')
),
compatibility_table_type (
manufacturers_varray_type('MERCEDES')
));
INSERT INTO parts(part_id, part_number, name, price, stock, ordered_status, part_characteristics, compatibility)
VALUES (parts_seq.NEXTVAL, 111103, 'BUMPER', 600, 0, 'Y', part_characteristics_type (
part_manufacturers_varray_type('NIPRESS'),
suppliers_varray_type('THESSALONIKI MERCEDES')
),
compatibility_table_type (
manufacturers_varray_type('MERCEDES')
));
INSERT INTO parts(part_id, part_number, name, price, stock, ordered_status, part_characteristics, compatibility)
VALUES (parts_seq.NEXTVAL, 111104, 'DISTRIBUTOR', 2200, 4, 'Y', part_characteristics_type (
part_manufacturers_varray_type('MARKET CREATORS'),
suppliers_varray_type('MAZDA HELLAS')
),
compatibility_table_type (
manufacturers_varray_type('MAZDA')
));
INSERT INTO parts(part_id, part_number, name, price, stock, ordered_status, part_characteristics, compatibility)
VALUES (parts_seq.NEXTVAL, 111105, 'BUMPER', 600, 0, 'Y', part_characteristics_type (
part_manufacturers_varray_type('JBM AUTO'),
suppliers_varray_type('HONDA PAPADOPOULOS')
),
compatibility_table_type (
manufacturers_varray_type('HONDA')
));
INSERT INTO parts(part_id, part_number, name, price, stock, ordered_status, part_characteristics, compatibility)
VALUES (parts_seq.NEXTVAL, 111106, 'SPARK PLUG', 600, 10, 'N', part_characteristics_type (
part_manufacturers_varray_type('FIEM INDUSTRIES'),
suppliers_varray_type('MAZDA HELLAS')
),
compatibility_table_type (
manufacturers_varray_type('MAZDA')
));
INSERT INTO parts(part_id, part_number, name, price, stock, ordered_status, part_characteristics, compatibility)
VALUES (parts_seq.NEXTVAL, 111107, 'FUEL LEVEL SENSOR', 750, 9, 'N', part_characteristics_type (
part_manufacturers_varray_type('JBM AUTO'),
suppliers_varray_type('THESSALONIKI MERCEDES')
),
compatibility_table_type (
manufacturers_varray_type('MERCEDES')
));
-- Insert into table bookings
INSERT INTO bookings(booking_id, customer_car_id, mechanic_id, complaint, fixed, booking_date, diagnosis)
VALUES (bookings_seq.NEXTVAL, 8, 1, 'I have problem steering the wheel', 'Y', '02-11-2016',
diagnosis_type ('Mechanic malfunction', '05-11-2016')
);
INSERT INTO bookings(booking_id, customer_car_id, mechanic_id, complaint, fixed, booking_date, diagnosis)
VALUES (bookings_seq.NEXTVAL, 1, 2, 'The car does not accelerate fast', 'Y', '23-09-2016',
diagnosis_type ('Electronic circuit needs updating', '27-09-2016')
);
INSERT INTO bookings(booking_id, customer_car_id, mechanic_id, complaint, fixed, booking_date, diagnosis)
VALUES (bookings_seq.NEXTVAL, 4, 3, 'Fridge liquid is missing', 'N', '10-01-2017',
diagnosis_type ('Fridge is broken', '16-01-2017')
);
INSERT INTO bookings(booking_id, customer_car_id, mechanic_id, complaint, fixed, booking_date, diagnosis)
VALUES (bookings_seq.NEXTVAL, 9, 1, 'I have trouble starting the engine', 'Y', '05-03-2017',
diagnosis_type ('The battery is almost dead', '07-03-2017')
);
INSERT INTO bookings(booking_id, customer_car_id, mechanic_id, complaint, fixed, booking_date, diagnosis)
VALUES (bookings_seq.NEXTVAL, 1, 3, 'The brakes do not work very well', 'Y', '07-03-2017',
diagnosis_type ('Not enough brakes liquid', '09-03-2017')
);
INSERT INTO bookings(booking_id, customer_car_id, mechanic_id, complaint, fixed, booking_date, diagnosis)
VALUES (bookings_seq.NEXTVAL, 2, 3, 'The car tends to turn left', 'Y', '10-03-2017',
diagnosis_type ('The left wheel is defective', '12-03-2017')
);
INSERT INTO bookings(booking_id, customer_car_id, mechanic_id, complaint, fixed, booking_date, diagnosis)
VALUES (bookings_seq.NEXTVAL, 7, 1, 'The lights go off unexpectedely', 'Y', '11-03-2017',
diagnosis_type ('Electrical malfunction', '16-03-2017')
);
INSERT INTO bookings(booking_id, customer_car_id, mechanic_id, complaint, fixed, booking_date, diagnosis)
VALUES (bookings_seq.NEXTVAL, 10, 3, 'I need a car service', 'Y', '12-MAR-2017',
diagnosis_type ('Normal annual service', '20-03-2017')
);
INSERT INTO bookings(booking_id, customer_car_id, mechanic_id, complaint, fixed, booking_date, diagnosis)
VALUES (bookings_seq.NEXTVAL, 10, 2, 'I need a car service', 'N', '13-03-2017',
diagnosis_type ('Normal annual service', '21-03-2017')
);
-- Insert into table replaced_parts
INSERT INTO replaced_parts(booking_id, part_id, quantity)
VALUES(1, 1, 1);
INSERT INTO replaced_parts(booking_id, part_id, quantity)
VALUES(1, 4, 1);
INSERT INTO replaced_parts(booking_id, part_id, quantity)
VALUES(2, 4, 2);
INSERT INTO replaced_parts(booking_id, part_id, quantity)
VALUES(3, 3, 1);
INSERT INTO replaced_parts(booking_id, part_id, quantity)
VALUES(3, 2, 1);
INSERT INTO replaced_parts(booking_id, part_id, quantity)
VALUES(4, 5, 1);
INSERT INTO replaced_parts(booking_id, part_id, quantity)
VALUES(5, 3, 1);
INSERT INTO replaced_parts(booking_id, part_id, quantity)
VALUES(5, 2, 2);
INSERT INTO replaced_parts(booking_id, part_id, quantity)
VALUES(6, 1, 1);
INSERT INTO replaced_parts(booking_id, part_id, quantity)
VALUES(7, 1, 1);
-----------------------------------------------
------------------/* SIMPLE QUERIES */---------
-----------------------------------------------
-- 1. Show customers who live in Thessaloniki
COLUMN customer_id HEADING 'ID';
COLUMN firstname HEADING 'Firstname' FORMAT A15;
COLUMN surname HEADING 'Surname' FORMAT A15;
COLUMN address.street HEADING 'Street' FORMAT A15;
COLUMN address.street_no HEADING 'No' FORMAT A5;
SELECT customer_id, firstname, surname, s.address.street, s.address.street_no
FROM customers s
WHERE s.address.city = 'THESSALONIKI';
-- 2. Show mechanics name and salary that come from another city
COLUMN firstname HEADING 'Firstname' FORMAT A15;
COLUMN surname HEADING 'Surname' FORMAT A15;
COLUMN salary HEADING 'Salary';
COLUMN address.street HEADING 'Street' FORMAT A15;
COLUMN address.street_no HEADING 'No' FORMAT A5;
COLUMN address.city HEADING 'City' FORMAT A15;
SELECT firstname, surname, salary, s.address.street, s.address.street_no, s.address.city
FROM mechanics s
WHERE s.address.city NOT IN 'THESSALONIKI';
-- 3. Show mechanics name and surname where their commision is from 7.50% - 9.50%
COLUMN firstname HEADING 'Firstname' FORMAT A15;
COLUMN surname HEADING 'Surname' FORMAT A10;
COLUMN commission HEADING 'Commission %';
SELECT surname, firstname, commission, ROUND(commission), FLOOR(commission)
FROM mechanics
WHERE commission BETWEEN 7.50 AND 9.50
ORDER BY commission DESC;
-- 4. Show how many bookings were made from 1st of January until current date by mechanic id 3
SELECT COUNT(booking_id)
FROM bookings
WHERE booking_date BETWEEN TO_DATE('01-JAN-2017') AND SYSDATE
AND mechanic_id = 3;
-- 5. Show booking dates and customer cost where a specific mechanic Gordon Marshall was assigned to the booking
COLUMN booking_date HEADING 'Booking Date' FORMAT A15;
COLUMN (ii.quantity*iii.price) HEADING 'Customer cost';
SELECT i.booking_date, (ii.quantity * iii.price)
FROM bookings i
JOIN replaced_parts ii
ON i.booking_id = ii.booking_id
JOIN parts iii
ON iii.part_id = ii.part_id
WHERE i.mechanic_id IN (
SELECT mechanic_id
FROM mechanics
WHERE firstname = 'GORDON' AND surname = 'MARSHALL');
-- 6. Show booking details where the mechanic could not fix the car
COLUMN booking_id HEADING 'ID';
COLUMN complaint HEADING 'Complaint' FORMAT A25;
COLUMN booking_date HEADING 'Booking Date' FORMAT A15;
COLUMN diagnosis.description HEADING 'Diagnosis' FORMAT A25;
SELECT booking_id, complaint, booking_date, b.diagnosis.description
FROM bookings b
WHERE fixed = 'N';
-- 7. Show booking id, complaint and diagnosis description where the customer cost was more than 1000
COLUMN booking_id HEADING 'ID';
COLUMN complaint HEADING 'Complaint' FORMAT A25;
COLUMN diagnosis.description HEADING 'Diagnosis' FORMAT A25;
COLUMN (ii.quantity*iii.price) HEADING 'Customer cost';
SELECT i.booking_id, i.complaint, i.diagnosis.description, (ii.quantity * iii.price)
FROM bookings i
JOIN replaced_parts ii
ON i.booking_id = ii.booking_id
JOIN parts iii
ON iii.part_id = ii.part_id
WHERE (ii.quantity * iii.price) > 1000;
-- 8. Show mechanics that have worked with customer car id 1
COLUMN firstname HEADING 'Firstname' FORMAT A15;
COLUMN surname HEADING 'Surname' FORMAT A15;
SELECT firstname, surname
FROM mechanics
JOIN bookings
ON bookings.mechanic_id = mechanics.mechanic_id
JOIN customer_cars
ON bookings.customer_car_id = customer_cars.customer_car_id
WHERE customer_cars.customer_car_id = 1
ORDER BY surname;
-- 9. Show the parts that have been replaced for all bookings of customer car id 1
COLUMN iii.name HEADING 'Part name' FORMAT A15;
COLUMN iii.price HEADING 'Price';
COLUMN iii.stock HEADING 'Stock';
SELECT iii.name, iii.price, iii.stock
FROM bookings i
JOIN replaced_parts ii
ON i.booking_id = ii.booking_id
JOIN parts iii
ON iii.part_id = ii.part_id
WHERE i.customer_car_id = 1;
-- 10. Show all parts that have been replaced, by stock ascending order
COLUMN iii.part_id HEADING 'ID';
COLUMN iii.name HEADING 'Part name' FORMAT A15;
COLUMN iii.price HEADING 'Price';
COLUMN iii.stock HEADING 'Stock';
SELECT iii.part_id, iii.name, iii.price, iii.stock
FROM bookings i
JOIN replaced_parts ii
ON i.booking_id = ii.booking_id
JOIN parts iii
ON iii.part_id = ii.part_id
ORDER BY iii.stock ASC;
-- 11. Create welcome message for mechanics with their newly created username
SELECT CONCAT('Good morning',
CONCAT
(INITCAP(SUBSTR(TRIM(TRAILING ' ' FROM firstname), 2, 3)),
LOWER(SUBSTR(surname, 2, 7)))
)
FROM mechanics;
-----------------------------------------------
------------------/* COMPLEX QUERIES */--------
-----------------------------------------------
-- 12. Show all manufacturers of all customer cars of customers
COLUMN firstname HEADING 'Firstname' FORMAT A15;
COLUMN surname HEADING 'Surname' FORMAT A15;
COLUMN column_value HEADING 'Manufacturers';
WITH my_data AS (
SELECT firstname, surname, c.characteristics.manufacturers AS manufacturers
FROM customers
JOIN customer_cars c
ON customers.customer_id = c.customer_id
)
SELECT my_data.firstname, my_data.surname, child.*
FROM my_data, TABLE(my_data.manufacturers) child;
-- 13. Show all manufacturers of all customer cars that have at least 2 bookings in the garage
COLUMN customer_car_id HEADING 'Customer ID';
COLUMN customer_id HEADING 'Car ID';
COLUMN column_value HEADING 'Manufacturers';
WITH my_data AS (
SELECT customer_id, customer_car_id, c.characteristics.manufacturers AS manufacturers
FROM customer_cars c
)
SELECT my_data.customer_id, my_data.customer_car_id, child.*
FROM my_data, TABLE(my_data.manufacturers) child
WHERE my_data.customer_car_id IN (
SELECT customer_car_id
FROM bookings