Question

My SQL Server database has the initial table named Lead. This table has all kinds of info about a lead person. It has 3 foreign key columns: IndustryId, BusinessTypeId and ReferenceId. I have a problem in the architecture concerning the tables Lead and Reference.

This is the script of the table 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

And this is the script for the table in question Reference:

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

The problem is that each Lead has a Reference, but the Reference may be a Lead. In the UI while adding a new Lead, user is asked to set its Reference, but he may choose an existing Lead to be the one who referenced the new Lead, or he may choose a normal Reference.

So now I'm not sure, what is the best way to set those tables? Should I merge them in a single table Lead and when it's a simple Reference I keep the unneeded columns as Null, or keep both tables and add a new column to Reference table "LeadId" without making it a foreign key? Or maybe another method. Please advise.

Était-ce utile?

La solution

Two ways to do it:

  • Option 1, as you suggest, is to make a new LeadId FK column in the [Reference] table, and make it nullable. This will work, it'll be easy enough to query, but you'll have a column with lots of nulls.

  • Option 2 is the 3NF (third normal form) way, with a third table that links [Reference] to [Lead]. That would contain a ReferenceId FK and a LeadId FK. You would need to join to this table in queries, but you wouldn't be stuck with a lot of nulls.

Licencié sous: CC-BY-SA avec attribution
Non affilié à dba.stackexchange
scroll top