mysql – (SQL) Each property has multiple images and a cover image

Question:

Suppose I have a Property table where a property can have multiple images.

So I have another table, Images, with the url.

I also have an intermediate table that makes the N:N relationship between Properties and Images.

If I want a property, in addition to the images, to have a cover image, what is the most correct way for me to create this field?

Answer:

You can add a "Cover" column to your Images table, of a boolean type. This way you can indicate that this is the cover image, including more than one if you are doing a slide show. In your application, you define the rule for when to display the cover image or not.

Another approach is to add a column in your Property table that will be an FK to the Image table indicating which of the images will be the cover image. But leave this constraint nullable. Otherwise you would have to have the images registered before creating the initial registration for the property.

Scroll to Top