So to follow up on another thread here
Return count even when no values
I wonder what to choose as the primary key for a new "lookup" table I'm creating. What I have now is the following table:
Code:
CREATE TABLE `mc_routes` (
`route_id` int(10) unsigned NOT NULL auto_increment,
`route_name` varchar(255) NOT NULL,
`route_grade` enum('3','4','5a','5a+','5b','5b+','5c','5c+','6a','6a+','6b','6b+','6c','6c+','7a','7a+','7b','7b+','7c','7c+','8a','8a+','8b','8b+','8c','8c+','9a','9a+','9b','9b+') NOT NULL,
`sector_id` INT(10) unsigned NULL,
`area_id` int(10) unsigned NULL,
PRIMARY KEY (`route_id`),
UNIQUE KEY (`route_name`, `route_grade`, `area_id`),
FOREIGN KEY (`sector_id`) REFERENCES mc_sectors (`sector_id`),
FOREIGN KEY (`area_id`) REFERENCES `mc_areas` (`area_id`)
) ENGINE=InnoDB;
As you can see, the grades start at "3" and go up until 9b+. Currently the most difficult climb in the world is about 9b. In the future it could increase. Not sure how much though.
However, because of the reason mentioned in the other thread, I'm going to create a separate table for the grades. However, I'm unsure what to do. Either use the natural key:
Code:
CREATE TABLE `mc_grades` (
`grade` VARCHAR(3) NOT NULL,
PRIMARY KEY (`grade`)
) ENGINE=InnoDB;
or a surrogate autoincrementing id, like:
Code:
CREATE TABLE `mc_grades` (
`grade_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`grade` VARCHAR(3) NOT NULL,
PRIMARY KEY (`grade_id`)
) ENGINE=InnoDB;
The advantage of the surrogate key would be that in case I do need to change the grades in the future, it's quite easy to do so since I only have to change a dozen lines in the grades table.
And the second question is, what to do with the field route_grade in the mc_routes table. Leave it as the enum field? Or change it to a varchar when I use the natural key for the grades table or use an int when I use a surrogate key for the grades table.
I know the general subject of natural vs surrogate keys has been discussed quite often and I have read a couple of threads. But it seems there's not a real consensus on what to do, or a "it depends" situation. So I'm unsure what to do in my specific example. Any help is appreciated.