Insert multiples ids to a table

Asked

Viewed 28 times

0

Hello,

I have the following problem:

I have the PERSON table and COURSE table, each person can choose more than one course, the id of the chosen courses need to be saved within the person table, what would be the best way to do?

  • You are only referring to the database modeling part and SQL?

  • That’s right, I thought about recording inside a varchar all ids but I think it’s gambiarra

1 answer

2

The ideal would be for you to have a table template similar to this:

inserir a descrição da imagem aqui

In these cases you wonder:

  • One Person may have as many Courses?
  • The Answer is "N" Courses, a person can have from 0 to N courses.
  • A Course can have as many People?
  • The Answer is also "N", a course can have from 0 to N people.

When we come across this situation of N-N there is the need to come up with an auxiliary table to connect the two tables which in the case is the table "Personal Course", it only exists in order to make the connection of Person with Course.

  • In this case when I insert a record it would look like this: INSERT INTO Personal?

  • That’s right, this way gives right, then only recovers with personal Where=1

  • 1

    Thank you very much, it worked right here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.