Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Guest
Nov 12, 2009 Nov 12, 2009

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

TOPICS
Server side applications
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 12, 2009 Nov 12, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Nov 12, 2009 Nov 12, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 12, 2009 Nov 12, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 13, 2009 Nov 13, 2009

Just to add to bregent's explanation, when creating the lookup table, both columns should be designated as a joint primary key (do not use auto_increment). This ensures that each combination is unique.

gallery_id
model_id
11
12
13
21
24
33
34
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Nov 13, 2009 Nov 13, 2009

David and bregent,

Thank you both for always coming to my rescue!

The issue here is that I never know from shoot to shoot how many people I will be working with, so it may not be feasible to build a database with the capacity to cover all possible probabilities. David, I did review your suggestion in that lifesaver, 'PHP Solutions' and recall seeing the very thing you have so kindly suggested. I think, however, that I may be trying to do too much.

The help from each of you is sincerely appreciated!

Cheers,

wordman

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 15, 2009 Nov 15, 2009

Wordman-GL wrote:

The issue here is that I never know from shoot to shoot how many people I will be working with, so it may not be feasible to build a database with the capacity to cover all possible probabilities.

That's the beauty of a lookup table. It consists of just two columns, the primary keys of the records you want to look up. So, you can have as few or as many models connected with a shoot as you like. Looking at it in terms of primary keys, it looks confusing to start with, but it's a lot easier if you think of it this way:

shoot
model
weddingAngela
weddingJohn
bikinisAngela
bikinisMonica
bikinisSarah
fitness kitJohn
fitness kitSarah
baby clothesDavid

You could have twenty models associated with a shoot or a hundred if you want. Because the lookup table stores only the primary keys of each record, it doesn't take up much capacity, and databases zip through columns of numbers at lightning speed.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Nov 15, 2009 Nov 15, 2009

David,

OOOHHH!!! I think I was looking at this BACKWARDS. It's not that I would need to populate my main 'Galleries' table with model information, rather I would keep the model info solely in the lookup table. Once again, your ability to explain these things has worked perfectly!

So to issue model credits, I just pull a query for the lookup table and then display that data. Excellent. Thank you so very, very much!

Sincerely,

wordman

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 16, 2009 Nov 16, 2009

>So to issue model credits, I just pull a query

>for the lookup table and then display that data.

Yes, you've got the idea now, but I just want to add some clarificaiton. David called this a lookup table. It's similar to a lookup table but it's really not the same. A lookup table is commonly used in picklists to allow users to select from different options. Or, they can be use to lookup details of other id values. For example, you could store a State id in the main table, and then pull the full state name from a lookup table.

When talking about many-to-many relationships, the table that links the two is more commonly referred to as a link, association, or intersection table.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Nov 16, 2009 Nov 16, 2009
LATEST

I removed the last post because I have altered the database design. Will share it soon.

Thanks!

wordman

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines