I have a mini enrollment system.
there are tables for professors, subjects, students, grades, and subjects that has students.
now, when you add a professor, you input it’s professor ID. but there is aslo a profNO. and the profNo is the primary key. since you can add more subjects to that professor, the profNo will increment but the ID stays the same.
PROBLEM: now, I want to set a restriction that when one wants to add a professor, if he inputs a professor ID that already exists, an error would appear which says “professor ID already exists” but the professor ID is not unique.
now, take a look at the photo. there are same professor IDs with different profNo. and that is because of the subjects they have. professor IDs were duplicated when you add subject to a professor. but when you add a professor, could it be possible that when one inputs an existing ID, an error would appear?
sample http://sphotos-e.ak.fbcdn.net/hphotos-ak-ash3/578472_162759700529875_718824031_n.jpg
Yes you can do what you’re asking. Just add a simple check before you insert new professors.
Pseudocode for when a new professor is submitted:
However, it doesn’t seem like a very logical system if you are duplicating professor details over and over when a professor has multiple subjects.
Consider this DB structure:
Table:
professorsFields:
id,firstname,lastname,genderTable:
subjectsFields:
id,professorid,subjectname,subjectcodeWith that, you would only maintain a single record in
professorsfor each professor and the same for the subjects table. When you insert new subjects, you would populate the professor ID which links back to theprofessorstable without duplicating data.This would be a simple structure that makes your database much more efficient, now depending on how complex your system needs to be you may need to normalise it even further. For example, if a subject can have multiple professors then you would not include a professor ID in the subject table, instead you would have a new table
professor_subject_associationwith fieldsprofessorid,subjectidwhere you would link multiple professors to subjects.