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!
You should never see “dynamically created tables” from a relational-databases point of view.
In general your problem is solved as follows:
userstable with (user_id, name, surname …)messagestable with (message_id, time_stamp, subject, body, sent_by …)messages_recipientstable (message_id, recipient_id)Both the
sent_byfield in themessagestable and therecipient_idfield in themessages_recipientstables should be foreign keys to theuser_idfield in theuserstable.