1

I'm making a website for a client but I stumbled upon a problem and I need some advice on it.

For each project, they want to have the possibility to set a variable amount of images and (sometimes) some corresponding text.

I was thinking about storing all of the information in one field, instead of making field_1 to field_99 just in case they need 99 fields.

// database column

'../fotos/foto1.png',
'hier komt tekst',
'../fotos/foto2.png',
'', (empty text)
'../fotos/foto3.png'

This solution has some disadvantadges, there must be better manners out there to achieve this.

What's the preferred way to do this?

2
  • 1
    Put the images in a separate table and use a foreign key. Commented Jun 4, 2012 at 12:33
  • Others have answered the question, but since you say you're doing this for a client, I just want to be sure you understand how bad an idea concatenating multiple data items or storing numerical data into a text field is. You will regret doing it. If you've already done it in other parts of your application, the time to fix it is right now, before it becomes even more difficult to do so. Text fields should only store text. Commented Jun 4, 2012 at 17:07

2 Answers 2

3

Create another table (e.g. FOTO_CODES) with all possibly values of foto and generate id for them.

Create another child table that will have the master table record id and ID from FOTO_CODES table and FOTO data (Image).

It's called normalization.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 When using a relational databases, always try and turn many columns into many rows.
0

The solution you described violates the principle of atomicity and therefore the 1NF. You'd have trouble maintaining and querying data in this format.

This is a classic 1-to-many relationship, that can be modeled in two ways:

1) Identifying relationship:

enter image description here

2) Non-identifying relationship:

enter image description here

Both have pros and cons, StackOverflow already has plenty of discussions on this topic.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.