Search code examples
mysqldatabase-designrelationships

MySQL Database Relationships with Specific attributes


I'm creating a resorts database and backend.

This backend allows the admin to create new resorts and then specify which amenities are included in a resort.

Amenities can be : tennis court, pool, etc.

Sounds easy right? The problem I'm running into is each amenity needs an attribute: [near by] or [on location].

The way I'm thinking of doing it is:

CREATE TABLE IF NOT EXISTS `amenity_resort_assn` (
  `amenities_id` int(10) unsigned NOT NULL,
  `resorts_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`amenities_id`,`resorts_id`),
  KEY `IDX_amenityresortassn_1` (`amenities_id`),
  KEY `IDX_amenityresortassn_2` (`resorts_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `resorts` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
   PRIMARY KEY (`id`),
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT ;


CREATE TABLE IF NOT EXISTS `amenities` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `amenities_onsite_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `IDX_amenities_1` (`amenities_onsite_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT ;


CREATE TABLE IF NOT EXISTS `onsite_type` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `IDX_amenitiesonsitetype_1` (`name`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT;

Solution

  • You are right to leave open the possibility of having some different onsite attribute besides "on location" and "near by". Maybe you add "within 20 miles" or "in your room" later.

    So I suggest doing the tables this way:

    RESORTS: id, name
    AMENITIES: id, name
    ONSITE_TYPE: id, name
    AMENITY_RESORT_ASSN: id, resorts_id, amenities_id, onsite_type_id
    

    This seems to be how the relationship of these things works in real life--each of these characteristics is independent of the others. And this leaves the possibility that a resort has both a restaurant on site and a restaurant near by.