我的 SQL Server 数据库有一个名为 Lead 的初始表。该表包含有关领导者的各种信息。它有 3 个外键列:IndustryId、BusinessTypeId 和 ReferenceId。我在有关表 Lead 和 Reference 的体系结构中遇到问题。
这是表 Lead 的脚本:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Lead](
[LeadId] [int] IDENTITY(1,1) NOT NULL,
[LeadName] [nvarchar](100) NOT NULL,
[CompanyName] [nvarchar](100) NOT NULL,
[Phone] [nvarchar](50) NOT NULL,
[Address] [nvarchar](max) NOT NULL,
[DOB] [date] NULL,
[IndustryID] [int] NOT NULL,
[BusinessTypeID] [int] NOT NULL,
[IsDeleted] [bit] NOT NULL CONSTRAINT [DF_Lead_IsDeleted] DEFAULT ((0)),
[ReferenceId] [int] NOT NULL,
[IsLead] [bit] NOT NULL,
[Email] [nvarchar](100) NOT NULL,
[Website] [nvarchar](100) NOT NULL,
[RepresentativeName] [nvarchar](100) NOT NULL CONSTRAINT [DF_Lead_RepresentativeName] DEFAULT (''),
[IsClosed] [bit] NOT NULL CONSTRAINT [DF_Lead_IsClosed] DEFAULT ((0)),
[feedback] [bit] NOT NULL CONSTRAINT [DF_Lead_feedback] DEFAULT ((0)),
[feedbackMemo] [nvarchar](100) NULL,
CONSTRAINT [PK_Lead] PRIMARY KEY CLUSTERED
(
[LeadId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[Lead] WITH CHECK ADD CONSTRAINT [FK_Lead_LeadIndustry] FOREIGN KEY([IndustryID])
REFERENCES [dbo].[Industry] ([IndustryId])
GO
ALTER TABLE [dbo].[Lead] CHECK CONSTRAINT [FK_Lead_LeadIndustry]
GO
ALTER TABLE [dbo].[Lead] WITH CHECK ADD CONSTRAINT [FK_Lead_LeadReference] FOREIGN KEY([ReferenceId])
REFERENCES [dbo].[Reference] ([ReferenceId])
GO
ALTER TABLE [dbo].[Lead] CHECK CONSTRAINT [FK_Lead_LeadReference]
GO
ALTER TABLE [dbo].[Lead] WITH CHECK ADD CONSTRAINT [FK_Lead_LeadType] FOREIGN KEY([BusinessTypeID])
REFERENCES [dbo].[BusinessType] ([BusinessTypeId])
GO
ALTER TABLE [dbo].[Lead] CHECK CONSTRAINT [FK_Lead_LeadType]
GO
这是有问题的表的脚本参考:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Reference](
[ReferenceId] [int] IDENTITY(1,1) NOT NULL,
[ReferenceName] [nvarchar](100) NOT NULL,
[Address] [nvarchar](max) NOT NULL,
[DOB] [date] NULL,
[Memo] [nvarchar](max) NOT NULL,
[IsDeleted] [bit] NOT NULL,
[Phone] [nvarchar](100) NOT NULL,
[DateTimeAdded] [datetime] NOT NULL,
[AddedByUserId] [int] NOT NULL,
[DateTimeUpdated] [datetime] NULL,
[UpdatedByUserId] [int] NOT NULL,
CONSTRAINT [PK_Reference] PRIMARY KEY CLUSTERED
(
[ReferenceId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
问题是每个 Lead 都有一个 Reference,但 Reference 可能是一个 Lead。在添加新线索时的 UI 中,要求用户设置其引用,但他可以选择现有线索作为引用新线索的线索,也可以选择普通引用。
所以现在我不确定,设置这些表的最佳方法是什么?我应该将它们合并到一个表 Lead 中,当它是一个简单的 Reference 时,我将不需要的列保留为 Null,还是保留两个表并向 Reference 表“LeadId”添加一个新列而不使其成为外键?或者也许是另一种方法。请指教。
两种方法:
正如您所建议的,选项 1 是
LeadId
在表中创建一个新的 FK 列[Reference]
,并使其可为空。这会起作用,查询起来很容易,但是您会有一个包含很多空值的列。选项 2 是 3NF(第三范式)方式,第三个表链接
[Reference]
到[Lead]
. 那将包含一个ReferenceId
FK 和一个LeadId
FK。您需要在查询中加入该表,但不会遇到大量空值。