Database design for internal Messaging (like facebook message) - sql-server

In the following database design, how do I mark an unread message, so we know who read the message either the sender and recipient?
(ie When person X sends a message to person Y, the message will be marked as unread for person Y until person Y reads the message, but its marked as read for person X because he sent the message).
MESSAGE
Id (PK)
Subject
Content
MessageTypeId (FK) - Message, Update (global board-cast), notifications etc
UserId (FK) - creator
CreateDate
ReadDate
MESSAGE_COMMENT
Id (PK)
MessageId (FK)
Content
UserId (FK) - creator
CreatedDate
USER
FirstName
LastName
Username
Password
IsActive
etc...etc...
MESSAGE_TYPE
Id
Code
Name
EDIT: it seems like the design is incomplete.

You could add a Read table which links user ID and message ID. When a message is created the application automatically puts in the UserID of the creator and the MessageID. Then when the receiver reads the message it records the ReceiverID and the MessageId.
So while there is no record for ReceiverID and MessageID, the message will appear unread for the receiver.

Related

how to set up database

I’m creating an app that tracks attendance of players in amateur football teams. And I’m just not sure how to set up the database. A bit of info: every user has a team and should only see the events within his team per season.
These are the main things:
Users
Events
User stats (for later)
Events
Training / match / social
Date
Time
Subject / opponent
Attendance
Main questions:
Do I make an event table per team per season? Or do I put all events in one big table for every user?
What is the best way to handle the attendance? They should be able to choose from
Present
absent
Fan
Thanks in advance!
I would do it this way:
User
userid PK
Name
Team
teamid PK
TeamName
UserInTeam
userid FK
teamid FK
Event
eventid PK
home_teamid FK
visiting_teamid FK
eventtypeid FK
Date
Time
Subject / opponent
Attendance
Event type
eventtypeid PK
EventType
Attendance Type
attendanceid PK
AttendanceType
UserAtEvent
userid FK
eventid FK
attendancetypeid FK
Content of Event Type
1 Training
2 Match
3 Social
Content of Attendance Type
1 Present
2 Absent
3 Fan
For Event, home_teamid and visiting_teamid can be NULL if the event is not a match
The application will show the events based on the team the user is a member of.

Best way to design Users and Contacts

I have two tables (e.g.):
Users (ID, firstName,middleName, lastName)
Contacts (ID, userID, serialNo, phoneNumber, eMail).
I shall be communicating (sending messages) to Users via phoneNumber or eMail or both and save it in Database (e.g.).
Log (ID, userID, contactID, message, onPhoneOrEmail) where, say, last field stores, say, 'p','e' or 'b', for phoneNumber, eMail, or both.
So, when I check logs, I can get to know that which message was sent to which email/phonenumber.
Problem:
What to do when Users change their contact details?
If I update the Contacts table, I lose Log, because the messages were not sent to the new number.
If I store the number or email in Logs, it would be to much of data to store (on large scale as compared to just one character).
Last: If I add new Contact with +1 serial number (serialNo - field), will it be feasible ? What about performance issues ? (uniqueness is not required, Users can changes number or email as many as times as they want - these are just for communcation).
I read this and this, but could not get an approriate answer regarding performance/methodological issues.
Please guide.
SAMPLE DATA:
USERS
| 1 | John | null | Cena |
CONTACTS
| 1 | 1 | 1 | 123456 | abc#xyz.com
| 2 | 1 | 2 | null | xyz#mnp.com
| 3 | 1 | 3 | 987654 | null
If you say that a User can change his contact detail this means that you inverted the dependency. The User has the Contact, so it is reasonable to associate to a user a contactID and not the opposite. Now, a User can change e.g. phone whenever he wants, and at the same time it make no sense for the same phone number to change its user at some point.
So it would be turned like this:
User(ID, firstName,middleName, lastName, contactID)
Contact(ID,serialNo, phoneNumber, eMail)
Log (ID, userID, message, onPhoneOrEmail).
You don't need both userID and contactID on Log. Remember that one is foreign key for the other (transitive dependency).
EDIT
If you need to store multiple contacts per User, keep your schema but change the Log in
Log (ID, contactID, message, onPhoneOrEmail)
From my point of view, when you need to change contact of a user it means that you will remove one and add another. If you have never sent any message to that contact you are removing, you have no reason to keep it in memory, otherwise, if you need a record you have to maintain the contact information in memory even after you have replaced it (maybe a column saying it is invalid is preferable). This is already the default behavior in mySQL (ON DELETE RESTRICT).
Get rid of your Contact table.
Create a new UserPhone table (PK - ID, FK - User.Id, Phone#, ActiveDate)
Create a new UserEmail table (PK - ID, FK - User.Id, Email,
ActiveDate).
It looks like SerialNumber is just an incrementer for
one User's Contact data. If it is just an incrementer, ActiveDate
should suffice as a replacement.
When phone, email information changes do not update existing record, add new record with today's date instead.
Your Log table will look like (PK - LogID, FK - UserEmail.ID, FK - UserPhone.ID).
No need for the PhoneOrEmail field. That information can be determined by presence of the FKs.
You might have some other design issues but this answer should get you on the right track.

SOQL Salesforce Join and Filter for 3 Different Objects

I would appreciate if someone can help me with the following requirement for building a SOQL.
Object1: Event :
Fields to get: Id, Subject, OwnerId
Object2: EventAttendee :
Fields to get: EventId, AttendeeId
Object3: User :
Fields to get: Id, Email
Note:
Id of Event = EventId of EventAttendee &
AttendeeId of EventAttendee = Id of user
So the requirement is to lookup Id from Event into EventId of EventAttendee and get the respective AttendeeID, then for the same AttendeeId lookup into user to get the Email address.
Thanks for your help.
One way would be to start from Event, go down to Attendees, then up to Users. The table that holds attendees is actually called EventRelation. Something like that should get you started:
SELECT Id, Subject, OwnerId,
(SELECT EventId, RelationId, Relation.Email FROM EventRelations)
FROM Event

Reading multiple records related to every record in the result set at a time

I have 3 tables:
Message table having MsgID, MsgText, MsgDate columns. MsgId is PK.
MessageSender table having MsgId, SenderId, SenderName. MsgId is FK.
MessageTo table having MsgId, ToId, ToName. MsgId is FK.
Every Message will have one Sender. But it can have many Recipients. i.e. For every record in the Message table, there will be one record in MessageSender table and more than one records in the MessageTo table.
I want to get ALL details of the all Messages in one query or at a time. e.g. for a specific Message, who is the Sender and who ALL are recipients.
How can I do this?
I am using MSSQL Server 2005 and Sybase 15.
Note: I have given only relevant details of the tables here. And I cannot change table schema since it's been there in production for a long time.
select distinct M.MsgID, M.MsgText, MS.SenderID, MS.SenderName,
MT.ToName, MT.ToId
from Message M, MessageSender MS, MessageTo MT
where M.MessageID = MS.MessageID and
M.MessageID = MT.MessageID
and MsgDate like '%20130307%' -- To get the messages sent on 07-Mar-2013

Database Design for Simple Private Messaging

The concept is simple:
User click "Send PM" button on one of ads.
Window pop up with "Title" box, "Message" box and send button.
Recipient sees a message with following info: "Sender Name", "Date Received", "Title" and "Message".
Recipient replies by filling: "Title" and "Message" and press reply.
Repeat step 3.
No IP will be stored.
Can you please give me an idea how to make a concise/efficient relational design?
Message table formate
Message
Id
UserID
Message
Send_DateTime
Title
Message_Id ( FK )- self referance
self referance is to keep the track of the whole chat.
for the first message its NULL and than onwards id of relative meesage
Id UserID Message Send_DateTime Title Message_Id ( FK )- self referance
1 1 test datetime tt Null
2 2 test1 datetime tt 1
3 1 test2 datetime tt 2
...
go on

Resources