I want to create a database application for creating academic examination. The database contains, for the sake of simplicity, 3 tables as follows.
Problems:
- ProblemID (int, identity, primary key)
- Problem (nvarchar)
- Solution (nvarchar)
Categories:
- CategoryID (int, identity, primary key)
- Category (nvarchar)
ProblemCategory:
- CategoryID (int, composite primary key)
- ProblemID (int, composite primary key
Each problem is linked to at least one category and at most 5 categories. My question is how to make sure this constraint is hold in database level?
Bonus question:
Is the following design recommended as a replacement for the design above?
Problems:
- ProblemID (int, identity, primary key)
- Problem (nvarchar)
- Solution (nvarchar)
- CategoryID1 (int, not null)
- CategoryID2 (int, null)
- CategoryID3 (int, null)
- CategoryID4 (int, null)
- CategoryID5 (int, null)
Categories:
- CategoryID (int, identity, primary key)
- Category (nvarchar)