I want to add a constraint to my table student such that any value entered in its majors column must also exist in majors table. How can I do this?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That’s a foreign key constraint.
Note that if student.major_id is nullable, the column’s value can still be null.
Note also your table doesn’t accommodate double majors. To do that, we’d have a student_major table that is a many-to-many relation between student and major. This also demonstrates creating foreign keys in a create table, instead of in an alter table
Comment:
So let me understand this. Bill agrees that I correctly answered the OP’s questions about constraints. He agrees that I correctly saw what the OP hadn’t asked about, possible double majors. But Bill still marks this answer as wrong because Bill and I disagree about composite keys.
I didn’t even say that a synthetic id was necessary; indeed, I specifically said it was optional but in my opinion a good idea. (Why? It ‘plays better’ with deletes, with tables that might reference student_majors, and with ORMS and generated code in general.)
Bill, that’s frankly petty. You marked down a correct answer over an elaboration (composite/synthetic) on an elaboration (students:majors being M:M instead of M:1), and over what’s a ‘religious’ war. Do you mark down correct answers because you disagree with the answerer’s stand on tabs vs. spaces or vi vs. emacs? Maybe you should have taken the time to give your own answer, rather than marking down correct answers.