简体   繁体   中英

How to normalize or design my MySQL database

I want to know which one structure is good to store about 50 data in same time into MySQL?

First

 data_id  user_id      data_key    data_content
 ---------------------------------------------------
 1        2            data_key1   content 1
 2        2            data_key2   content 2
 3        2            data_key3   content 3
 ..       ..           ..          ..
 ..       ..           ..          ..
 50       2            data_key50  content 50

Seconds

 data_id  user_id  data_key1  data_key2  data_key3  .. ..  data_key50
 -------------------------------------------------------------------
 1        2        content 1  content 2  content 3  .. ..  content 50

Or have other solution? user_id will have more than 5000 users.

Definitely the first one! What if the need arises to have more than 50 different "data keys". The first solution is way more flexible and unpolluted.

Do you know the data keys before hand and are they likely to change? If they're along the lines of "first_name, last_name, hair_color, eye_color, birthday, ..." then the second model would be feasible since you'll often want to retrieve all of these for a user at the same time.

The first model is like having a persistent hashtable, so you'd have to store all the values as strings, and your app will have to know which keys to query for. This can make sense if the keys are user defined, however.

I do not think the second solution is a goeod one. If I do not suppose wrong not all tue users are 2, but you have different numbers what you should do is normalize that table, divide it in to.

One that has:

Table main: id , data_id, user_id

Table key main_id, data_key

Table data: main_id,data_content

where in both tables key and data main_id reffer to the id in the table main, which is the way to join them

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