Need a tip on simple MySQL db design

前端 未结 3 910
遥遥无期
遥遥无期 2021-01-03 12:57

I am trying to make a simple item database using MySQL for a game. Here is what my 3 tables would look like

     items
     itemId | itemName 
    --------------         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-03 13:33

    You are dealing with a two common problems:

    • Entities that are similar to each other but not identical (all items have a name and description, but not necessarily an intellect).
    • A design in which you need to add attributes once the database is in production (you can pretty easily predict that at some point you'll need to add, for instance, a magic-resistance attribute to some items).

    You've solved your problem by reinventing the EAV system in which you store both attribute names and values as data. And you've rediscovered some of the problems with this system (type checking, relational integrity).

    In this case, I'd personally go with a solution midway between the relational one and the EAV one. I'd take the common columns and add them, as columns, to either the items table or, if items represents kinds of items, not individual ones, to the items_owners table. Those columns would include description and possibly type and in the example you gave, pretty much match up with the text columns. I'd then keep the existing layout for those attributes that are numerical ratings, making the value type int. That gives you type-checking and proper normalization across the integer attributes (you won't be storing lots of NULLs) at the expense of the occasional NULL type or description.

提交回复
热议问题