Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a database table that contains two columns that are foreign keys have a third column which is the primary key?

I am guessing no, since the foreign keys are the primary keys in their own tables, so they will be unique.

More info

I am using MySQL and the following three tables are using the InnoDB engine.

=======================    =======================
| galleries           |    | images              |
|---------------------|    |---------------------|
| PK | gallery_id     |    | PK | image_id       |
|    | name           |    |    | title          |
|    | description    |    |    | description    |
|    | max_images     |    |    | filename       |
|    | enabled        |    |    | enabled        |
=======================    =======================

========================
| galleries_images     |
|----------------------|
| FK | gallery_id      |
| FK | image_id        |  <----- Should I add a PK to this table?
========================

Epilogue

Thanks for the excellent answers. I learned about composite keys and, after considering my specific case, I decided to make the image_id column in the galleries_images table a primary key. This way, images may only be associated with one gallery, which is what I want.

I am also going to implement a order_num column in galleries_images which I will use PHP logic to maintain. This way the user can put images in a specific order in each gallery. I ended up with this:

============================
| galleries_images         |
|--------------------------|
| PK, FK | image_id        |
| FK     | gallery_id      | 
|        | order_num       |
============================

Thanks again!

Epilogue II

Thanks to those of you who pointed out that I didn't even need this table. I didn't provide the most complete information to begin with. I ended up dropping the galleries_images table altogether and just added the gallery_id as a foreign key to the images table. Anyways, I still learned more than I thought I would and am grateful for the help.

like image 475
Mike Moore Avatar asked Jun 21 '10 15:06

Mike Moore


People also ask

Does a foreign key have to be a primary?

Yes, foreign key has to be primary key of parent table.

Can a table have two foreign key and no primary key?

Yes, you can make one without a Primary Key (or, another option is a Compound Primary Key - making the two references a unique pair, and using that as the unique identifying key - but even this isn't necessary (note: just because it "isn't necessary" doesn't mean it isn't "good practice"; it wouldn't generally be a ...

Can a table have 3 primary keys?

A primary key is a field or set of fields with values that are unique throughout a table. Values of the key can be used to refer to entire records, because each record has a different value for the key. Each table can only have one primary key.

Can a column be a primary key and a foreign key?

The primary key column(s) can be used (along with the foreign key) to create a reference between two tables. As shown earlier, the primary key column of one table can be the foreign key column in another table.


1 Answers

In theory, if the combination of the two foreign keys (FKs) is unique in the table, or if the combination of the two FKs plus some other column is unique, then the table has a compound primary key and there is no strict need to introduce another key as a surrogate primary key. However, it is not unusual to find that people do add an extra key. In part, it depends on what else the data in the table with the compound primary key will used for. If it describes something that will itself have rows from other tables associated with it, then it may make sense to introduce a simple PK.

Some software seems to require simple PKs, even though the Relational Data Model does not.

like image 122
Jonathan Leffler Avatar answered Oct 05 '22 17:10

Jonathan Leffler