database design - The number of categories must be greater than one but fewer than five -


i want create database application creating academic examination. database contains, sake of simplicity, 3 tables 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 linked @ least 1 category , @ 5 categories. question how make sure constraint hold in database level?

bonus question:

is following design recommended replacement 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)

question 1: each problem linked @ least 1 category.

answer: declare foreign key constraint on problems table.

problems.problemid references problemcategory.problemid 

(presumably there's foreign key constraints declared on problemcategory reference problems.problemid , categories.categoryid.)

question 2: each problem linked @ 5 categories.

answer: declare constraint on problemcategory table. (as branko points to.)

where (select max(catcount)        (select count(*) catcount              problemcategory              group problemid)       ) <= 5 

question 3: design 5 cat id per problem record.

answer: no, that's terrible idea. (read text normalisation.) consider:

  • how query problems category "normalisation"?
  • how prevent same cat getting repeated on given problem?
  • what mean problem have cat id3 , id5 id4 null?

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -