Skip to main content
November 13, 2009
Question

MySQL - Can multiple Foreign Keys be linked to a main table?

  • November 13, 2009
  • 1 reply
  • 1170 views

Hello all,

Here's a pretty straightforward scenario and question.

I'm building the database to manage my online photo galleries.

Some of the galleries will feature more than one model.

Because of this, I have a table for model names in order to credt those in the photos.

SO...

If one gallery has say, three models, another has two, another has four, how do I assign three, two or four separate foreign keys to the main table (called Galleries)?

I want to use this data to display model names in the text description of each gallery and feel that this would streamline things for me.

Please let me know if this makes sense.

Thank you in advance for your help!

Sincerely,

wordman

This topic has been closed for replies.

1 reply

Participating Frequently
November 13, 2009

>If one gallery has say, three models, another has two,

>another has four, how do I assign three, two or four

>separate foreign keys to the main table (called Galleries)?

You are talking about a many-to-many relationship. You need to create a link (relation) table with two columns. This stores the id of the gallery and the id of the model. So, if you gallery has 3 models, the link table would have 3 rows - each row would contain the same gallery id and the individual model id's.

November 13, 2009

bregent,

Thank you for that! Without getting into the specifics, if I understand this correctly, I should build this table anticipating the maximum number of models appearing an a shoot and size the table accordingly, correct?

Many thanks for taking the time to pick up this thread!

Sincerely,

wordman

Participating Frequently
November 13, 2009

>if I understand this correctly, I should

>build this table anticipating the maximum

>number of models appearing an a shoot

>and size the table accordingly, correct?

I'm not sure I understand. Are you asking about database space allocation? The table width will be small - only two columns that each store an id value. The number of rows is the number of models associated with each gallery. If gallery 1 has four models and gallery 2 has 3 models, that's 7 rows total.