-
Notifications
You must be signed in to change notification settings - Fork 1
/
Car_Data.sql
1246 lines (1126 loc) · 37.7 KB
/
Car_Data.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
-- Prompt user for file path and file name
ACCEPT file_path PROMPT 'Enter the file path (e.g., M:\Y3S2\AdvanceDB\Assgm_Max\cardatabase): '
ACCEPT file_name PROMPT 'Enter the file name (e.g., cardata.csv): '
DECLARE
v_file_path VARCHAR2(255) := '&file_path';
v_file_name VARCHAR2(255) := '&file_name';
v_sql VARCHAR2(4000);
v_error_message VARCHAR2(4000);
BEGIN
-- Create Directory Object
BEGIN
EXECUTE IMMEDIATE 'CREATE OR REPLACE DIRECTORY my_dir AS ''' || v_file_path || '''';
EXCEPTION
WHEN OTHERS THEN
v_error_message := 'Error creating directory: ' || SQLERRM;
DBMS_OUTPUT.PUT_LINE(v_error_message);
RAISE;
END;
-- Create External Table
BEGIN
v_sql := '
CREATE TABLE CarData_ext (
Car_Name VARCHAR2(50),
Year NUMBER,
Selling_Price NUMBER,
Present_Price NUMBER,
Kms_Driven NUMBER,
Fuel_Type VARCHAR2(10),
Seller_Type VARCHAR2(10),
Transmission VARCHAR2(10),
Owner NUMBER
)
ORGANIZATION EXTERNAL (
TYPE ORACLE_LOADER
DEFAULT DIRECTORY my_dir
ACCESS PARAMETERS (
records delimited by newline
fields terminated by '','' optionally enclosed by ''"''
)
LOCATION (''' || v_file_name || ''')
)
REJECT LIMIT UNLIMITED
';
EXECUTE IMMEDIATE v_sql;
EXCEPTION
WHEN OTHERS THEN
v_error_message := 'Error creating external table: ' || SQLERRM;
DBMS_OUTPUT.PUT_LINE(v_error_message);
RAISE;
END;
-- Create Internal Table (if not already created)
BEGIN
EXECUTE IMMEDIATE '
CREATE TABLE CarData (
ID NUMBER PRIMARY KEY,
Car_Name VARCHAR2(50),
Year NUMBER,
Selling_Price NUMBER,
Present_Price NUMBER,
Kms_Driven NUMBER,
Fuel_Type VARCHAR2(10),
Seller_Type VARCHAR2(10),
Transmission VARCHAR2(10),
Owner NUMBER
)
';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE = -955 THEN
-- Table already exists, ignore
NULL;
ELSE
v_error_message := 'Error creating internal table: ' || SQLERRM;
DBMS_OUTPUT.PUT_LINE(v_error_message);
RAISE;
END IF;
END;
-- Create Sequence
BEGIN
EXECUTE IMMEDIATE '
CREATE SEQUENCE CarData_seq
START WITH 1
INCREMENT BY 1
NOCACHE
NOCYCLE
';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE = -955 THEN
-- Sequence already exists, ignore
NULL;
ELSE
v_error_message := 'Error creating sequence: ' || SQLERRM;
DBMS_OUTPUT.PUT_LINE(v_error_message);
RAISE;
END IF;
END;
-- Create Trigger
BEGIN
EXECUTE IMMEDIATE '
CREATE OR REPLACE TRIGGER CarData_before_insert
BEFORE INSERT ON CarData
FOR EACH ROW
BEGIN
IF :NEW.ID IS NULL THEN
SELECT CarData_seq.NEXTVAL
INTO :NEW.ID
FROM dual;
END IF;
END;
';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE = -4080 THEN
-- Trigger already exists, ignore
NULL;
ELSE
v_error_message := 'Error creating trigger: ' || SQLERRM;
DBMS_OUTPUT.PUT_LINE(v_error_message);
RAISE;
END IF;
END;
-- Insert Data into Internal/Actual Table
BEGIN
EXECUTE IMMEDIATE '
INSERT INTO CarData (Car_Name, Year, Selling_Price, Present_Price, Kms_Driven, Fuel_Type, Seller_Type, Transmission, Owner)
SELECT Car_Name, Year, Selling_Price, Present_Price, Kms_Driven, Fuel_Type, Seller_Type, Transmission, Owner
FROM CarData_ext
';
EXCEPTION
WHEN OTHERS THEN
v_error_message := 'Error inserting data into internal table: ' || SQLERRM;
DBMS_OUTPUT.PUT_LINE(v_error_message);
RAISE;
END;
DBMS_OUTPUT.PUT_LINE('Process completed successfully.');
END;
/
------------------------------------------------------------------------------------------------------------------------------------------
--Create Car--
--Before Trigger--
CREATE OR REPLACE TRIGGER before_insert_car
BEFORE INSERT ON CarData
FOR EACH ROW
BEGIN
-- Validate the data
IF :NEW.Year < 1886 OR :NEW.Year > EXTRACT(YEAR FROM SYSDATE) THEN
RAISE_APPLICATION_ERROR(-20001, 'Invalid Year. Please enter a valid year.');
END IF;
IF :NEW.Selling_Price < 0 THEN
RAISE_APPLICATION_ERROR(-20002, 'Selling Price cannot be negative.');
END IF;
IF :NEW.Present_Price < 0 THEN
RAISE_APPLICATION_ERROR(-20003, 'Present Price cannot be negative.');
END IF;
END;
/
--After Trigger--
CREATE OR REPLACE TRIGGER after_insert_car
AFTER INSERT ON CarData
FOR EACH ROW
BEGIN
DBMS_OUTPUT.PUT_LINE('New car record inserted: ' || :NEW.Car_Name);
END;
/
CREATE OR REPLACE PROCEDURE create_car(
p_car_name IN VARCHAR2,
p_year IN NUMBER,
p_selling_price IN NUMBER,
p_present_price IN NUMBER,
p_kms_driven IN NUMBER,
p_fuel_type IN VARCHAR2,
p_seller_type IN VARCHAR2,
p_transmission IN VARCHAR2,
p_owner IN NUMBER
) AS
BEGIN
SAVEPOINT before_insert;
BEGIN
INSERT INTO CarData (Car_Name, Year, Selling_Price, Present_Price, Kms_Driven, Fuel_Type, Seller_Type, Transmission, Owner)
VALUES (p_car_name, p_year, p_selling_price, p_present_price, p_kms_driven, p_fuel_type, p_seller_type, p_transmission, p_owner);
COMMIT;
DBMS_OUTPUT.PUT_LINE('Record created successfully.');
EXCEPTION
WHEN OTHERS THEN
ROLLBACK TO before_insert;
DBMS_OUTPUT.PUT_LINE('Error creating record: ' || SQLERRM);
END;
END;
/
SET SERVEROUTPUT ON
-- Prompt user for input--
ACCEPT car_name PROMPT 'Enter Car Name: '
ACCEPT year PROMPT 'Enter Year: '
ACCEPT selling_price PROMPT 'Enter Selling Price: '
ACCEPT present_price PROMPT 'Enter Present Price: '
ACCEPT kms_driven PROMPT 'Enter Kms Driven: '
ACCEPT fuel_type PROMPT 'Enter Fuel Type: '
ACCEPT seller_type PROMPT 'Enter Seller Type: '
ACCEPT transmission PROMPT 'Enter Transmission: '
ACCEPT owner PROMPT 'Enter Owner: '
-- Execute the procedure with the provided values--
BEGIN
create_car(
'&car_name',
&year,
&selling_price,
&present_price,
&kms_driven,
'&fuel_type',
'&seller_type',
'&transmission',
&owner
);
END;
/
------------------------------------------------------------------------------------------------------------------------------------------
--Get all cars--
CREATE OR REPLACE PROCEDURE get_all_cars AS
CURSOR car_cursor IS
SELECT Car_Name, Year, Selling_Price, Present_Price, Kms_Driven, Fuel_Type, Seller_Type, Transmission, Owner
FROM CarData;
v_car_name CarData.Car_Name%TYPE;
v_year CarData.Year%TYPE;
v_selling_price CarData.Selling_Price%TYPE;
v_present_price CarData.Present_Price%TYPE;
v_kms_driven CarData.Kms_Driven%TYPE;
v_fuel_type CarData.Fuel_Type%TYPE;
v_seller_type CarData.Seller_Type%TYPE;
v_transmission CarData.Transmission%TYPE;
v_owner CarData.Owner%TYPE;
BEGIN
OPEN car_cursor;
LOOP
FETCH car_cursor INTO v_car_name, v_year, v_selling_price, v_present_price, v_kms_driven, v_fuel_type, v_seller_type, v_transmission, v_owner;
EXIT WHEN car_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Car Name: ' || v_car_name);
DBMS_OUTPUT.PUT_LINE('Year: ' || v_year);
DBMS_OUTPUT.PUT_LINE('Selling Price: ' || v_selling_price);
DBMS_OUTPUT.PUT_LINE('Present Price: ' || v_present_price);
DBMS_OUTPUT.PUT_LINE('Kms Driven: ' || v_kms_driven);
DBMS_OUTPUT.PUT_LINE('Fuel Type: ' || v_fuel_type);
DBMS_OUTPUT.PUT_LINE('Seller Type: ' || v_seller_type);
DBMS_OUTPUT.PUT_LINE('Transmission: ' || v_transmission);
DBMS_OUTPUT.PUT_LINE('Owner: ' || v_owner);
DBMS_OUTPUT.PUT_LINE('-------------------------------');
END LOOP;
CLOSE car_cursor;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error retrieving data: ' || SQLERRM);
END;
/
/*
BEGIN
get_all_cars();
END;
/
*/
------------------------------------------------------------------------------------------------------------------------------------------
--Retrieve Car with Name--
CREATE OR REPLACE PROCEDURE read_car(p_car_name IN VARCHAR2) AS
v_car_name CarData.Car_Name%TYPE;
v_year CarData.Year%TYPE;
v_selling_price CarData.Selling_Price%TYPE;
v_present_price CarData.Present_Price%TYPE;
v_kms_driven CarData.Kms_Driven%TYPE;
v_fuel_type CarData.Fuel_Type%TYPE;
v_seller_type CarData.Seller_Type%TYPE;
v_transmission CarData.Transmission%TYPE;
v_owner CarData.Owner%TYPE;
BEGIN
SELECT Car_Name, Year, Selling_Price, Present_Price, Kms_Driven, Fuel_Type, Seller_Type, Transmission, Owner
INTO v_car_name, v_year, v_selling_price, v_present_price, v_kms_driven, v_fuel_type, v_seller_type, v_transmission, v_owner
FROM CarData
WHERE Car_Name = p_car_name;
DBMS_OUTPUT.PUT_LINE('Car Name: ' || v_car_name);
DBMS_OUTPUT.PUT_LINE('Year: ' || v_year);
DBMS_OUTPUT.PUT_LINE('Selling Price: ' || v_selling_price);
DBMS_OUTPUT.PUT_LINE('Present Price: ' || v_present_price);
DBMS_OUTPUT.PUT_LINE('Kms Driven: ' || v_kms_driven);
DBMS_OUTPUT.PUT_LINE('Fuel Type: ' || v_fuel_type);
DBMS_OUTPUT.PUT_LINE('Seller Type: ' || v_seller_type);
DBMS_OUTPUT.PUT_LINE('Transmission: ' || v_transmission);
DBMS_OUTPUT.PUT_LINE('Owner: ' || v_owner);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No record found for car name: ' || p_car_name);
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error reading record: ' || SQLERRM);
END;
/
/*
BEGIN
read_car('CarName');
END;
/
*/
------------------------------------------------------------------------------------------------------------------------------------------
--Update Car--
--Before Trigger--
CREATE OR REPLACE TRIGGER before_update_car
BEFORE UPDATE ON CarData
FOR EACH ROW
BEGIN
-- Validate the data
IF :NEW.Year < 1886 OR :NEW.Year > EXTRACT(YEAR FROM SYSDATE) THEN
RAISE_APPLICATION_ERROR(-20001, 'Invalid Year. Please enter a valid year.');
END IF;
IF :NEW.Selling_Price < 0 THEN
RAISE_APPLICATION_ERROR(-20002, 'Selling Price cannot be negative.');
END IF;
IF :NEW.Present_Price < 0 THEN
RAISE_APPLICATION_ERROR(-20003, 'Present Price cannot be negative.');
END IF;
END;
/
--After Trigger--
CREATE OR REPLACE TRIGGER after_update_car
AFTER UPDATE ON CarData
FOR EACH ROW
BEGIN
DBMS_OUTPUT.PUT_LINE('Car record updated: ' || :NEW.Car_Name);
END;
/
CREATE OR REPLACE PROCEDURE update_car(
p_id IN NUMBER,
p_car_name IN VARCHAR2,
p_year IN NUMBER,
p_selling_price IN NUMBER,
p_present_price IN NUMBER,
p_kms_driven IN NUMBER,
p_fuel_type IN VARCHAR2,
p_seller_type IN VARCHAR2,
p_transmission IN VARCHAR2,
p_owner IN NUMBER
) AS
BEGIN
SAVEPOINT before_update;
BEGIN
UPDATE CarData
SET Car_Name = p_car_name,
Year = p_year,
Selling_Price = p_selling_price,
Present_Price = p_present_price,
Kms_Driven = p_kms_driven,
Fuel_Type = p_fuel_type,
Seller_Type = p_seller_type,
Transmission = p_transmission,
Owner = p_owner
WHERE ID = p_id;
IF SQL%ROWCOUNT = 0 THEN
ROLLBACK TO before_update;
DBMS_OUTPUT.PUT_LINE('No record found for ID: ' || p_id);
ELSE
COMMIT;
DBMS_OUTPUT.PUT_LINE('Record updated successfully.');
END IF;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK TO before_update;
DBMS_OUTPUT.PUT_LINE('Error updating record: ' || SQLERRM);
END;
END;
/
SET SERVEROUTPUT ON
-- Prompt user for input
ACCEPT id PROMPT 'Enter ID of the record to update: '
ACCEPT car_name PROMPT 'Enter new Car Name: '
ACCEPT year PROMPT 'Enter new Year: '
ACCEPT selling_price PROMPT 'Enter new Selling Price: '
ACCEPT present_price PROMPT 'Enter new Present Price: '
ACCEPT kms_driven PROMPT 'Enter new Kms Driven: '
ACCEPT fuel_type PROMPT 'Enter new Fuel Type: '
ACCEPT seller_type PROMPT 'Enter new Seller Type: '
ACCEPT transmission PROMPT 'Enter new Transmission: '
ACCEPT owner PROMPT 'Enter new Owner: '
-- Execute the procedure with the provided values
BEGIN
update_car(
&id,
'&car_name',
&year,
&selling_price,
&present_price,
&kms_driven,
'&fuel_type',
'&seller_type',
'&transmission',
&owner
);
END;
/
------------------------------------------------------------------------------------------------------------------------------------------
--Delete Car--
--Before Trigger--
CREATE OR REPLACE TRIGGER before_delete_car
BEFORE DELETE ON CarData
FOR EACH ROW
BEGIN
-- Validate the data
IF :OLD.Selling_Price > 100000 THEN
RAISE_APPLICATION_ERROR(-20004, 'Cannot delete cars with a selling price above 100,000.');
END IF;
END;
/
--After Trigger--
CREATE OR REPLACE TRIGGER after_delete_car
AFTER DELETE ON CarData
FOR EACH ROW
BEGIN
DBMS_OUTPUT.PUT_LINE('Car record deleted: ' || :OLD.Car_Name);
END;
/
CREATE OR REPLACE PROCEDURE delete_car(p_id IN NUMBER) AS
BEGIN
SAVEPOINT before_delete;
BEGIN
DELETE FROM CarData
WHERE ID = p_id;
IF SQL%ROWCOUNT = 0 THEN
ROLLBACK TO before_delete;
DBMS_OUTPUT.PUT_LINE('No record found for ID: ' || p_id);
ELSE
COMMIT;
DBMS_OUTPUT.PUT_LINE('Record deleted successfully.');
END IF;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK TO before_delete;
DBMS_OUTPUT.PUT_LINE('Error deleting record: ' || SQLERRM);
END;
END;
/
SET SERVEROUTPUT ON
-- Prompt user for input
ACCEPT id PROMPT 'Enter ID of the record to delete: '
-- Execute the procedure with the provided value
BEGIN
delete_car(
&id
);
END;
/
------------------------------------------------------------------------------------------------------------------------------------------
--WHY USING INDEX?
--Faster Data Retrieval
--Search Optimization: Indexes allow the database to locate data without scanning every row in a table
--Efficient Sorting: Indexes help quickly sort the data on the indexed column(s), making operations like ORDER BY or range queries faster
--Reduced I/O Operations
--Less Disk Access: Instead of scanning the entire table, the database engine can use the index to quickly jump to the desired data
--Enhanced Performance for Joins
--Join Optimization: Indexes on the columns used in JOIN conditions can significantly improve the performance of join operations by quickly locating matching rows
--Improved Query Efficiency
--Selective Queries: Queries that filter based on specific conditions
--Aggregation and Grouping: Speed up aggregation operations like COUNT, SUM, AVG, and GROUP BY by reducing the number of rows the database needs to scan
-- Index on Year --
--e.g. SELECT * FROM CarData WHERE Year = 2011;
CREATE INDEX idx_car_year ON CarData (Year);
-- Index on Selling_Price --
CREATE INDEX idx_car_selling_price ON CarData (Selling_Price);
-- Index on Fuel_Type --
CREATE INDEX idx_car_fuel_type ON CarData (Fuel_Type);
--Index on Seller_Type --
CREATE INDEX idx_car_seller_type ON CarData (Seller_Type);
-- Composite Index on Fuel_Type and Transmission (for queries involving both columns) --
--e.g. SELECT * FROM CarData WHERE Fuel_Type = 'Petrol' AND Transmission = 'Manual';
CREATE INDEX idx_car_fuel_transmission ON CarData (Fuel_Type, Transmission);
------------------------------------------------------------------------------------------------------------------------------
--Data Analysis--
--Extracting Useful Information--
--- Procedure to get all cars from a specific year---
CREATE OR REPLACE PROCEDURE get_cars_by_year(p_year IN NUMBER) AS
CURSOR car_cursor IS
SELECT Car_Name, Year, Selling_Price, Present_Price, Kms_Driven, Fuel_Type, Seller_Type, Transmission, Owner
FROM CarData
WHERE Year = p_year;
v_car_name CarData.Car_Name%TYPE;
v_year CarData.Year%TYPE;
v_selling_price CarData.Selling_Price%TYPE;
v_present_price CarData.Present_Price%TYPE;
v_kms_driven CarData.Kms_Driven%TYPE;
v_fuel_type CarData.Fuel_Type%TYPE;
v_seller_type CarData.Seller_Type%TYPE;
v_transmission CarData.Transmission%TYPE;
v_owner CarData.Owner%TYPE;
BEGIN
OPEN car_cursor;
LOOP
FETCH car_cursor INTO v_car_name, v_year, v_selling_price, v_present_price, v_kms_driven, v_fuel_type, v_seller_type, v_transmission, v_owner;
EXIT WHEN car_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Car Name: ' || v_car_name);
DBMS_OUTPUT.PUT_LINE('Year: ' || v_year);
DBMS_OUTPUT.PUT_LINE('Selling Price: ' || v_selling_price);
DBMS_OUTPUT.PUT_LINE('Present Price: ' || v_present_price);
DBMS_OUTPUT.PUT_LINE('Kms Driven: ' || v_kms_driven);
DBMS_OUTPUT.PUT_LINE('Fuel Type: ' || v_fuel_type);
DBMS_OUTPUT.PUT_LINE('Seller Type: ' || v_seller_type);
DBMS_OUTPUT.PUT_LINE('Transmission: ' || v_transmission);
DBMS_OUTPUT.PUT_LINE('Owner: ' || v_owner);
DBMS_OUTPUT.PUT_LINE('-------------------------------');
END LOOP;
CLOSE car_cursor;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error retrieving data: ' || SQLERRM);
END;
/
SET SERVEROUTPUT ON;
--- Execute the procedure to get cars from a specific year ---
-- Prompt the user to enter specific year --
ACCEPT user_year PROMPT 'Enter the year you want to get cars from: '
-- PL/SQL Block to call the procedure with the user-provided year
BEGIN
get_cars_by_year(&user_year);
END;
/
-- Performing filtering table task --
--- Procedure to get cars with selling price above a certain amount, sorted by price ---
CREATE OR REPLACE PROCEDURE get_cars_by_price(p_min_price IN NUMBER) AS
CURSOR car_cursor IS
SELECT Car_Name, Year, Selling_Price, Present_Price, Kms_Driven, Fuel_Type, Seller_Type, Transmission, Owner
FROM CarData
WHERE Selling_Price > p_min_price
ORDER BY Selling_Price DESC;
v_car_name CarData.Car_Name%TYPE;
v_year CarData.Year%TYPE;
v_selling_price CarData.Selling_Price%TYPE;
v_present_price CarData.Present_Price%TYPE;
v_kms_driven CarData.Kms_Driven%TYPE;
v_fuel_type CarData.Fuel_Type%TYPE;
v_seller_type CarData.Seller_Type%TYPE;
v_transmission CarData.Transmission%TYPE;
v_owner CarData.Owner%TYPE;
BEGIN
OPEN car_cursor;
LOOP
FETCH car_cursor INTO v_car_name, v_year, v_selling_price, v_present_price, v_kms_driven, v_fuel_type, v_seller_type, v_transmission, v_owner;
EXIT WHEN car_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('Car Name: ' || v_car_name);
DBMS_OUTPUT.PUT_LINE('Year: ' || v_year);
DBMS_OUTPUT.PUT_LINE('Selling Price: ' || v_selling_price);
DBMS_OUTPUT.PUT_LINE('Present Price: ' || v_present_price);
DBMS_OUTPUT.PUT_LINE('Kms Driven: ' || v_kms_driven);
DBMS_OUTPUT.PUT_LINE('Fuel Type: ' || v_fuel_type);
DBMS_OUTPUT.PUT_LINE('Seller Type: ' || v_seller_type);
DBMS_OUTPUT.PUT_LINE('Transmission: ' || v_transmission);
DBMS_OUTPUT.PUT_LINE('Owner: ' || v_owner);
DBMS_OUTPUT.PUT_LINE('-------------------------------');
END LOOP;
CLOSE car_cursor;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error retrieving data: ' || SQLERRM);
END;
/
--- Execute the procedure to get cars with selling price above the amount user inputed ---
ACCEPT price PROMPT 'Enter the price to get cars with selling price above this certain amount: '
BEGIN
get_cars_by_price(&price);
END;
/
-- Create reusable PL/SQL functions and procedures to perform calculations on the data --
--- Calculate Average Selling Price by Year ---
CREATE OR REPLACE FUNCTION get_avg_selling_price_by_year(p_year IN NUMBER) RETURN NUMBER IS
v_avg_price NUMBER;
BEGIN
SELECT AVG(Selling_Price) INTO v_avg_price
FROM CarData
WHERE Year = p_year;
RETURN v_avg_price;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No records found for the year: ' || p_year);
RETURN NULL;
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error calculating average selling price: ' || SQLERRM);
RETURN NULL;
END;
/
SET SERVEROUTPUT ON
-- Prompt user to enter the year --
ACCEPT year PROMPT 'Enter the year you want to calculate the average selling price for: '
-- PL/SQL Block to calculate and display the average selling price --
DECLARE
v_avg_price NUMBER;
BEGIN
v_avg_price := get_avg_selling_price_by_year(&year);
IF v_avg_price IS NOT NULL THEN
DBMS_OUTPUT.PUT_LINE('Average Selling Price for ' || &year || ': ' || v_avg_price);
ELSE
DBMS_OUTPUT.PUT_LINE('No data found for the year ' || &year || '.');
END IF;
END;
/
-- Generating meaningful report --
CREATE OR REPLACE PROCEDURE generate_comprehensive_report AS
v_avg_price NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE('---- Car Data Report ----');
DBMS_OUTPUT.PUT_LINE('');
-- Part 1: Cars from a Specific Year (e.g., 2010)
DBMS_OUTPUT.PUT_LINE('--- Cars from the Year 2010 ---');
BEGIN
get_cars_by_year(2010);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error retrieving cars for the year 2015: ' || SQLERRM);
END;
DBMS_OUTPUT.PUT_LINE('');
-- Part 2: Cars with Selling Price Above a Certain Amount (e.g., 20)
DBMS_OUTPUT.PUT_LINE('--- Cars with Selling Price Above 20 ---');
BEGIN
get_cars_by_price(20);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error retrieving cars with selling price above 20: ' || SQLERRM);
END;
DBMS_OUTPUT.PUT_LINE('');
-- Part 3: Average Selling Price by Year
DBMS_OUTPUT.PUT_LINE('--- Average Selling Price by Year ---');
FOR year IN 2012..2020 LOOP
BEGIN
v_avg_price := get_avg_selling_price_by_year(year);
IF v_avg_price IS NOT NULL THEN
DBMS_OUTPUT.PUT_LINE('Year ' || year || ': ' || v_avg_price);
ELSE
DBMS_OUTPUT.PUT_LINE('Year ' || year || ': No data found.');
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error calculating average selling price for the year ' || year || ': ' || SQLERRM);
END;
END LOOP;
DBMS_OUTPUT.PUT_LINE('');
DBMS_OUTPUT.PUT_LINE('---- End of Report ----');
END;
/
BEGIN
generate_comprehensive_report;
END;
/
--- Generated report content ---
---- Car Data Report ----
--- Cars from the Year 2010 ---
Car Name: sx4
Year: 2010
Selling Price: 2.65
Present Price: 7.98
Kms Driven: 41442
Fuel Type: Petrol
Seller Type: Dealer
Transmission: Manual
Owner: 0
-------------------------------
Car Name: alto k10
Year: 2010
Selling Price: 1.95
Present Price: 3.95
Kms Driven: 44542
Fuel Type: Petrol
Seller Type: Dealer
Transmission: Manual
Owner: 0
-------------------------------
Car Name: corolla altis
Year: 2010
Selling Price: 4.75
Present Price: 18.54
Kms Driven: 50000
Fuel Type: Petrol
Seller Type: Dealer
Transmission: Manual
Owner: 0
-------------------------------
Car Name: fortuner
Year: 2010
Selling Price: 9.25
Present Price: 20.45
Kms Driven: 59000
Fuel Type: Diesel
Seller Type: Dealer
Transmission: Manual
Owner: 0
-------------------------------
Car Name: corolla altis
Year: 2010
Selling Price: 5.25
Present Price: 22.83
Kms Driven: 80000
Fuel Type: Petrol
Seller Type: Dealer
Transmission: Automatic
Owner: 0
-------------------------------
Car Name: land cruiser
Year: 2010
Selling Price: 35
Present Price: 92.6
Kms Driven: 78000
Fuel Type: Diesel
Seller Type: Dealer
Transmission: Manual
Owner: 0
-------------------------------
Car Name: fortuner
Year: 2010
Selling Price: 9.65
Present Price: 20.45
Kms Driven: 50024
Fuel Type: Diesel
Seller Type: Dealer
Transmission: Manual
Owner: 0
-------------------------------
Car Name: Bajaj Pulsar 220 F
Year: 2010
Selling Price: .52
Present Price: .94
Kms Driven: 45000
Fuel Type: Petrol
Seller Type: Individual
Transmission: Manual
Owner: 0
-------------------------------
Car Name: Bajaj Avenger 220 dtsi
Year: 2010
Selling Price: .45
Present Price: .95
Kms Driven: 27000
Fuel Type: Petrol
Seller Type: Individual
Transmission: Manual
Owner: 0
-------------------------------
Car Name: Honda Karizma
Year: 2010
Selling Price: .31
Present Price: 1.05
Kms Driven: 213000
Fuel Type: Petrol
Seller Type: Individual
Transmission: Manual
Owner: 0
-------------------------------
Car Name: TVS Wego
Year: 2010
Selling Price: .25
Present Price: .52
Kms Driven: 22000
Fuel Type: Petrol
Seller Type: Individual
Transmission: Automatic
Owner: 0
-------------------------------
Car Name: Honda CB twister
Year: 2010
Selling Price: .16
Present Price: .51
Kms Driven: 33000
Fuel Type: Petrol
Seller Type: Individual
Transmission: Manual
Owner: 0
-------------------------------
Car Name: i20
Year: 2010
Selling Price: 3.25
Present Price: 6.79
Kms Driven: 58000
Fuel Type: Diesel
Seller Type: Dealer
Transmission: Manual
Owner: 1
-------------------------------
Car Name: jazz
Year: 2010
Selling Price: 2.25
Present Price: 7.5
Kms Driven: 61203
Fuel Type: Petrol
Seller Type: Dealer
Transmission: Manual
Owner: 0
-------------------------------
Car Name: city
Year: 2010
Selling Price: 3.25
Present Price: 9.9
Kms Driven: 38000
Fuel Type: Petrol
Seller Type: Dealer
Transmission: Manual
Owner: 0
-------------------------------
--- Cars with Selling Price Above 20 ---
Car Name: benza
Year: 2008
Selling Price: 89
Present Price: 28
Kms Driven: 20000
Fuel Type: Petrol
Seller Type: Dealer
Transmission: 0
Owner: 0
-------------------------------
Car Name: land cruiser
Year: 2010
Selling Price: 35
Present Price: 92.6
Kms Driven: 78000
Fuel Type: Diesel
Seller Type: Dealer
Transmission: Manual
Owner: 0
-------------------------------
Car Name: fortuner
Year: 2017
Selling Price: 33
Present Price: 36.23
Kms Driven: 6000
Fuel Type: Diesel
Seller Type: Dealer
Transmission: Automatic
Owner: 0
-------------------------------
Car Name: fortuner
Year: 2015
Selling Price: 23.5
Present Price: 35.96
Kms Driven: 47000
Fuel Type: Diesel
Seller Type: Dealer
Transmission: Automatic
Owner: 0
-------------------------------
Car Name: fortuner
Year: 2015
Selling Price: 23
Present Price: 30.61
Kms Driven: 40000
Fuel Type: Diesel
Seller Type: Dealer
Transmission: Automatic
Owner: 0
-------------------------------
Car Name: fortuner
Year: 2015
Selling Price: 23
Present Price: 30.61
Kms Driven: 40000
Fuel Type: Diesel
Seller Type: Dealer
Transmission: Automatic
Owner: 0
-------------------------------
Car Name: innova
Year: 2017
Selling Price: 23
Present Price: 25.39
Kms Driven: 15000
Fuel Type: Diesel
Seller Type: Dealer
Transmission: Automatic
Owner: 0
-------------------------------
Car Name: innova
Year: 2016
Selling Price: 20.75
Present Price: 25.39
Kms Driven: 29000
Fuel Type: Diesel
Seller Type: Dealer
Transmission: Automatic
Owner: 0
-------------------------------
--- Average Selling Price by Year ---
Year 2012: 3.8413043478260869565217391304347826087
Year 2013: 3.54090909090909090909090909090909090909
Year 2014: 4.76210526315789473684210526315789473684
Year 2015: 5.92704918032786885245901639344262295082
Year 2016: 5.2132
Year 2017: 6.17852941176470588235294117647058823529
Year 2018: 9.25
Year 2019: No data found.
Year 2020: No data found.
---- End of Report ---
------------------------------------------------------------------------------------------------------------------------------
-------Aggregations:Calculate the Percentage Change in Average Selling Price Year-over-Year--------
DECLARE
v_avg_price_current NUMBER;
v_avg_price_previous NUMBER;
v_percentage_change NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE('--- Percentage Change in Average Selling Price Year-over-Year ---');
FOR year IN 2013..2020 LOOP
BEGIN
-- Get average price for the current year
v_avg_price_current := get_avg_selling_price_by_year(year);
-- Get average price for the previous year
v_avg_price_previous := get_avg_selling_price_by_year(year - 1);
IF v_avg_price_current IS NOT NULL AND v_avg_price_previous IS NOT NULL THEN
-- Calculate percentage change
v_percentage_change := ((v_avg_price_current - v_avg_price_previous) / v_avg_price_previous) * 100;
DBMS_OUTPUT.PUT_LINE('Year ' || year || ' vs ' || (year - 1) || ': ' || ROUND(v_percentage_change, 2) || '%');
ELSE
IF v_avg_price_current IS NULL THEN
DBMS_OUTPUT.PUT_LINE('Year ' || year || ': No data found.');
END IF;
IF v_avg_price_previous IS NULL THEN