grandnational
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
grandnational [2021/04/25 09:27] – created z0hpvk | grandnational [2025/03/08 22:24] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | < | + | ===== Grand National Sweepstake (Oracle) ===== |
+ | |||
+ | ==== Create User ==== | ||
+ | |||
+ | < | ||
+ | CREATE USER GRANDNATIONAL IDENTIFIED BY horses; | ||
+ | GRANT CREATE SESSION TO GRANDNATIONAL; | ||
+ | GRANT CREATE TABLE TO GRANDNATIONAL; | ||
+ | GRANT CREATE PROCEDURE TO GRANDNATIONAL; | ||
+ | GRANT CREATE VIEW TO GRANDNATIONAL; | ||
+ | GRANT CREATE SEQUENCE TO GRANDNATIONAL; | ||
+ | GRANT UNLIMITED TABLESPACE TO GRANDNATIONAL; | ||
+ | </ | ||
+ | |||
+ | ==== Create tables and insert data ==== | ||
+ | |||
+ | <code sql> | ||
-- Lists the entrants in the sweepstake | -- Lists the entrants in the sweepstake | ||
Drop table participants; | Drop table participants; | ||
Create table participants (ID number(2) generated by default on null as identity, | Create table participants (ID number(2) generated by default on null as identity, | ||
Name varchar2(20), | Name varchar2(20), | ||
- | | + | constraint unique_name unique (name)); |
insert into participants (name) values (' | insert into participants (name) values (' | ||
insert into participants (name) values (' | insert into participants (name) values (' | ||
Line 11: | Line 27: | ||
insert into participants (name) values (' | insert into participants (name) values (' | ||
insert into participants (name) values (' | insert into participants (name) values (' | ||
+ | |||
+ | -- List of horses entered into the Grand National | ||
+ | Drop table horses; | ||
+ | Create table horses (HorseNo number(2), | ||
+ | Name varchar2(40), | ||
+ | insert into horses values (1,' | ||
+ | insert into horses values (2,' | ||
+ | insert into horses values (3,' | ||
+ | insert into horses values (4,' | ||
+ | insert into horses values (5,' | ||
+ | insert into horses values (6,' | ||
+ | insert into horses values (7,' | ||
+ | insert into horses values (8,' | ||
+ | insert into horses values (9,' | ||
+ | insert into horses values (10,' | ||
+ | insert into horses values (11,' | ||
+ | insert into horses values (12,' | ||
+ | insert into horses values (13,' | ||
+ | insert into horses values (14,' | ||
+ | insert into horses values (15,' | ||
+ | insert into horses values (16,' | ||
+ | insert into horses values (17,' | ||
+ | insert into horses values (18,' | ||
+ | insert into horses values (19,' | ||
+ | insert into horses values (20,' | ||
+ | insert into horses values (21,' | ||
+ | insert into horses values (22,' | ||
+ | insert into horses values (23,' | ||
+ | insert into horses values (24,' | ||
+ | insert into horses values (25,' | ||
+ | insert into horses values (26,' | ||
+ | insert into horses values (27,' | ||
+ | insert into horses values (28,' | ||
+ | insert into horses values (29,' | ||
+ | insert into horses values (30,' | ||
+ | insert into horses values (31,' | ||
+ | insert into horses values (32,' | ||
+ | insert into horses values (33,' | ||
+ | insert into horses values (34,' | ||
+ | insert into horses values (35,' | ||
+ | insert into horses values (36,' | ||
+ | insert into horses values (37,' | ||
+ | insert into horses values (38,' | ||
+ | insert into horses values (39,' | ||
+ | insert into horses values (40,' | ||
+ | |||
+ | -- Lists the entrants and the horses that they got | ||
+ | drop table sweepstake; | ||
+ | create table sweepstake (PlayerID number(2), | ||
+ | | ||
+ | HorseName varchar2(40), | ||
+ | </ | ||
+ | |||
+ | ==== Create procedures ==== | ||
+ | <code plsql> | ||
+ | -- Initialise the SWEEPSTAKE table | ||
+ | create or replace procedure init as | ||
+ | begin | ||
+ | EXECUTE IMMEDIATE ' | ||
+ | EXECUTE IMMEDIATE ' | ||
+ | end; | ||
+ | / | ||
+ | |||
+ | -- Performs one round of the draw. | ||
+ | create or replace procedure OneCycle as | ||
+ | Cursor C1 is select PlayerID, PlayerName, horseno, HorseName from ( | ||
+ | | ||
+ | HorseNo, HorseName from sweepstake where playerid is null), | ||
+ | (select id PlayerID, name PlayerName from participants) | ||
+ | where row_num = PlayerID; | ||
+ | BEGIN | ||
+ | FOR a IN C1 | ||
+ | LOOP | ||
+ | EXECUTE IMMEDIATE ' | ||
+ | using a.PlayerID, a.HorseNo; | ||
+ | END LOOP; | ||
+ | END; | ||
+ | / | ||
+ | |||
+ | -- Generate the sweepstake data | ||
+ | create or replace procedure GenSweepstake as | ||
+ | Cursor C1 is select name, horseno, horsename | ||
+ | from participants, | ||
+ | where id = playerid order by horseno; | ||
+ | NoParticipants number(2); | ||
+ | NoHorses | ||
+ | NoCycles | ||
+ | begin | ||
+ | select count(*) into NoParticipants from participants; | ||
+ | select count(*) into NoHorses from horses; | ||
+ | NoCycles := ceil(NoHorses/ | ||
+ | init; | ||
+ | FOR a IN 1..NoCycles | ||
+ | LOOP | ||
+ | OneCycle; | ||
+ | END LOOP; | ||
+ | |||
+ | dbms_output.put_line(' | ||
+ | dbms_output.put_line(' | ||
+ | dbms_output.put_line(' | ||
+ | FOR b in C1 | ||
+ | LOOP | ||
+ | | ||
+ | END LOOP; | ||
+ | end; | ||
+ | / | ||
+ | </ | ||
+ | |||
+ | ==== Create Views ==== | ||
+ | |||
+ | <code sql> | ||
+ | create or replace view v_sweepstake as | ||
+ | | ||
+ | from participants, | ||
+ | where id = playerid | ||
+ | order by horseno; | ||
+ | |||
+ | create or replace view v_check as | ||
+ | | ||
+ | group by name | ||
+ | order by 2 DESC; | ||
+ | </ | ||
+ | |||
+ | ==== Generate Sweepstake ==== | ||
+ | |||
+ | <code plsql> | ||
+ | set serveroutput on | ||
+ | BEGIN | ||
+ | GenSweepstake(); | ||
+ | END; | ||
+ | / | ||
</ | </ |
grandnational.1619342873.txt.gz · Last modified: 2025/03/08 22:23 (external edit)