Database design for email messaging system

后端 未结 9 881
情书的邮戳
情书的邮戳 2021-01-29 22:58

I want to make an email messaging system like gmail have. I would like to have following option: Starred, Trash, Spam, Draft, Read, Unread. Right now I have the below following

9条回答
  •  [愿得一人]
    2021-01-29 23:41

    You need to split your table for it. You could have following schema and structure

    CREATE TABLE [Users]
        (
          [UserID] INT ,
          [UserName] NVARCHAR(50) ,
          [FirstName] NVARCHAR(50) ,
          [LastName] NVARCHAR(50)
        )
    
    CREATE TABLE [Messages]
        (
          [MessageID] INT ,
          [Subject] NVARCHAR(MAX) ,
          [Body] NVARCHAR(MAX) ,
          [Date] DATETIME,
          [AuthorID] INT,
        )
    
    CREATE TABLE [MessagePlaceHolders]
        (
          [PlaceHolderID] INT ,
          [PlaceHolder] NVARCHAR(255)--For example: InBox, SentItems, Draft, Trash, Spam 
        )
    
    CREATE TABLE [Users_Messages_Mapped]
        (
          [MessageID] INT ,
          [UserID] INT ,
          [PlaceHolderID] INT,
          [IsRead] BIT ,
          [IsStarred] BIT 
    
        )
    

    In users table you can have users."Messages" denotes the table for messages. "MessagePlaceHolders" denotes the table for placeholders for messages. Placeholders can be inbox, sent item, draft, spam or trash. "Users_Messages_Mapped" denotes the mapping table for users and messages. The "UserID" and "PlaceHolderID" are the foreign keys."IsRead" and "IsStarred" signifies what their name stands for. If there is no record found for a particular messageid in "Users_Messages_Mapped" table that record will be deleted from Messages table since we no longer need it.

提交回复
热议问题