Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 171145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:56:08+00:00 2026-05-11T12:56:08+00:00

I want to add a constraint to my table student such that any value

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T12:56:08+00:00Added an answer on May 11, 2026 at 12:56 pm

    That’s a foreign key constraint.

    ALTER TABLE student   ADD CONSTRAINT somename   FOREIGN KEY (major_id) REFERENCES major (id); 

    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

    create table student_major (  id serial not null unique primary key,           -- optional, but good idea  student_id int not null references student(id),  -- fk   major_id int not null references major(id),      -- fk  UNIQUE (student_id, major_id)                    -- no redundant relations ); 

    Comment:

    -1 for rejecting composite keys. – Bill Karwin

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add a named constraint in my CREATE TABLE statement that will
Question: I want to add a unique constraint on a mapping table (n:n). I
I want to add a default constraint to a existing table. So i use
I want to alter a table to add a constraint during upgrade on a
I have a foreign key constraint in my table, I want to add ON
I have a table with which I want to add a check constraint after
I want to add a column for a table which would become a PRIMARY
I want add my custom sidebar next right column all page. Please check this
I have a list of string values that I want add to a hashtable
I want a column from a table to have a limited domain, which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.