Football Results Database – part 1

In order to store the results of the NFL games, we’re going to need some tables. My initial design is as follows;

  • a table to store details of the teams in the league. For simplicity, we’ll assume that we only want to store results for a single season (for now), so the teams won’t change divisions. The ‘teams’ table just needs to store a ‘short-name’ for each team (which might as well act as the Primary Key for the table), the full name of the team, which conference it plays in, and which division. That should do for now.
  • a table to store details of the two Conferences. We might as well use AFC and NFC as the Primary Key for this table
  • a table to store details of each game; the week number, the date, maybe the location (maybe we’ll add this later) and whether the game went into overtime
  • a table to sore the results. a result will take up two rows of the table, one row for each team. I’ve added a ‘home’ field, which will be 1 if that row contains the score of the home team.

So, here is my basic table schema for MySQL;

CREATE TABLE `conferences` (
  `short` char(3) NOT NULL,
  `name` varchar(50) NOT NULL,
  PRIMARY KEY (`short`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert  into conferences values 
('AFC', 'American Football Conference'), 
('NFC', 'National Football Conference');

CREATE TABLE `games` (
  `game_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `week` int(11) NOT NULL,
  `year` int(11) NOT NULL,
  `date` date NOT NULL,
  `overtime` tinyint(4) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`game_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `results` (
  `game_id` int(11) unsigned NOT NULL,
  `team` char(2) NOT NULL,
  `points` int(11) NOT NULL,
  `home` tinyint(4) NOT NULL DEFAULT '0',
  KEY `game_id` (`game_id`),
  KEY `team_fk` (`team`),
  CONSTRAINT `game_fk` FOREIGN KEY (`game_id`) REFERENCES `games` (`game_id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `team_fk` FOREIGN KEY (`team`) REFERENCES `teams` (`short`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `teams` (
  `short` char(2) NOT NULL,
  `name` varchar(100) NOT NULL,
  `conference` char(3) NOT NULL,
  `division` varchar(5) NOT NULL,
  PRIMARY KEY (`short`),
  KEY `conf` (`conference`) USING BTREE,
  CONSTRAINT `conf` FOREIGN KEY (`conference`) REFERENCES `conferences` (`short`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO teams VALUES 
('AC', 'Arizona Cardinals', 'NFC', 'West'), 
('AF', 'Atlanta Falcons', 'NFC', 'South'), 
('BB', 'Buffalo Bills', 'AFC', 'East'), 
('BR', 'Baltimore Ravens', 'AFC', 'North'), 
('CB', 'Chicago Bears', 'NFC', 'North'), 
('CL', 'Cleveland Browns', 'AFC', 'North'), 
('CN', 'Cincinnati Bengals', 'AFC', 'North'), 
('CP', 'Carolina Panthers', 'NFC', 'South'), 
('DB', 'Denver Broncos', 'AFC', 'West'), 
('DC', 'Dallas Cowboys', 'NFC', 'East'), 
('DL', 'Detroit Lions', 'NFC', 'North'), 
('GB', 'Green Bay Packers', 'NFC', 'North'), 
('HT', 'Houston Texans', 'AFC', 'South'), 
('IC', 'Indianapolis Colts', 'AFC', 'South'), 
('JJ', 'Jacksonville Jaguars', 'AFC', 'South'), 
('KC', 'Kansas City Chiefs', 'AFC', 'West'), 
('MD', 'Miami Dolphins', 'AFC', 'East'), 
('MV', 'Minnesota Vikings', 'NFC', 'North'), 
('NE', 'New England Patriots', 'AFC', 'East'), 
('NG', 'New York Giants', 'NFC', 'East'), 
('NJ', 'New York Jets', 'AFC', 'East'), 
('NO', 'New Orleans Saints', 'NFC', 'South'), 
('OR', 'Oakland Raiders', 'AFC', 'West'), 
('PE', 'Philadelphia Eagles', 'NFC', 'East'), 
('PS', 'Pittsburg Steelers', 'AFC', 'North'), 
('SD', 'San Diego Chargers', 'AFC', 'West'), 
('SF', 'San Francisco 49ers', 'NFC', 'West'), 
('SL', 'St. Louise Rams', 'NFC', 'West'), 
('SS', 'Seattle Seahawks', 'NFC', 'West'), 
('TB', 'Tampa Bay Buccaneers', 'NFC', 'South'), 
('TT', 'Tennessee Titans', 'AFC', 'South'), 
('WR', 'Washington Redskins', 'NFC', 'East');

Football Results Database – Prologue

I guess you should always blog about something you know about, so I’m going to start rambling on about databases and (American) Football. Many years ago, I had to choose a topic for my Computer Studies O-level. I came up with two options – a London Underground route-planner or a database to store NFL results. I chose the later, after my 15-year-old brain couldn’t figure out shortest-path algorithms (strange that I should end up specializing in traffic-assignment algorithms, but that’s another story).

My results database was written for my BBC micro, using BBC basic. I was very proud of the fact that it didn’t contain a single GOTO statement! It didn’t actually use a proper database either – these were the days before MySQL.

Here’s how it worked;

  • Each team was given a two-letter code to save you having to type in its full name every time. Tampa Bay was “TB”, San Francisco was “SF”. I realised that there were quite a few teams that could have used the “CB” code – Chicago Bears, Cleveland Browns or Cincinnati Bengals. I think I decided Chicago got “CB”.
  • You typed in the two-letter code of the home team, then the number of points score Repeat for the away team.
  • The program then saved all the results to disk, and would generate the league standings. It even could take into account some of the NFL’s complicated tie-breaker procedures.
  • It could also show each week’s results in a nice MODE7 table, just like on teletext.

So, I’m now going to create a similar program using PHP and MySQL, and, hopefully, learn something new.