I'm working on creating a messaging system as a pet project, that will include the ability to have file attachments. This will be used on a website of mine for the internal messaging system.
One of the features of this system is I want to maintain the MD5 checksum of every file that is uploaded so if duplicate files are uploaded, the two links will reference the same file.
I've come up with the following so far:
Message
----------
MessageID (PK)
SenderID (FK)
RecipientsID (FK)
AttachmentsID (FK)
Subject
MessageText
DateSent
Recipient
----------
UserID (FK)
MessageID (FK)
Attachment
----------
ID
Name
MessageID (FK)
FileID (FK)
File
----------
ID
Checksum
LastAccessDate
AccessCount
So, you will be able to have several messages, each of which can have multiple attachments. But also, to save space on our sever since my use case will have users uploading many of the same file, different attachments can reference the same file.
My question is, should the Message table contain some kind of RecipientsID? Or is it enough to have my Recipient table reference MessageID?
The same question for AttachmentsID on the Message table. Should I have an some sort of AttachmentsID? Or is it enough that the Attachment table references the MessageID.
Is it ok for Message to be not have any reference to its Attachments or Recipients, if both Attachments and Recipients know which Message they belong to? Or should I be doing it another way?
I'm curious to hear how some veteran SQL guys would lay this schema out.
Edit: I'm looking to have multiple recipients and multiple attachments, per message. I'm sorry if that wasn't clear.
It is in these one-to-many relationships that I'm struggling with understanding if I'm doing it the best way.