title: Database model for a 24/7 Staff roster at a casino
body: <p>We presently use a pen/paper based roster to manage table games staff at the casino. Each row is an employee, each column is a 20 minute block of time and each cell represents what table the employee is assigned to, or alternatively they've been assigned to a break. The start and end time of shifts for employees vary as do the games/skills they can deal. We need to keep a copy of the rosters for 7 years, with paper this is fairly easy, I'm wanting to develop a digital application and am having difficulty how to store the data in a database for archiving. </p>
  
  <p>I'm fairly new to working with databases, I think I understand how to model the data for a graph database like neo4j, but I had difficulty when it came to working with time. I've tried to learn about RDBMS databases like MySQL, below is how I think the data should be modelled. Please point out if I'm going in the wrong direction or if a different database type would be more appropriate, it would be greatly appreciated!</p>
  
  <p><strong>Basic Data</strong><br>
  Here is some basic data to work with before we factor in scheduling/time.</p>
  
  <p>Employee<br>
   - ID Number<br>
   - Name<br>
   - Skills (Blackjack, Baccarat, Roulette, etc)  </p>
  
  <p>Table<br>
   - ID Number<br>
   - Skill/Type (Can only be one skill)  </p>
  
  <p>It may be better to store the roster data as a file like JSON instead? Time sensitive data wouldn't be so much of a problem then. The benefit of going digital with a database would be queries, these could help assist time consuming tasks where human error is common. </p>
  
  <p><strong>Possible Queries</strong><br>
  <em>Note: Staff that are on shift are either on a break or on the floor (assigned to a table), Skills have a major or minor type based on difficulty to learn.</em></p>
  
  <ul>
  <li>What staff have been on the floor for 80 minutes or more? (They are due for a break)</li>
  <li>What open tables can I assign this employee to based on their skillset?</li>
  <li>I need an employee that has Baccarat skill but is not already been assigned to a Baccarat table.</li>
  <li>What employee(s) was on this table during this period of time?</li>
  <li>Where was this employee at this point in time?</li>
  <li>Who is on shift right now?</li>
  <li>How many staff on shift can deal Blackjack?</li>
  <li>How many staff have 3 major skills?</li>
  <li>What staff have had the Baccarat skill for at least 3 months?</li>
  </ul>
  
  <p>These queries could also be sorted by alphabetical order or time, skill etc.</p>
  
  <p>I'm pretty sure I know how to perform these queries with cypher for neo4j provided I model the data right. I'm not as knowledgeable with SQL queries, I've read it can get a bit complicated depending on the query and structure. </p>
  
  <p><strong>----------------------------------------------------------------------------------------</strong></p>
  
  <h1><strong>MYSQL Specific</strong></h1>
  
  <p>An employee table could contain properties such as their ID number and Name, but am I right that for their skills and shifts these would be separate tables that reference the employee by a unique integer(I think this is called a foreign key?).</p>
  
  <p>Another table could store the gaming Tables, these would have their own ID and reference a skill/gametype with a foreign key.</p>
  
  <p>To record data like the pen/paper roster, each day could have a table with columns starting from 0000 increasing by 20 in value going all the way to 2340? Prior to the time columns I could have one for staff where each employee is represented with their foreign key, the time columns would then have foreign keys to the assigned gaming Tables, the row data is bound to have many cells that aren't populated since the employee shift won't be 24/7. If I'm using foreign keys to reference gaming Tables I now have a problem when the employee is on break? Unless I treat say the first gaming Table entry as a break?</p>
  
  <p>I may need to further complicate things though, management will over time try different gaming Table layouts, some of the gaming Tables can be converted from say Blackjack to Baccarat. this is bound to happen quite a bit over 7 years, would I want to be creating new gaming Table entries or add a column to use a foreign key and refer to a new table that stores the history of game types during periods of time? Employees will also learn to deal new games during their career, very rarely they may also have the skill removed.</p>
  
  <p><strong>----------------------------------------------------------------------------------------</strong></p>
  
  <h1><strong>Neo4j Specific</strong></h1>
  
  <p>With this data would I have an Employee and a Table node that have "isA" relationship edges mapping to actual employees or tables?
  I imagine with the skills for the two types I would be best with a Skill node and establish relationships like so?: Blackjack->isA->Skill, Employee->hasSkill->Blackjack, Table->typeIs->Blackjack?</p>
  
  <p><strong>TIME</strong><br>
  I find difficulty when I want this database to now work with a timeline. I've come across the following suggestions for connecting nodes with time:  </p>
  
  <ul>
  <li>Unix Epoch seems to be a common recommendation?</li>
  <li>Connecting nodes to a year/month/day graph?</li>
  <li>Lucene timeline? (I don't know much about this or how to work with it, have seen some  mention it)</li>
  </ul>
  
  <p>And some cases with how time and data relate:</p>
  
  <ul>
  <li><p>Staff have varied days and start/end times from week to week, this could be shift node with properties {shiftStart,shiftEnd,actualStart,actualEnd}, staff may arrive late or get sick during shift. Would this be the right way to link each shift to an employee? Employee(node)->Shifts(groupNode)->Shift(node)  </p></li>
  <li><p>Tables and Staff may have skill data modified, with archived data this could be an issue, I think the solution is to have time property on the relationship to the skill?  </p></li>
  <li><p>We open and close tables throughout the day, each table has open/close times for each day, this could change in a month depending on what management wants, in addition the times are not strict, for various reasons a manager may open or close tables during the shift. The open/closed status of a table node may only be relevant for queries during the shift, which confuses me as I'd want this for queries but for archiving with time it might not make sense?</p></li>
  </ul>
  
  <p>It's with queries that I have trouble deciding when to use a node or add a property to a node. For an Employee they have a name and ID number, if I wanted to find an employee by their ID number would it be better to have that as a node of it's own? It would be more direct right, instead of going through all employees for that unique ID number. </p>
  
  <p>I've also come across labels just recently, I can understand that those would be useful for typing employee and table nodes rather than grouping them under a node. With the shifts for an employee I think should continue to be grouped with a shifts node, If I were to do cypher queries for employees working shifts through a time period a label might be appropriate, however should it be applied to individual shift nodes or the shifts group node that links back to the employee? I might need to add a property to individual shift nodes or the relationship to the shifts group node? I'm not sure if there should be a shifts group node, I'm assuming that reducing the edges connecting to the employee node would be optimal for queries.</p>
  
  <p><strong>----------------------------------------------------------------------------------------</strong></p>
  
  <p>If there are any great resources I can learn about database development that'd be great, there is so much information and options out there it's difficult to know what to begin with. Thanks for your time :)</p>