如何存储在数据库中的数组?数组、数据库中

由网友(不如离去)分享简介:我有到现在使用基于文件的存储简单的邮件系统。因为该系统的用户的数量逐渐增加,我想切换到基于数据库存储。在$ C C消息服务器$维护用户的列表(其全权证书)和信息的列表。 Message对象作为其主要领域发件人(User类型),收件人和内容(类型为字符串)(类型用户的[])。通常情况下,用户将请求消息给他的从服务器并...

我有到现在使用基于文件的存储简单的邮件系统。因为该系统的用户的数量逐渐增加,我想切换到基于数据库存储。

在$ C C消息服务器$维护用户的列表(其全权证书)和信息的列表。 Message对象作为其主要领域发件人(User类型),收件人和内容(类型为字符串)(类型用户的[])。 通常情况下,用户将请求消息给他的从服务器并接收其收件人字段包含自己的用户名的所有消息。

因此,对于数据库我设想如下表: -A用户表 -A信息表 ( - 为每个用户指定的邮件给他的邮件ID表)?

我的问题是如何存储在这样一种方式,一个数据库查询可以搜索其用于请求接收消息给他的用户的接收者字段(其中包含的阵列)。我看不出有什么更好的解决办法,而不是动态创建一个表中为每个新用户保存提及,他被列为收件人的邮件系统上的注册。其他可能的办法?

非常感谢!

解决方案   

我可以看到比没有更好的解决办法   动态地为每个表   新的用户...

您永远不应该看到动态创建表从关系,数据库视图点。

在一般的问题解决如下:

A 用户表(user_ID的,名字,姓氏......) A 的消息表(与Message_ID,TIME_STAMP,主题,正文,sent_by ...) A messages_recipients 表(与Message_ID,recipient_id) 保存数组类型数据 JAVA基础数组

的消息无论是 sent_by 字段表和 recipient_id messages_recipients领域表应外键,以在用户 USER_ID 字段表。

I have a simple messaging system that up till now uses file based storage. Because the number of users of the system is increasing gradually I would like to switch over to database based storage.

In code the message server maintains a list of users (with their credentials) and a list of messages. A Message object has as its main fields a Sender (of type User), Recipients (of type User[]) and a Content(of type string). Typically, a user will request the messages addressed to him from the server and receive all messages in which the Recipients field contains his own username.

So, for the database I envision the following tables: -A Users table -A Messages table (- a table for each user that specifies the messages addressed to him by MessageID)?

The problem I have is how to store the Recipients field (which contains an array) in such a way that a database query can search it for the user that requests to receive the messages addressed to him. I can see no better solution than to dynamically create a table for each new user that registers on the system that holds references to the messages in which he is listed as a recipient. Are other approaches possible?

Thanks a lot!

解决方案

I can see no better solution than to dynamically create a table for each new user...

You should never see "dynamically created tables" from a relational-databases point of view.

In general your problem is solved as follows:

A users table with (user_id, name, surname ...) A messages table with (message_id, time_stamp, subject, body, sent_by ...) A messages_recipients table (message_id, recipient_id)

Both the sent_by field in the messages table and the recipient_id field in the messages_recipients tables should be foreign keys to the user_id field in the users table.

阅读全文

相关推荐

最新文章