-1

I have a REST api that will retrieve info from a database with 4 columns: student, course, university and info.
Given a student,course and university, it will retrieve info that is student-related.
If student is null, then given course and university, it will retrieve info that is course-related.
So ideally i want the primary key to be a composite of student,course,university.

But DB2 doesn't seem to let me create a primary key with a column that could be null.

The other option would be to have all three columns student,course,university be non-nullable.
In that case, DB2 lets me create a primary key using these 3 columns, but i would then have to enter a record where student would be null. Not sure if that is good practice.

stumped
  • 491
  • 3
  • 6
  • 19
  • And your question is? – mustaccio Aug 16 '14 at 21:26
  • The question is what would my options be? I want to make a primary key with 3 columns, one of which i would like to be nullable. Since DB2 is not letting me do this, what are other options? – stumped Aug 16 '14 at 21:39
  • 2
    It sounds like what you actually want is multiple tables - one for `Student`s, one for `University`s, and one for `Course`s (plus linking/cross-reference tables). Then, when your REST api is called, it figures out which table(s) to hit based on the information it was(n't) given. Or do multiple sections in your api, for each thing to be retrieved. You're not required to make your db look like your accessing api (and there are cases where you really don't want to). – Clockwork-Muse Aug 17 '14 at 07:07

1 Answers1

0

I think what you are trying to say is add a primary key to a db2 table?

ALTER TABLE DATABASE.TABLE ADD PRIMARY KEY (STUDENT,COURSE,UNIVERSITY)
Lorbat
  • 385
  • 2
  • 8
  • 27
  • I'd like the Student column to handle null values, but after that, DB2 does not seem to let me create a primary key using the Student column. – stumped Aug 16 '14 at 21:34
  • 1
    You can't create a primary key on a null value, read this answer:http://stackoverflow.com/questions/386040/whats-wrong-with-nullable-columns-in-composite-primary-keys – Lorbat Aug 16 '14 at 21:37