-
Notifications
You must be signed in to change notification settings - Fork 2
/
lemans24.sql
1893 lines (1705 loc) · 69.1 KB
/
lemans24.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
-- MySQL Script generated by MySQL Workbench
-- Sun Jun 16 19:11:06 2024
-- Model: Le Mans 24h Version: 1.0
-- MySQL Workbench Forward Engineering
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
-- -----------------------------------------------------
-- Schema lemans24
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema lemans24
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `lemans24` DEFAULT CHARACTER SET utf8mb4 ;
USE `lemans24` ;
-- -----------------------------------------------------
-- Table `lemans24`.`drivers`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`drivers` (
`id` INT NOT NULL AUTO_INCREMENT,
`title` VARCHAR(16) NULL COMMENT 'Royal, military or academic title',
`fname` VARCHAR(32) NULL,
`lname` VARCHAR(32) NOT NULL,
`nickname` VARCHAR(32) NULL,
`nm_suffix` VARCHAR(4) NULL COMMENT 'Name suffix (e.g. Jr., Sr., III)',
`full_name` VARCHAR(64) GENERATED ALWAYS AS (concat((case when ((not((`title` like 'Baron %'))) and (not((`title` like 'Earl %'))) and (not((`title` like 'Lord %')))) then concat(`title`,' ') else '' end),(case when isnull(`fname`) then '' else concat(`fname`,' ') end),(case when isnull(`nickname`) then '' else concat('"',`nickname`,'" ') end),`lname`,(case when ((`title` like 'Baron %') or (`title` like 'Earl %') or (`title` like 'Lord %')) then concat(', ',`title`) else '' end),(case when isnull(`nm_suffix`) then '' else concat(' ',`nm_suffix`) end))) VIRTUAL COMMENT 'Driver full name including title, names, nickname and suffix',
`sex` ENUM('M', 'F') NULL,
`born` DATE NULL,
`country` CHAR(4) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `idx_driver_unq` (`lname` ASC, `sex` ASC, `country` ASC, `fname` ASC, `nickname` ASC, `title` ASC, `nm_suffix` ASC) VISIBLE,
INDEX `idx_driver_sex` (`sex` ASC) VISIBLE,
FULLTEXT INDEX `idx_driver_full` (`title`, `fname`, `lname`, `nickname`, `nm_suffix`) VISIBLE)
ENGINE = InnoDB
COMMENT = 'Drivers register';
-- -----------------------------------------------------
-- Table `lemans24`.`circuits`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`circuits` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`since` DATE NOT NULL,
`length_km` DECIMAL(5,3) NULL,
`length_mi` DECIMAL(5,3) GENERATED ALWAYS AS (ROUND(length_km * 0.621371192, 3)) VIRTUAL,
`layout` LONGBLOB NULL,
`changes` VARCHAR(128) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB
COMMENT = 'Circutis register';
-- -----------------------------------------------------
-- Table `lemans24`.`races`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`races` (
`id` INT NOT NULL,
`circuit_id` INT NOT NULL,
`event_date` DATE NOT NULL,
`start_time` TIME NOT NULL,
`cancelled` TINYINT NOT NULL,
`cancellation_reason` VARCHAR(128) NULL,
`distance_km` DECIMAL(10,3) NULL,
`distance_mi` DECIMAL(10,3) GENERATED ALWAYS AS (ROUND(distance_km * 0.621371192, 3)) VIRTUAL,
`laps` DECIMAL(7,3) NOT NULL COMMENT 'Number of laps',
`avg_speed_kmh` DECIMAL(5,2) GENERATED ALWAYS AS (ROUND(distance_km / 24 , 2)),
`avg_speed_mph` DECIMAL(5,2) GENERATED ALWAYS AS (ROUND(avg_speed_kmh * 0.621371192, 2)) VIRTUAL,
PRIMARY KEY (`id`),
INDEX `fk_race_circuit_idx` (`circuit_id` ASC) VISIBLE,
CONSTRAINT `fk_race_circuit`
FOREIGN KEY (`circuit_id`)
REFERENCES `lemans24`.`circuits` (`id`)
ON DELETE RESTRICT
ON UPDATE CASCADE)
ENGINE = InnoDB
COMMENT = 'Races register';
-- -----------------------------------------------------
-- Table `lemans24`.`cars`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`cars` (
`id` INT NOT NULL AUTO_INCREMENT,
`car_class` VARCHAR(12) NULL,
`car_chassis` VARCHAR(64) NOT NULL,
`car_engine` VARCHAR(64) NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `idx_car_unq` (`car_class` ASC, `car_chassis` ASC, `car_engine` ASC) VISIBLE)
ENGINE = InnoDB
COMMENT = 'Cars register';
-- -----------------------------------------------------
-- Table `lemans24`.`car_numbers`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`car_numbers` (
`id` INT NOT NULL AUTO_INCREMENT,
`race_id` INT NOT NULL,
`car_id` INT NULL,
`nbr` VARCHAR(3) CHARACTER SET 'ascii' NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_car_nums_race_idx` (`race_id` ASC) VISIBLE,
INDEX `fk_car_nums_car_idx` (`car_id` ASC) VISIBLE,
CONSTRAINT `fk_car_nums_race`
FOREIGN KEY (`race_id`)
REFERENCES `lemans24`.`races` (`id`)
ON DELETE RESTRICT
ON UPDATE CASCADE,
CONSTRAINT `fk_car_nums_car`
FOREIGN KEY (`car_id`)
REFERENCES `lemans24`.`cars` (`id`)
ON DELETE RESTRICT
ON UPDATE CASCADE)
ENGINE = InnoDB
COMMENT = 'Car number per race';
-- -----------------------------------------------------
-- Table `lemans24`.`results`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`results` (
`id` INT NOT NULL AUTO_INCREMENT,
`race_id` INT NOT NULL,
`car_id` INT NOT NULL,
`pos` VARCHAR(3) NOT NULL,
`laps` INT NULL,
`distance` DECIMAL(8,3) NULL,
`racing_time` TIME NULL,
`reason` VARCHAR(128) NULL,
PRIMARY KEY (`id`),
INDEX `fk_result_race_idx` (`race_id` ASC) VISIBLE,
INDEX `fk_results_car_idx` (`car_id` ASC) VISIBLE,
INDEX `idx_results_pos` (`pos` ASC) VISIBLE,
CONSTRAINT `fk_results_race`
FOREIGN KEY (`race_id`)
REFERENCES `lemans24`.`races` (`id`)
ON DELETE RESTRICT
ON UPDATE CASCADE,
CONSTRAINT `fk_results_car`
FOREIGN KEY (`car_id`)
REFERENCES `lemans24`.`car_numbers` (`id`)
ON DELETE RESTRICT
ON UPDATE CASCADE)
ENGINE = InnoDB
COMMENT = 'Driver results per race';
-- -----------------------------------------------------
-- Table `lemans24`.`results_in`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`results_in` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`race_yr` INT NOT NULL COMMENT 'Race year',
`pos` VARCHAR(3) NOT NULL COMMENT 'Position - either number or NC (not classified), DNF (do not finish) or DSQ (disqualified)',
`car_class` VARCHAR(12) NOT NULL COMMENT 'Car class (litres or named)',
`car_nbr` VARCHAR(3) NOT NULL COMMENT 'Car number',
`team_cntry` VARCHAR(16) NOT NULL COMMENT 'Team country (ISO 3166 3 characters code)',
`team_name` VARCHAR(128) NOT NULL COMMENT 'Team name',
`drivers_cntry` VARCHAR(32) NOT NULL COMMENT 'Driver countries (ISO 3166 3 characters code) separated',
`drivers_name` VARCHAR(256) NOT NULL COMMENT 'Drivers names',
`car_chassis` VARCHAR(64) NOT NULL COMMENT 'Chassis name',
`car_engine` VARCHAR(64) NOT NULL COMMENT 'Engine name',
`car_tyres` VARCHAR(16) NULL COMMENT 'Tyres manufacturer',
`laps` INT NULL COMMENT 'Laps completed',
`distance` DECIMAL(8,3) NULL COMMENT 'Distance covered',
`racing_time` TIME NULL COMMENT 'Racing time',
`reason` VARCHAR(128) NULL COMMENT 'Reason for NC, DNF, DSQ',
`processed` TINYINT NOT NULL DEFAULT 0 COMMENT 'Record processed or not',
PRIMARY KEY (`id`),
INDEX `idx_processed` (`processed` ASC) VISIBLE)
ENGINE = InnoDB
COMMENT = 'Table for results input';
-- -----------------------------------------------------
-- Table `lemans24`.`teams`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`teams` (
`id` INT NOT NULL AUTO_INCREMENT,
`title` VARCHAR(64) NOT NULL,
`country` CHAR(4) NULL,
`private_entrant` TINYINT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `idx_team_unq` (`title` ASC, `country` ASC) VISIBLE)
ENGINE = InnoDB
COMMENT = 'Teams register';
-- -----------------------------------------------------
-- Table `lemans24`.`tyres`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`tyres` (
`id` CHAR(2) NOT NULL,
`brand` VARCHAR(16) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB
COMMENT = 'Tyre brands register';
-- -----------------------------------------------------
-- Table `lemans24`.`car_tyres`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`car_tyres` (
`car_id` INT NOT NULL,
`tyre_id` CHAR(2) NOT NULL,
PRIMARY KEY (`car_id`, `tyre_id`),
INDEX `fk_car_tyres_tyre_idx` (`tyre_id` ASC) VISIBLE,
CONSTRAINT `fk_car_tyres_car`
FOREIGN KEY (`car_id`)
REFERENCES `lemans24`.`car_numbers` (`id`)
ON DELETE RESTRICT
ON UPDATE CASCADE,
CONSTRAINT `fk_car_tyres_tyre`
FOREIGN KEY (`tyre_id`)
REFERENCES `lemans24`.`tyres` (`id`)
ON DELETE RESTRICT
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `lemans24`.`driver_results`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`driver_results` (
`driver_id` INT NOT NULL,
`result_id` INT NOT NULL,
`ord_num` INT NOT NULL COMMENT 'Order number',
PRIMARY KEY (`driver_id`, `result_id`),
INDEX `fk_driver_results_result_idx` (`result_id` ASC) VISIBLE,
CONSTRAINT `fk_drv_res_driver`
FOREIGN KEY (`driver_id`)
REFERENCES `lemans24`.`drivers` (`id`)
ON DELETE RESTRICT
ON UPDATE CASCADE,
CONSTRAINT `fk_drv_res_result`
FOREIGN KEY (`result_id`)
REFERENCES `lemans24`.`results` (`id`)
ON DELETE RESTRICT
ON UPDATE CASCADE)
ENGINE = InnoDB
COMMENT = 'Connect drivers to results';
-- -----------------------------------------------------
-- Table `lemans24`.`team_results`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`team_results` (
`team_id` INT NOT NULL,
`result_id` INT NOT NULL,
`ord_num` INT NOT NULL COMMENT 'Order number',
PRIMARY KEY (`team_id`, `result_id`),
INDEX `fk_tm_res_res_idx` (`result_id` ASC) VISIBLE,
CONSTRAINT `fk_tm_res_team`
FOREIGN KEY (`team_id`)
REFERENCES `lemans24`.`teams` (`id`)
ON DELETE RESTRICT
ON UPDATE CASCADE,
CONSTRAINT `fk_tm_res_res`
FOREIGN KEY (`result_id`)
REFERENCES `lemans24`.`results` (`id`)
ON DELETE RESTRICT
ON UPDATE CASCADE)
ENGINE = InnoDB
COMMENT = 'Connect teams to results';
USE `lemans24` ;
-- -----------------------------------------------------
-- Placeholder table for view `lemans24`.`Circuits`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`Circuits` (`Years` INT, `Length_km` INT, `Length_mi` INT, `Changes` INT);
-- -----------------------------------------------------
-- Placeholder table for view `lemans24`.`Races`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`Races` (`id` INT);
-- -----------------------------------------------------
-- Placeholder table for view `lemans24`.`RaceDistKmRecords`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`RaceDistKmRecords` (`Year` INT, `Distance_km` INT, `Laps` INT);
-- -----------------------------------------------------
-- Placeholder table for view `lemans24`.`RaceLapsRecords`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`RaceLapsRecords` (`Year` INT, `Distance_km` INT, `Laps` INT);
-- -----------------------------------------------------
-- Placeholder table for view `lemans24`.`RaceAvgSpdKmhRecords`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`RaceAvgSpdKmhRecords` (`Year` INT, `Distance_km` INT, `Laps` INT, `AvgSpeed_kmh` INT);
-- -----------------------------------------------------
-- Placeholder table for view `lemans24`.`Results`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`Results` (`Race` INT, `Pos` INT, `Class` INT, `CarNb` INT, `TCountry` INT, `Team` INT, `DCountry` INT, `Drivers` INT, `Chassis` INT, `Engine` INT, `Tyre` INT, `Laps` INT, `Distance` INT, `RacingTime` INT, `Reason` INT);
-- -----------------------------------------------------
-- Placeholder table for view `lemans24`.`Winners`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`Winners` (`id` INT);
-- -----------------------------------------------------
-- Placeholder table for view `lemans24`.`WinningCarNumbers`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`WinningCarNumbers` (`Number` INT, `Years` INT, `Percent` INT);
-- -----------------------------------------------------
-- Placeholder table for view `lemans24`.`MostWinsByDriver`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`MostWinsByDriver` (`Driver` INT, `Wins` INT, `FirstWin` INT, `LastWin` INT, `Years` INT);
-- -----------------------------------------------------
-- Placeholder table for view `lemans24`.`MostWinsByTeam`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`MostWinsByTeam` (`Team` INT, `Wins` INT, `FirstWin` INT, `LastWin` INT, `Years` INT);
-- -----------------------------------------------------
-- Placeholder table for view `lemans24`.`MostWinsByTeamWAppr`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`MostWinsByTeamWAppr` (`Team` INT, `Wins` INT, `FirstWin` INT, `LastWin` INT, `Years` INT);
-- -----------------------------------------------------
-- Placeholder table for view `lemans24`.`TotalStartsByDriver`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`TotalStartsByDriver` (`Driver` INT, `DCountry` INT, `Starts` INT, `FirstStart` INT, `LastStart` INT, `Years` INT);
-- -----------------------------------------------------
-- Placeholder table for view `lemans24`.`MostPodiumsByDriver`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`MostPodiumsByDriver` (`Driver` INT, `FstPlace` INT, `SndPlace` INT, `TrdPlace` INT, `TtlPodiums` INT, `FirstPodium` INT, `LastPodium` INT, `Years` INT);
-- -----------------------------------------------------
-- Placeholder table for view `lemans24`.`FemaleDrivers`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`FemaleDrivers` (`Name` INT, `Country` INT, `Years` INT, `Starts` INT, `BestFinish` INT, `OvWins` INT, `OvTop10` INT, `OvTop10Years` INT, `ClassWins` INT, `ClassWinYears` INT);
-- -----------------------------------------------------
-- Placeholder table for view `lemans24`.`AllWomenTeams`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`AllWomenTeams` (`Year` INT, `Team` INT, `Drivers` INT, `Car` INT, `Finish` INT, `Inclass` INT);
-- -----------------------------------------------------
-- Placeholder table for view `lemans24`.`FemaleDriversByCntry`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`FemaleDriversByCntry` (`Country` INT, `First` INT, `Drivers` INT, `Starts` INT, `Top10` INT, `Top10Years` INT, `ClassWins` INT, `ClassWinYears` INT);
-- -----------------------------------------------------
-- Placeholder table for view `lemans24`.`DriversWithNickname`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`DriversWithNickname` (`Name` INT, `Sex` INT, `Country` INT, `Years` INT, `Starts` INT, `BestFinish` INT);
-- -----------------------------------------------------
-- Placeholder table for view `lemans24`.`AllDriversResults`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`AllDriversResults` (`Name` INT, `Sex` INT, `Country` INT, `Years` INT, `Starts` INT, `BestFinish` INT, `OvWins` INT, `OvTop10` INT, `OvTop10Years` INT, `ClassWins` INT, `ClassWinYears` INT);
-- -----------------------------------------------------
-- Placeholder table for view `lemans24`.`MostWinsByConstructor`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`MostWinsByConstructor` (`Constructor` INT, `Wins` INT, `FirstWin` INT, `LastWin` INT, `Years` INT);
-- -----------------------------------------------------
-- Placeholder table for view `lemans24`.`MostWinsByCar`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`MostWinsByCar` (`Car` INT, `Wins` INT, `FirstWin` INT, `LastWin` INT, `Years` INT);
-- -----------------------------------------------------
-- Placeholder table for view `lemans24`.`WinnersByClass`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`WinnersByClass` (`id` INT);
-- -----------------------------------------------------
-- Placeholder table for view `lemans24`.`RacesByCarsFinished`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`RacesByCarsFinished` (`Year` INT, `Started` INT, `Finished` INT, `FinishedPercent` INT, `Disqualified` INT, `Not_classified` INT, `Retired` INT);
-- -----------------------------------------------------
-- Placeholder table for view `lemans24`.`MostWinsByNation`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `lemans24`.`MostWinsByNation` (`Nationality` INT, `Wins` INT, `Drivers` INT, `FirstWin` INT, `LastWin` INT, `Years` INT);
-- -----------------------------------------------------
-- function check_position
-- -----------------------------------------------------
DELIMITER //
USE `lemans24`//
CREATE FUNCTION check_position(pos VARCHAR(3))
RETURNS BOOL NO SQL
BEGIN
/* Either a number */
IF pos RLIKE '^[1-9][0-9]*$' THEN
RETURN TRUE;
/* Ore one of these values
* DNA - Did Not Attend
* DNF - Did Not Finish
* DNP - Did Not Practice
* DNQ - Did Not Qualify
* DNS - Did Not Start
* DSQ - Disqualified
* NC - Not classified
* RES - Reserve
*/
ELSEIF pos IN ('DNA', 'DNF', 'DNP', 'DNQ', 'DNS', 'DSQ', 'NC', 'RES') THEN
RETURN TRUE;
ELSE
RETURN FALSE;
END IF;
END//
DELIMITER ;
-- -----------------------------------------------------
-- procedure cleanup
-- -----------------------------------------------------
DELIMITER //
USE `lemans24`//
CREATE PROCEDURE cleanup()
BEGIN
DELETE FROM driver_results WHERE driver_id > 0;
ALTER TABLE driver_results AUTO_INCREMENT = 1;
DELETE FROM team_results WHERE team_id > 0;
ALTER TABLE team_results AUTO_INCREMENT = 1;
DELETE FROM results WHERE id > 0;
ALTER TABLE results AUTO_INCREMENT = 1;
DELETE FROM car_tyres WHERE car_id > 0;
ALTER TABLE car_tyres AUTO_INCREMENT = 1;
DELETE FROM car_numbers WHERE id > 0;
ALTER TABLE car_numbers AUTO_INCREMENT = 1;
DELETE FROM cars WHERE id > 0;
ALTER TABLE cars AUTO_INCREMENT = 1;
DELETE FROM drivers WHERE id > 0;
ALTER TABLE drivers AUTO_INCREMENT = 1;
DELETE FROM teams WHERE id > 0;
ALTER TABLE teams AUTO_INCREMENT = 1;
UPDATE results_in
SET processed = FALSE
WHERE processed = TRUE;
END//
DELIMITER ;
-- -----------------------------------------------------
-- procedure process_results
-- -----------------------------------------------------
DELIMITER //
USE `lemans24`//
CREATE PROCEDURE process_results()
BEGIN
/* Input results - table results_in */
DECLARE res_id INT;
DECLARE res_race_yr INT;
DECLARE res_pos VARCHAR(3);
DECLARE res_car_class VARCHAR(12);
DECLARE res_car_nbr VARCHAR(3);
DECLARE res_team_cntry VARCHAR(16);
DECLARE res_team_name VARCHAR(128);
DECLARE res_drivers_cntry VARCHAR(32);
DECLARE res_drivers_name VARCHAR(256);
DECLARE res_car_chassis VARCHAR(64);
DECLARE res_car_engine VARCHAR(64);
DECLARE res_car_tyres VARCHAR(16);
DECLARE res_laps INT;
DECLARE res_distance DECIMAL(8,3);
DECLARE res_racing_time TIME(3);
DECLARE res_reason VARCHAR(128);
/* Car data */
DECLARE new_car_id INT;
DECLARE new_carnb_id INT;
/* Result */
DECLARE new_res_id INT;
/* Team data */
DECLARE new_team_id INT;
DECLARE new_team_name VARCHAR(64);
DECLARE new_team_cntry CHAR(4);
DECLARE new_team_priv TINYINT;
DECLARE new_team_ord INT;
/* Driver data */
DECLARE new_driver_id INT;
DECLARE new_drv_name VARCHAR(64);
DECLARE new_drv_title VARCHAR(16);
DECLARE new_drv_fname VARCHAR(32);
DECLARE new_drv_lname VARCHAR(32);
DECLARE new_drv_nick VARCHAR(32);
DECLARE new_drv_nm_sfx VARCHAR(4);
DECLARE new_drv_sex CHAR(1);
DECLARE new_drv_cntry CHAR(4);
DECLARE new_drv_ord INT;
/* Cursor and handling */
DECLARE done INT DEFAULT FALSE;
DECLARE cur_results CURSOR FOR
SELECT id,
race_yr,
pos,
car_class,
car_nbr,
team_cntry,
team_name,
drivers_cntry,
drivers_name,
car_chassis,
car_engine,
car_tyres,
laps,
distance,
racing_time,
reason
FROM results_in
WHERE processed IS FALSE;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur_results;
res_loop: LOOP
SET new_car_id = NULL;
SET new_carnb_id = NULL;
SET new_team_id = NULL;
SET new_driver_id = NULL;
FETCH cur_results
INTO res_id,
res_race_yr,
res_pos,
res_car_class,
res_car_nbr,
res_team_cntry,
res_team_name,
res_drivers_cntry,
res_drivers_name,
res_car_chassis,
res_car_engine,
res_car_tyres,
res_laps,
res_distance,
res_racing_time,
res_reason;
IF done THEN
LEAVE res_loop;
END IF;
/* Create car */
IF res_car_class != '' OR res_car_chassis != '' OR res_car_engine != '' THEN
BEGIN
DECLARE CONTINUE HANDLER FOR NOT FOUND SET new_car_id = NULL;
SET res_car_class = NULLIF(res_car_class, '');
SET res_car_engine = NULLIF(res_car_engine, '');
/* Check if car exists */
SELECT id
INTO new_car_id
FROM cars
WHERE ( (car_class IS NULL AND res_car_class IS NULL)
OR car_class = res_car_class
)
AND car_chassis = res_car_chassis
AND ( (car_engine IS NULL AND res_car_engine IS NULL)
OR car_engine = res_car_engine
);
/* else create it */
IF new_car_id IS NULL THEN
INSERT INTO cars (car_class, car_chassis, car_engine)
VALUES (res_car_class, res_car_chassis, res_car_engine);
SET new_car_id = LAST_INSERT_ID();
END IF;
END;
ELSE /* car not defined */
SET new_car_id = NULL;
END IF;
INSERT INTO car_numbers (race_id, car_id, nbr)
VALUES (res_race_yr, new_car_id, res_car_nbr);
SET new_carnb_id = LAST_INSERT_ID();
/* Car tyres */
IF res_car_tyres IS NOT NULL THEN
INSERT INTO car_tyres (car_id, tyre_id)
SELECT new_carnb_id, id
FROM tyres
WHERE brand = res_car_tyres;
END IF;
/* Create result */
INSERT INTO results (race_id, car_id, pos, laps, distance, racing_time, reason)
VALUES (res_race_yr, new_carnb_id, res_pos, res_laps, res_distance, res_racing_time, res_reason);
SET new_res_id = LAST_INSERT_ID();
/* Strip double quotes */
IF res_team_name LIKE '%"' THEN
SET res_team_name = REPLACE(SUBSTR(res_team_name, 1, CHAR_LENGTH(res_team_name) - 1), '""', '"');
END IF;
/* Process team(s) */
SET new_team_ord = 0;
WHILE res_team_name IS NOT NULL DO
SET new_team_name = SUBSTRING_INDEX(res_team_name, '|', 1);
SET new_team_cntry = SUBSTRING_INDEX(res_team_cntry, '|', 1);
SET new_team_priv = FALSE;
SET new_team_ord = new_team_ord + 1;
/* Detect private entrants */
IF new_team_name LIKE '% (private entrant)' THEN
SET new_team_priv = TRUE;
SET new_team_name = REPLACE(new_team_name, ' (private entrant)', '');
END IF;
IF new_team_name != '' THEN
BEGIN
DECLARE CONTINUE HANDLER FOR NOT FOUND SET new_team_id = NULL;
SET new_team_cntry = NULLIF(new_team_cntry, '');
SELECT id
INTO new_team_id
FROM teams
WHERE title = new_team_name
AND ( (country IS NULL AND new_team_cntry IS NULL)
OR country = new_team_cntry
)
AND private_entrant = new_team_priv;
IF new_team_id IS NULL THEN
INSERT INTO teams (title, country, private_entrant)
VALUES (new_team_name, new_team_cntry, new_team_priv);
SET new_team_id = LAST_INSERT_ID();
END IF;
END;
INSERT INTO team_results (team_id, result_id, ord_num)
VALUES (new_team_id, new_res_id, new_team_ord);
END IF;
IF NOT INSTR(res_team_name, '|') THEN
SET res_team_name = NULL;
SET res_team_cntry = NULL;
ELSE
SET res_team_name = SUBSTR(res_team_name , INSTR(res_team_name , '|') + 1);
SET res_team_cntry = SUBSTR(res_team_cntry, INSTR(res_team_cntry, '|') + 1);
END IF;
END WHILE; /* teams */
/* Strip double quotes */
IF res_drivers_name LIKE '%"' THEN
SET res_drivers_name = REPLACE(SUBSTR(res_drivers_name, 1, CHAR_LENGTH(res_drivers_name) - 1), '""', '"');
END IF;
/* Process driver(s) */
SET new_drv_ord = 0;
WHILE res_drivers_name IS NOT NULL DO
SET new_drv_title = NULL;
SET new_drv_name = SUBSTRING_INDEX(res_drivers_name, '|', 1);
SET new_drv_nick = NULL;
SET new_drv_nm_sfx = NULL;
SET new_drv_cntry = SUBSTRING_INDEX(res_drivers_cntry, '|', 1);
SET new_drv_sex = 'M';
SET new_drv_ord = new_drv_ord + 1;
/* Detect females */
IF new_drv_name LIKE 'Mme%' OR
new_drv_name LIKE 'Miss%' OR
new_drv_name LIKE 'Mrs.%'
THEN
SET new_drv_sex = 'F';
SET new_drv_name = SUBSTR(new_drv_name, INSTR(new_drv_name, ' ') + 1);
END IF;
/* Detect titles in front of name */
IF new_drv_name LIKE 'Baron %' OR /* Royal titles */
new_drv_name LIKE 'Comte %' OR /* Comte = Count */
new_drv_name LIKE 'Count %' OR
(new_drv_name LIKE 'Earl %'
AND new_drv_name <> 'Earl Bamber') OR /* except Earl Bamber */
new_drv_name LIKE 'Graf %' OR
new_drv_name LIKE 'Lord %' OR
new_drv_name LIKE 'Marquis %' OR
new_drv_name LIKE 'Prince %' OR
new_drv_name LIKE 'Sir %' OR
new_drv_name LIKE 'Capt.%' OR /* Military titles, Captain */
new_drv_name LIKE 'Colonel %' OR
new_drv_name LIKE 'Lt. %' OR /* Lieutenant */
new_drv_name LIKE 'Maj %' OR /* Major */
new_drv_name LIKE 'Dr.%' /* Academic titles, Doctor */
THEN
IF new_drv_name LIKE 'Lt. Cdr.%' THEN /* Lieutenant commander */
SET new_drv_title = SUBSTR(new_drv_name, 1, 8);
SET new_drv_name = SUBSTR(new_drv_name, 10);
ELSE
SET new_drv_title = SUBSTR(new_drv_name, 1, INSTR(new_drv_name, ' ') - 1);
SET new_drv_name = SUBSTR(new_drv_name, INSTR(new_drv_name, ' ') + 1);
END IF;
END IF;
/* Detect titles after name */
IF new_drv_name LIKE '%, Baron%' OR
new_drv_name LIKE '%, Lord%' OR
new_drv_name LIKE '%, Earl%'
THEN
SET new_drv_title = SUBSTR(new_drv_name, INSTR(new_drv_name, ',') + 2);
SET new_drv_name = SUBSTR(new_drv_name, 1, INSTR(new_drv_name, ',') - 1);
END IF;
/* Detect name suffixes */
IF new_drv_name LIKE '% Jr.' OR
new_drv_name LIKE '% Sr.' OR
new_drv_name LIKE '% III' /* third */
THEN
SET new_drv_nm_sfx = SUBSTRING_INDEX(new_drv_name, ' ', -1);
SET new_drv_name = SUBSTR(new_drv_name, 1, LENGTH(new_drv_name) - 4);
END IF;
IF new_drv_name != '' AND new_drv_cntry != '' THEN
BEGIN
DECLARE tmp_drv_name VARCHAR(64) DEFAULT new_drv_name;
/* Extract nickname */
IF new_drv_name LIKE '%"%"%' THEN /* name with two double quotes */
BEGIN
DECLARE fst_qt_idx INTEGER DEFAULT INSTR(new_drv_name, '"');
DECLARE snd_qt_idx INTEGER DEFAULT LOCATE('"', new_drv_name, fst_qt_idx + 1);
SET tmp_drv_name = SUBSTRING(new_drv_name, 1, fst_qt_idx - 1);
SET tmp_drv_name = CONCAT(tmp_drv_name, TRIM(SUBSTRING(new_drv_name, snd_qt_idx + 1)));
SET new_drv_nick = SUBSTRING(new_drv_name, fst_qt_idx + 1, snd_qt_idx - fst_qt_idx - 1);
END;
END IF;
IF new_drv_name LIKE '%''%''%' THEN /* name with two single quotes */
BEGIN
DECLARE fst_qt_idx INTEGER DEFAULT INSTR(new_drv_name, '''');
DECLARE snd_qt_idx INTEGER DEFAULT LOCATE('''', new_drv_name, fst_qt_idx + 1);
SET tmp_drv_name = SUBSTRING(new_drv_name, 1, fst_qt_idx - 1);
SET tmp_drv_name = CONCAT(tmp_drv_name, TRIM(SUBSTRING(new_drv_name, snd_qt_idx + 1)));
SET new_drv_nick = SUBSTRING(new_drv_name, fst_qt_idx + 1, snd_qt_idx - fst_qt_idx - 1);
END;
END IF;
/* clean braces in name and trim */
SET new_drv_name = REPLACE(tmp_drv_name, '(', '');
SET new_drv_name = REPLACE(new_drv_name, ')', '');
SET new_drv_name = TRIM(new_drv_name);
/* Split driver name - first name to the first space */
SET new_drv_fname = SUBSTRING_INDEX(new_drv_name, ' ', 1);
/* last name everything else */
SET new_drv_lname = SUBSTR(new_drv_name, INSTR(new_drv_name, ' ') + 1);
IF new_drv_fname = new_drv_lname THEN
SET new_drv_fname = NULL;
END IF;
IF new_drv_fname = 'f.n.u.' OR new_drv_fname = '' THEN /* first name unknown */
SET new_drv_fname = NULL;
END IF;
BEGIN
DECLARE CONTINUE HANDLER FOR NOT FOUND SET new_driver_id = NULL;
/* Query shoud be unique or non-unique index lookup on idx_driver_unq */
SELECT id
INTO new_driver_id
FROM drivers
WHERE ( (title IS NULL AND new_drv_title IS NULL) /* no title */
OR title = new_drv_title
)
/* if first name unknown */
AND ( (fname IS NULL AND new_drv_fname IS NULL)
OR fname = new_drv_fname
)
AND lname = new_drv_lname
/* if no nickname */
AND ( (nickname IS NULL AND new_drv_nick IS NULL )
OR nickname = new_drv_nick
)
/* if no name suffix */
AND ( (nm_suffix IS NULL AND new_drv_nm_sfx IS NULL )
OR nm_suffix = new_drv_nm_sfx
)
AND sex = new_drv_sex
AND country = new_drv_cntry;
IF new_driver_id IS NULL THEN
INSERT INTO drivers
(title,
fname, lname, nm_suffix, nickname,
sex, country)
VALUES
(new_drv_title,
new_drv_fname, new_drv_lname, new_drv_nm_sfx, new_drv_nick,
new_drv_sex, new_drv_cntry);
SET new_driver_id = LAST_INSERT_ID();
END IF;
END;
END;
END IF;
IF new_driver_id IS NOT NULL THEN
INSERT INTO driver_results (driver_id, result_id, ord_num)
VALUES (new_driver_id, new_res_id, new_drv_ord);
END IF;
IF NOT INSTR(res_drivers_name, '|') THEN
SET res_drivers_name = NULL;
SET res_drivers_cntry = NULL;
ELSE
SET res_drivers_name = SUBSTR(res_drivers_name , INSTR(res_drivers_name , '|') + 1);
SET res_drivers_cntry = SUBSTR(res_drivers_cntry, INSTR(res_drivers_cntry, '|') + 1);
END IF;
END WHILE; /* drivers */
/* Mark record processed */
UPDATE results_in
SET processed = TRUE
WHERE id = res_id;
END LOOP; /* res_loop */
CLOSE cur_results;
END//
DELIMITER ;
-- -----------------------------------------------------
-- function num_to_pos
-- -----------------------------------------------------
DELIMITER //
USE `lemans24`//
CREATE FUNCTION num_to_pos(num INT)
RETURNS VARCHAR(3)
DETERMINISTIC
BEGIN
RETURN CASE
WHEN num BETWEEN 1 AND 90
THEN CAST(num AS CHAR)
WHEN num = 92 THEN 'NC'
WHEN num = 93 THEN 'DNF'
WHEN num = 94 THEN 'DSQ'
WHEN num = 95 THEN 'DNS'
WHEN num = 96 THEN 'DNQ'
WHEN num = 97 THEN 'DNP'
WHEN num = 98 THEN 'RES'
WHEN num = 99 THEN 'DNA'
END;
END//
DELIMITER ;
-- -----------------------------------------------------
-- function pos_to_num
-- -----------------------------------------------------
DELIMITER //
USE `lemans24`//
CREATE FUNCTION pos_to_num(pos VARCHAR(3))
RETURNS INT
DETERMINISTIC
BEGIN
RETURN CASE
WHEN pos RLIKE '^[1-9][0-9]*$'
THEN CAST(pos AS UNSIGNED)
WHEN pos = 'NC' THEN 92
WHEN pos = 'DNF' THEN 93
WHEN pos = 'DSQ' THEN 94
WHEN pos = 'DNS' THEN 95
WHEN pos = 'DNQ' THEN 96
WHEN pos = 'DNP' THEN 97
WHEN pos = 'RES' THEN 98
WHEN pos = 'DNA' THEN 99
END;
END//
DELIMITER ;
-- -----------------------------------------------------
-- function drv_full_name
-- -----------------------------------------------------
DELIMITER //
USE `lemans24`//
CREATE FUNCTION drv_full_name(title VARCHAR(16),
fname VARCHAR(32),
lname VARCHAR(32),
nickname VARCHAR(32),
nm_suffix VARCHAR(4))
RETURNS VARCHAR(64) NO SQL
BEGIN
DECLARE full_name VARCHAR(64) DEFAULT '';
/* first add royal title in front if title with name */
IF title NOT LIKE 'Baron %' AND
title NOT LIKE 'Earl %' AND
title NOT LIKE 'Lord %'
THEN
SET full_name := CONCAT(full_name, title, ' ');
END IF;
/* then add first name if known */
IF fname IS NOT NULL THEN
SET full_name := CONCAT(full_name, fname, ' ');
END IF;
/* then add nickname if any */
IF nickname IS NOT NULL THEN
SET full_name := CONCAT(full_name, '"', nickname, '" ');
END IF;
/* then add last name */
SET full_name := CONCAT(full_name, lname);
/* then add title after name */
IF title LIKE 'Baron %' OR
title LIKE 'Earl %' OR
title LIKE 'Lord %'
THEN
SET full_name := CONCAT(full_name, ', ', title);
END IF;
/* finally add name suffix if any */
IF nm_suffix IS NOT NULL THEN
SET full_name := CONCAT(full_name, ' ', nm_suffix);
END IF;
RETURN full_name;
END//
DELIMITER ;
-- -----------------------------------------------------
-- function check_car_number
-- -----------------------------------------------------
DELIMITER //
USE `lemans24`//
CREATE FUNCTION check_car_number(car_nbr VARCHAR(3))
RETURNS BOOL NO SQL
BEGIN
/* Car numbers should be non-zero padded integers */
IF car_nbr RLIKE '^[1-9][0-9]{0,2}$'
/* or 007, 008 and 009 like between 2006 and 2011 */
OR car_nbr RLIKE '^00[7-9]$'
/* or just zero */
OR car_nbr RLIKE '^0$'
THEN
RETURN TRUE;
ELSE
RETURN FALSE;
END IF;
END//
DELIMITER ;
-- -----------------------------------------------------
-- View `lemans24`.`Circuits`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `lemans24`.`Circuits`;
USE `lemans24`;
CREATE OR REPLACE VIEW Circuits AS
SELECT CASE
WHEN YEAR(C.since) != (SELECT MIN(YEAR(since))
FROM circuits
WHERE YEAR(since) > YEAR(C.since)
) - 1
THEN
CONCAT(YEAR(C.since), '-', (SELECT MIN(YEAR(since))
FROM circuits
WHERE YEAR(since) > YEAR(C.since)
) - 1
)
ELSE
YEAR(C.since)
END AS Years,
C.length_km AS Length_km,