Sina
Sina

Reputation: 1132

Database schema design for handling multiple image resources

I have some tables in the db which have either one image or several images associated with them. For instance:

# table 1
- id
- name
- created_at

# table 2
- id
- name
- created_at

Now each of these tables has either one or many images. A typical design would be like this:

# table 1
- id
- name
- image_path
- created_at

# table 2
- id
- name
- created_at

# images table
- id
- table_2_id
- image_path
- created_at

However, I happened to have several problems with design as the following:

  1. I have many tables associated with one or more images.
  2. Images are going to be uploaded in the different hosts for storage capacity sake.
  3. There might be more tables added to my database with the same needs.
  4. Dependent on domain changes some tables' image path might change as well.

So now I want to dealing with this problem as a multi-dimensional table for images is the right design choice and is it also going to be future proof?

# images
- id
- table_id
- table_name
- image_path
- created_at

Best regards. Thank you.

Upvotes: 3

Views: 2118

Answers (2)

Jacques Amar
Jacques Amar

Reputation: 1833

You are looking at the problem in the reverse way. You need 1 table with all your images and each table that needs an image will have a link to the images tables

images table relationsship

Upvotes: 4

Jason
Jason

Reputation: 601

You might consider doing a quick install of a popular open source CMS like WordPress or Drupal and add a few images to see how they accomplish this. Many CMS may have thought through some issues you have not considered.

Upvotes: -1

Related Questions