さて、あなたの要件を満たすためにあなたのデザインはうまくいくでしょう。与えられた仕様を満たすことができる他の2つの簡単に実装できるオプションがあります。
1.)インタレストテーブルに外部キーが設定された7つのインタレストを持つ学生テーブル。一連の一意の制約を使用して、同じ興味を持つ2つの生徒がいないことを確認します。null許容外部キーを使用し、項目4〜7でnullを許可します。
2.)利害関係の表を分割します。ルールは99%同じように機能しますが、必須の関心は、関心が選択された場合にのみ制約をトリガーします(したがって、学生は0または3〜8の関心を持つことができます)。これはもう少し合理的なアプローチであるため、興味とは別に学生テーブルにデータを入力できます。これにより、物事を拡張可能に保つことができます。
どちらのアプローチも、ANSI-92準拠のデータベースで実行可能な第3正規形です。SQLサーバーでの#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