Relating a variable number of rows in one table to one in another

database-design

For an online store I'm currently considering the following SQL structure:

Table 'productImages':

+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| productImageID | int(11)      | NO   | PRI | NULL    | auto_increment |
| url            | varchar(200) | NO   |     | NULL    |                |
| baseWidth      | int(11)      | NO   |     | NULL    |                |
| baseHeight     | int(11)      | NO   |     | NULL    |                |
+----------------+--------------+------+-----+---------+----------------+

Table 'productImageSets':

+-------------------+---------+------+-----+---------+----------------+
| Field             | Type    | Null | Key | Default | Extra          |
+-------------------+---------+------+-----+---------+----------------+
| productImageSetID | int(11) | NO   | PRI | NULL    | auto_increment |
| productImageID1   | int(11) | YES  |     | NULL    |                |
| productImageID2   | int(11) | YES  |     | NULL    |                |
| productImageID3   | int(11) | YES  |     | NULL    |                |
| productImageID4   | int(11) | YES  |     | NULL    |                |
| productImageID5   | int(11) | YES  |     | NULL    |                |
| productImageID6   | int(11) | YES  |     | NULL    |                |
| productImageID7   | int(11) | YES  |     | NULL    |                |
| productImageID8   | int(11) | YES  |     | NULL    |                |
+-------------------+---------+------+-----+---------+----------------+

Table 'products':

+-------------------+--------------+------+-----+---------+----------------+
| Field             | Type         | Null | Key | Default | Extra          |
+-------------------+--------------+------+-----+---------+----------------+
| productID         | int(11)      | NO   | PRI | NULL    | auto_increment |
| title             | varchar(100) | NO   |     | NULL    |                |
| description       | text         | NO   |     | NULL    |                |
| uploaded          | datetime     | NO   |     | NULL    |                |
| productImageSetID | int(11)      | NO   |     | NULL    |                |
+-------------------+--------------+------+-----+---------+----------------+

As ugly as it looks and feels to me, this is the best I have so far.
The lowdown of it is that each product has a number of images, however this number is not the same for every product. This led me to creating the productImageSets table with the productImageID(1-8) columns, which each either point to a row in the productImages table, or are null.

What I'd like to know is if there's something I'm missing, or some better way to do this. Any help would be greatly appreciated!

Best Answer

The classic way to realize many to many relation is with intermediate table. Like Products(ID) - ProductImages(ProductID, ImageID) - Images(ID) What is the reason to make these "sets"?