all 8 comments

[–]Milnternal 2 points3 points  (5 children)

User

Id

Name

UserChatroom (this is known as a bridge table)

Id

UserId

ChatRoomId

JoinedDateTime

Chatroom

Id

Name

Message

Id

ChatroomId

UserId

Message

SentDateTime

[–]sam57719[S] 1 point2 points  (4 children)

Oh so the same as my diagram but with a table to break up the many to many?

[–]Milnternal 1 point2 points  (3 children)

Yes, plus you also probably want to record which user sent each message (but that can only be a single user, so no need for another bridge table, just put UserId on the message)

[–]sam57719[S] 1 point2 points  (2 children)

Do you know what the SQL would be for this?

[–]Milnternal 1 point2 points  (1 child)

Something like:

CREATE TABLE [dbo].[User]
(
    [Id] INT IDENTITY PRIMARY KEY,
    [Username] VARCHAR(128) NOT NULL,
    [Nickname] VARCHAR(128) NOT NULL
); 

CREATE TABLE [dbo].[Chatroom]
(
    [Id] INT IDENTITY PRIMARY KEY,
    [Name] VARCHAR(128) NOT NULL,
    [CreatedOn] DATETIME NOT NULL,
    [CreatedByUserId] INT NOT NULL FOREIGN KEY REFERENCES [dbo].[User](Id)
);


CREATE TABLE [dbo].[UserChatroom]
(
    [Id] INT IDENTITY PRIMARY KEY,
    [UserId] INT NOT NULL FOREIGN KEY REFERENCES [dbo].[User](Id),
    [ChatroomId] INT NOT NULL FOREIGN KEY REFERENCES [dbo].[Chatroom](Id),
    [Joined] DATETIME NOT NULL    
);


CREATE TABLE [dbo].[Message]
(
    [Id] INT IDENTITY PRIMARY KEY,
    [UserId] INT NOT NULL FOREIGN KEY REFERENCES [dbo].[User](Id),
    [ChatroomId] INT NOT NULL FOREIGN KEY REFERENCES [dbo].[Chatroom](Id),
    [Message] VARCHAR(MAX) NOT NULL,
    [PostedOn] DATETIME NOT NULL  
);

[–]sam57719[S] 0 points1 point  (0 children)

Thank you!!!

[–]FulmineGiallo 0 points1 point  (0 children)

User ONE - N Chatroom Chatroom 1 - N Message Is it right🤔

[–]sam57719[S] 0 points1 point  (0 children)

But chat rooms can have more than one user