简体   繁体   English

如何规范化或设计我的MySQL数据库

[英]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? 我想知道哪种结构同时可以将大约50个数据存储到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. user_id将拥有5000多名用户。

Definitely the first one! 绝对是第一个! What if the need arises to have more than 50 different "data keys". 如果需要具有50个以上的不同“数据密钥”怎么办。 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. 如果它们遵循“ first_name,last_name,hair_color,eye_color,birthday ...”的路线,则第二个模型将是可行的,因为您经常需要同时为用户检索所有这些信息。

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. 如果我没猜错,不是所有的tue用户都为2,但是您有不同的数字,则应该对该表进行规范化,将其划分为多个。

One that has: 一个具有:

Table main: id , data_id, user_id 表主体:id,data_id,user_id

Table key main_id, data_key 表键main_id,data_key

Table data: main_id,data_content 表数据: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 在表和表中,key和data main_id都对应于表main中的id,这是联接它们的方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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