Marc Rasmussen
Marc Rasmussen

Reputation: 20565

Database tables with unknown amount of columns

This is something I've been wondering about for quite a while. And since I am creating a program where I need to do it i want to ask you guys what you do when you "hit" the following case:

Lets say for instance that you have a chat program. In this program people can send messages back and forward.

Now you want to store a conversations between an unknown amount of people.

Now what you could do is have a message table and then for each of the users who are involved in the conversation you add a row.

Now if you have anything other than a small size program this message table would quickly expand to a very Large size

So I guess my question is what do you do in this case?

Upvotes: 0

Views: 127

Answers (1)

Vatev
Vatev

Reputation: 7590

To create multi recipient messages you can create a messages table which has:

  • id
  • sender -> users.id
  • text
  • time

and a message_recipients table which has:

  • message -> messages.id
  • recipient -> users.id
  • is_read

This will still have the same (large) amount of rows, but those will be in the message_recipients table which is only a few bytes per row.

Upvotes: 3

Related Questions