简体   繁体   English

在Mysql数据库的单个字段中存储数组的替代方法

[英]Alternative to storing array in a single field of Mysql database

How to avoid storing array of users in one field? 如何避免将用户数组存储在一个字段中? I have a session table that have a field with users ids that joins the session(joinusers_id). 我有一个会话表,该表具有包含加入会话的用户ID的字段(joinusers_id)。 How I should approach on inserting them? 我应该如何插入它们? The easy way is to put them separated with comma, but I guess it isn't the most efficient way. 最简单的方法是用逗号将它们分开,但是我想这不是最有效的方法。 Attached is a screenshot of my current implementation of session Table: 附件是我当前执行的会话表的屏幕截图: 表格中目前有什么

Ok, we are talking about databases and normalization. 好的,我们正在谈论数据库和规范化。

http://en.wikipedia.org/wiki/Database_normalization http://en.wikipedia.org/wiki/Database_normalization

No, please do not use non-atomic fields, it is bad and you will get in to trouble, if you want to join such non-atomic fields. 不,请不要使用非原子场,这很不好,如果您想加入这样的非原子场,将会遇到麻烦。

I would suggest you are doing this: 我建议您这样做:

You have users, so please create a user table containing all the things belonging to a user. 您有用户,因此请创建一个用户表,其中包含属于该用户的所有事物。 The user table needs a primary key: user_mailaddress 用户表需要一个主键: user_mailaddress

You have sessions, create a session table ... primary key: session_id 您有会话,创建会话表...主键: session_id

Now create a table user_session , this table has two attributes (columns) user_mailaddress and session_id using this table you can connect the two tables together. 现在创建一个表user_session ,该表具有两个属性(列) user_mailaddresssession_id,使用此表可以将两个表连接在一起。

SELECT *
FROM user
NATURAL_JOIN user_session
NATURAL_JOIN session

The natural join will join the tables, where the attributes and the contents of the attributes are comparable. 自然联接将联接表,其中属性和属性的内容是可比较的。

NOTE: The tables user and session are called entities. 注意:表用户会话称为实体。 The table user_session is called the relation. user_session称为关系。 So this is called the entity-relationship model. 因此,这称为实体关系模型。 => http://en.wikipedia.org/wiki/Entity%E2%80%93relationship_model => http://en.wikipedia.org/wiki/Entity%E2%80%93relationship_model

To read it in more depth: https://www.stat.auckland.ac.nz/~paul/ItDT/HTML/node42.html 要更深入地阅读它: https : //www.stat.auckland.ac.nz/~paul/ItDT/HTML/node42.html

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

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