简体   繁体   English

存储数据时mysql中表结构的首选方法

[英]Which is preferred method of table structure in mysql when storing data

Hey im building app which takes use of mysql. 嗨,即时通讯建立使用mysql的应用程序。 I am storing user data in users table and groups data in groups table. 我将用户数据存储在用户表中,并将数据存储在组表中。 Predicted user's in total is little over 100 预计的用户总数略高于100

I was wondering is there any downsides or which is preferred method of constructing such structure like described below. 我想知道是否存在任何弊端,或者是构造此类结构的首选方法,如下所述。 I am using php to manage mysql data. 我正在使用php管理mysql数据。

Now I have two tables: 现在我有两个表:

Users
-----

id, firstname, lastname, in_groups
----------------------------------

1, Jake, New house, [1]

35, Gavin, McInnes, [1, 32, 41]  

Groups
------

id, group_name, group_password, total
--------------------------------------------

1, Destroyers, password, 1

32, Gamers, password, 41

41, Media, password, 22

Is this good method or should I divide this into more tables like 这是个好方法还是应该将其划分为更多表格?

in_group
--------

id, user_id, group_id
---------------------
1, 1, 1

2, 35, 1

3, 35, 32

4, 35, 41

Definitely better to use the separate in_group table. 使用单独的in_group表绝对好。 That will make it equally easy to select all the "users in a group" (currently hard) as "groups for a user" (currently easy), plus even for the easy "groups for a user" query, right now you need to parse that out separately and by using a separate table you just read the rows of the results. 这样一来,选择“组中的所有用户”(当前很难)作为“用户组”(当前很容易),甚至简单的“用户组”查询都同样容易,现在您需要通过使用单独的表分别进行解析,您只需读取结果行。

There is another advantage if you have multiple updates going on at the same time. 如果您同时进行多个更新,则还有另一个优势。 Right now any update would replace the entire set of groups for a user. 现在,任何更新都将替换用户的整个组。 By separating the tables, you could have the ability to add or delete a group from a user without having any effect on the other groups for the user. 通过分离表,您可以从用户添加或删除组,而不会对该用户的其他组产生任何影响。 There are other ways around that potential problem, but a separate table makes it very simple to handle. 还有其他方法可以解决该潜在问题,但是单独的表使其非常易于处理。

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

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