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.
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 the galleries_images table?
All tables should have a primary key.
It is not necessary to create a new surrogate column to act as primary key though. Taking John’s example it would be perfectly acceptable to have a composite primary key with the 2 primary key fields from other tables and a date field.
From a pragmatic point of view though sometimes creating a new surrogate column can be easier to work with than a composite one though if the PK is itself referenced in yet another table or for binding to various controls that don’t handle composite primary key’s well.
Edit
Following the update to your question I would just make the primary key composite on gallery_id, image_id. I don’t see any benefit of adding a new column.