好的,为了满足您的要求,您的设计将起作用。我看到另外两个可以满足给定规范的易于实现的选项。
1.) 7个兴趣表外键为兴趣表的学生表;使用一组独特的约束来确保没有学生有两个相同的兴趣。使用可为空的外键并允许在项目 4-7 上使用空值。
2.) 打破利益表。该规则的工作原理 99% 相同,尽管强制性兴趣只会在选择兴趣后触发约束(因此学生可以有 0 或 3-8 个兴趣)。这是一种稍微合理的方法,因此您可以将学生表与兴趣分开填充。这使事情具有可扩展性。
这两种方法都属于第三范式,适用于符合 ANSI-92 的数据库。SQL server 中#2 的示例实现如下。
USE [DB]
GO
/****** Object: Table [dbo].[Interests] Script Date: 11/9/2012 8:32:39 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Interests](
[interestID] [int] IDENTITY(1,1) NOT NULL,
[interestText] [varchar](255) NOT NULL,
[interestDescription] [varchar](max) NULL,
CONSTRAINT [PK_Interests] PRIMARY KEY CLUSTERED
(
[interestID] 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
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[StudentInterests] Script Date: 11/9/2012 8:32:39 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[StudentInterests](
[studentInterestID] [int] IDENTITY(1,1) NOT NULL,
[studentID] [int] NOT NULL,
[interestID_1] [int] NOT NULL,
[interestID_2] [int] NOT NULL,
[interestID_3] [int] NOT NULL,
[interestID_4] [int] NULL,
[interestID_5] [int] NULL,
[interestID_6] [int] NULL,
[interestID_7] [int] NULL,
[interestID_8] [int] NULL,
CONSTRAINT [PK_StudentInterests] PRIMARY KEY CLUSTERED
(
[studentInterestID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [IX_StudentInterests] UNIQUE NONCLUSTERED
(
[studentInterestID] ASC,
[interestID_1] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [IX_StudentInterests_1] UNIQUE NONCLUSTERED
(
[studentInterestID] ASC,
[interestID_2] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [IX_StudentInterests_2] UNIQUE NONCLUSTERED
(
[studentInterestID] ASC,
[interestID_3] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [IX_StudentInterests_3] UNIQUE NONCLUSTERED
(
[studentInterestID] ASC,
[interestID_4] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [IX_StudentInterests_4] UNIQUE NONCLUSTERED
(
[studentInterestID] ASC,
[interestID_4] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [IX_StudentInterests_5] UNIQUE NONCLUSTERED
(
[studentInterestID] ASC,
[interestID_5] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [IX_StudentInterests_6] UNIQUE NONCLUSTERED
(
[studentInterestID] ASC,
[interestID_6] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [IX_StudentInterests_7] UNIQUE NONCLUSTERED
(
[studentInterestID] ASC,
[interestID_7] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [IX_StudentInterests_8] UNIQUE NONCLUSTERED
(
[studentInterestID] ASC,
[interestID_8] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [IX_StudentInterests_9] UNIQUE NONCLUSTERED
(
[studentID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Students] Script Date: 11/9/2012 8:32:39 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Students](
[studentID] [int] IDENTITY(1,1) NOT NULL,
[firstName] [varchar](255) NOT NULL,
[lastName] [varchar](255) NOT NULL,
CONSTRAINT [PK_Students] PRIMARY KEY CLUSTERED
(
[studentID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[StudentInterests] WITH CHECK ADD CONSTRAINT [FK_StudentInterests_Interests] FOREIGN KEY([interestID_1])
REFERENCES [dbo].[Interests] ([interestID])
GO
ALTER TABLE [dbo].[StudentInterests] CHECK CONSTRAINT [FK_StudentInterests_Interests]
GO
ALTER TABLE [dbo].[StudentInterests] WITH CHECK ADD CONSTRAINT [FK_StudentInterests_Interests1] FOREIGN KEY([interestID_2])
REFERENCES [dbo].[Interests] ([interestID])
GO
ALTER TABLE [dbo].[StudentInterests] CHECK CONSTRAINT [FK_StudentInterests_Interests1]
GO
ALTER TABLE [dbo].[StudentInterests] WITH CHECK ADD CONSTRAINT [FK_StudentInterests_Interests2] FOREIGN KEY([interestID_3])
REFERENCES [dbo].[Interests] ([interestID])
GO
ALTER TABLE [dbo].[StudentInterests] CHECK CONSTRAINT [FK_StudentInterests_Interests2]
GO
ALTER TABLE [dbo].[StudentInterests] WITH CHECK ADD CONSTRAINT [FK_StudentInterests_Interests3] FOREIGN KEY([interestID_4])
REFERENCES [dbo].[Interests] ([interestID])
GO
ALTER TABLE [dbo].[StudentInterests] CHECK CONSTRAINT [FK_StudentInterests_Interests3]
GO
ALTER TABLE [dbo].[StudentInterests] WITH CHECK ADD CONSTRAINT [FK_StudentInterests_Interests4] FOREIGN KEY([interestID_5])
REFERENCES [dbo].[Interests] ([interestID])
GO
ALTER TABLE [dbo].[StudentInterests] CHECK CONSTRAINT [FK_StudentInterests_Interests4]
GO
ALTER TABLE [dbo].[StudentInterests] WITH CHECK ADD CONSTRAINT [FK_StudentInterests_Interests5] FOREIGN KEY([interestID_6])
REFERENCES [dbo].[Interests] ([interestID])
GO
ALTER TABLE [dbo].[StudentInterests] CHECK CONSTRAINT [FK_StudentInterests_Interests5]
GO
ALTER TABLE [dbo].[StudentInterests] WITH CHECK ADD CONSTRAINT [FK_StudentInterests_Interests6] FOREIGN KEY([interestID_7])
REFERENCES [dbo].[Interests] ([interestID])
GO
ALTER TABLE [dbo].[StudentInterests] CHECK CONSTRAINT [FK_StudentInterests_Interests6]
GO
ALTER TABLE [dbo].[StudentInterests] WITH CHECK ADD CONSTRAINT [FK_StudentInterests_Interests7] FOREIGN KEY([interestID_8])
REFERENCES [dbo].[Interests] ([interestID])
GO
ALTER TABLE [dbo].[StudentInterests] CHECK CONSTRAINT [FK_StudentInterests_Interests7]
GO
ALTER TABLE [dbo].[StudentInterests] WITH CHECK ADD CONSTRAINT [FK_StudentInterests_Students] FOREIGN KEY([studentID])
REFERENCES [dbo].[Students] ([studentID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[StudentInterests] CHECK CONSTRAINT [FK_StudentInterests_Students]
GO