Wysie Wysie - 1 year ago 326
SQL Question

Best way to implement a database of lottery numbers?

I'm trying to build an app involving my local lottery system and would need to store the numbers. Each draw has the following:

  • Date

  • Draw Number

  • Winning Numbers (6 of them)

  • Additional Winning Number

I would like to be able to make the following information out of these numbers, such as:

  • Frequency of each number appearing in general

  • Frequency of each number appearing in the standard 6 winning numbers

  • Frequency of each number appearing as an additional winning number

  • Retrieve a number and the associated dates for which the number appeared

  • And of course, simply display the numbers given a date and/or draw number

My issue is regarding the winning numbers and the additional winning number. I can think of the following:
- Storing the 6 winning numbers as a list or similar (DynamoDB), and the additional number separately
- Storing each number separately: winning1, winning2, etc... additional_number

I'm open to suggestions and am not fixed on using a particular database yet, hope to hear your thoughts and expertise on this. I'm thinking that at the end of the day I will need additional tables to keep track of the frequency of each number etc. separately.


Answer Source

Since you are planning on querying the database with different queries I would recommend a standard SQL database and not a noSql approach. NoSQL databases really shine if the data to store is not so well defined. However, you have a clear pattern of data to store. Here is what I would suggest:

table drawing:
id | date | draw_number | lottery_id

table winning_number:
id | number | position | drawing_id

With that design you could store the info if a number is drawn as extra number in the position field. If you know in what order all numbers are drawn you can store this too in the field.

id should be the primary key in each table. winning_number.drawing_id is a foreign key to the drawing table.

lottery_id is also meant as foreign key to a table that describes the parameters of the lottery system. maybe something like this:

table lottery
id | total_numbers | country | whatever

Queries about frequency should now be easy. Ask, if you have further questions.