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

  • Home
  • SEARCH
  • 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 1022897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:32:01+00:00 2026-05-16T11:32:01+00:00

I have 2 tables with many to many relation: student ( id int(11) NOT

  • 0

I have 2 tables with many to many relation:

student 
(
id int(11) NOT NULL, 
name varchar(255), 
primary key(id)
);

teacher
(
id int(11) NOT NULL, 
name varchar(255), 
primary key(id)
);

and I should do 3 table – student_has_teacher

  1. option add id separate field primary key

    student_has_teacher
    (
    id int(11) NOT NULL,
    teacher_id int(11),
    student_id int(11)
    primary key(id)
    );

  2. option make 2 fields primary key

    student_has_teacher
    (
    teacher_id int(11),
    student_id int(11),
    primary key(teacher_id,student_id),
    foreign key(teacher_id) references teacher(id),
    foreign key(student_id) references student(id)
    );

What is better option and why?

Thanks

  • 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. Editorial Team
    Editorial Team
    2026-05-16T11:32:01+00:00Added an answer on May 16, 2026 at 11:32 am

    The two options are not equivalent. In option 1 there can be multiple pairings of each teacher and student. In option 2 only 1 row is permitted for each combination of teacher and student.

    There is another difference as well. In 1 the student and teacher are nullable. In 2 they aren’t.

    On the information given I don’t see why a teacher would need to be paired with the same student more than once. So as a guess I’d say that 2 was more appropriate, but it all depends on the business requirements and you haven’t really given much information to say either way.

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

Sidebar

Related Questions

I have these tables: media id (int primary key) uri (varchar). media_to_people media_id (int
I have two tables, products and product_tags . products table; product_id int name varchar
A question: I have 2 tables: Product id INT name VARCHAR(64) something TEXT else
I have 2 tables having many to many relation and hence there is junction
I have 3 tables: Book, Section and Content. I want to add many-to-many relation
I have multiple level parent-child tables(GrandParent, Parent and Child, all many to one relation,
I have 2 tables: First (Cases) Second (Comments) in one to many relation ;
I have a many-to-many relation in 3 tables: ProgramUserGroup and Feature are the two
I have two tables: Items and Colors. They have a many to many relation.
My DB have two tables - Question and Topic. To implement many-to-many relation, there

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.