Hi,
This is a typical in office debate I would love some feedback on.
What are the pros and cons of these 2 solutions for records that may be arbitrarily grouped by a user into zero or one group.
Solution 1.
Base table with a nullable foreign key to the group table:
CREATE TABLE [dbo].[Foo](
[FooId] [int] NOT NULL,
[Name] [varchar](50) NOT NULL,
[FooGroupId] [int] NULL,
CONSTRAINT [PK_Foo] PRIMARY KEY CLUSTERED
(
[FooId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[FooGroup](
[FooGroupId] [int] NOT NULL,
[Name] [varchar](50) NOT NULL,
CONSTRAINT [PK_FooGroup] PRIMARY KEY CLUSTERED
(
[FooGroupId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[Foo] WITH CHECK ADD CONSTRAINT [FK_Foo_FooGroup] FOREIGN KEY([FooGroupId])
REFERENCES [dbo].[FooGroup] ([FooGroupId])
Solution 2.
Base Table, Extension of Base table with foreign Key to Group Table:
CREATE TABLE [dbo].[Bar](
[BarId] [int] NOT NULL,
[Name] [varchar](50) NULL,
CONSTRAINT [PK_Bar] PRIMARY KEY CLUSTERED
(
[BarId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[BarGroup](
[BarGroupId] [int] NOT NULL,
[Name] [varchar](50) NULL,
CONSTRAINT [PK_BarGroup] PRIMARY KEY CLUSTERED
(
[BarGroupId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[BarGroupBarExtension](
[BarId] [int] NOT NULL,
[BarGroupId] [int] NOT NULL,
CONSTRAINT [PK_BarGroupBarExtension] PRIMARY KEY CLUSTERED
(
[BarId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[BarGroupBarExtension] WITH CHECK ADD CONSTRAINT [FK_BarGroupBarExtension_Bar] FOREIGN KEY([BarId])
REFERENCES [dbo].[Bar] ([BarId])
GO
ALTER TABLE [dbo].[BarGroupBarExtension] WITH CHECK ADD CONSTRAINT [FK_BarGroupBarExtension_BarGroup] FOREIGN KEY([BarGroupId])
REFERENCES [dbo].[BarGroup] ([BarGroupId])
GO
The 2nd way seems the most "correct" from a Database point of view, but the first way makes it much easier to code against. Like I said we are looking for outside opinions and discussion.
Thanks