如何在[mail_Receivers]和[mail_Users_Messages_Mapped]表之间建立关系?

时间:2014-11-10 07:16:23

标签: sql-server-2008

我正在为电子邮件系统创建数据库设计,例如gmail,hotmail,yahoo,但我遇到了一些关于 [mail_Receivers] 表的问题。我必须在 [mail_Users_Messages_Mapped] 表中定义此表的Id,但问题是当接收者id多于一个示例时:5因此数据库中将影响5行,它的平均5人已收到此电子邮件,那么如何在 [mail_Users_Messages_Mapped] 表中定义 [mail_Receivers] 表格ID?

请帮助我,

先谢谢。

我的代码:

--1) 
CREATE TABLE [mail_Users]
    (
      [UserID] INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
      [UserEmail] NVARCHAR(50) NOT NULL,
      [PASSWORD] NVARCHAR(50) NOT NULL,
      [FirstName] NVARCHAR(50),
      [LastName] NVARCHAR(50)
    )
--2)
CREATE TABLE [mail_Messages]
    (
      [MessageID] INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
      [Subject] NVARCHAR(MAX),
      [Body] NVARCHAR(MAX),
      [Date] DATETIME,
    )
--3)
CREATE TABLE [mail_Receivers]
(
    [ReceiverId] INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
    [ReceiverEmail] NVARCHAR(MAX) NOT NULL,
)
--4)
CREATE TABLE [mail_MessagePlaceHolders]
    (
      [PlaceHolderID] INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
      [PlaceHolder] NVARCHAR(255)--For example: InBox, SentItems, Draft, Trash, Spam 
    )

    /*
     *  Custom Implementation   
     */
--5)
CREATE TABLE [mail_Users_Attachments]
    (
        [AttachmentId] INT PRIMARY KEY IDENTITY(1,1),
        [AttachmentName] NVARCHAR(255),
        [AttachmentUri] NVARCHAR(255),
        [MessageId] INT FOREIGN KEY REFERENCES [mail_Messages]([MessageID]) NOT NULL
    )

    /*
     * End
     */
--6)
CREATE TABLE [mail_Users_Messages_Mapped]
    (
        [UserID] INT FOREIGN KEY REFERENCES [mail_Users]([UserID]) NOT NULL,
        [MessageID] INT FOREIGN KEY REFERENCES [mail_Messages]([MessageID]) NOT NULL,
        [ReceiverId] INT FOREIGN KEY REFERENCES [mail_ReceiverId]([ReceiverId]) NOT NULL,
        [PlaceHolderID] INT FOREIGN KEY REFERENCES [mail_MessagePlaceHolders]([PlaceHolderID]) NOT NULL,
        [AttachmentId] INT FOREIGN KEY REFERENCES [mail_Users_Attachments]([AttachmentId]),
        [IsRead] BIT,
    )

1 个答案:

答案 0 :(得分:0)

您将使用多对多映射表。这个表包含mesaage_id和receiver_id,如下所示:

message_id receiver_id
1          1
1          2
1          3
2          1
3          2