简体   繁体   中英

Storing multiple Foreign Keys in MySQL

I am in a design dilemma, I am designing a database for a house rental application, the point that got me stuck is the amenities of the house, given that a house can have multi amenities like Ceiling board,Dishwasher, Refrigerator, Paved yard, Wall fence etc How do I store such a house in the database? Some suggestion I have been given are(Simplified tables):

`  |id |description|amenities|
   |1  |           |         | 
  1. Store amenity ids eg 1,2,3,4 in the amenities column

  2. Store the amenity strings in the amenities column

Between the two option 1 is best because am saving memory but isn't there a better way to store multiple ids than this?

You can use EAV model for that, you will save memory, but will lose performance on select, or use sparse columns

EAV approach:

EAV table:

  • entityId
  • attributeId
  • value

Attribute table:

  • attributeId
  • attribute name

Entity table:

  • entityId
  • entity name

Attributes would be

Ceiling board,Dishwasher, Refrigerator, Paved yard, Wall fence etc

Entities

house 1, house 2

Sparse columns:

House table:

  • House id
  • Your attribute(Nullable)
  • Your attribute2(Nullable)
  • Your attribute3(Nullable)

Simple mapping:

House table:

  • houseId
  • house name

Amenities table:

  • amenityId
  • name

HouseAmenitiesMapping:

  • houseId
  • amenityId

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM