8
It is good practice to create a table to record all images from a database, or I can create columns that save image information for each specific need?
For example, let’s assume that I want to create a table for categories of a site and each category of the table has an image.
tlb_category
- id
- title
- img_name
- img_ext
- date_created
- date_updated
- status
or is more correct like this?
tlb_category
- id
- title
- img_id
- date_created
- date_updated
- status
tbl_image
- id
- img_name
- img_ext
- date_created
- date_updated
- status
Which is best for good practice? Do you have any other suggestions? The second form seems to me more correct, but it is normal to have several images of categories, products, avatars of users, etc... All mixed in a table?
Interesting answer. Yes, it is a ratio of 1 to 1, a category can only have one image. But I will add: In this database, there will not only be this table of categories with images. Are there other tables (products, users' avatars) that will need images, in this case it would be more advantageous to create a table to store all images? Or using a table for each need can still be more advantageous?
– Thiago Yoithi
It still depends. If images are always an integral part of where they’re going to be used, and not independent, there’s no point in doing this. If the image is something independent and by chance will be attached to another item of the database, then it is interesting. It depends on the degree of associativity. You have to conceptualize well what you need to know how to implement. No one will be able to conceptualize the best for you unless you do all the lifting work. If everything will be a 1:1 me relationship, probably, put in the respective tables. Until I discover a reason to avoid this.
– Maniero
Cool. I liked your advice, I had thought about it, whether it would compensate to create a general table of images or not. I finally decided that I will not do this, because for example these images belonging to the category will not be used anywhere else but the categories. I marked yours as an answer, thank you very much!
– Thiago Yoithi
Actually, you didn’t score.
– Maniero
Oxe, now scored, hahaha.
– Thiago Yoithi
I liked your answer - but did you notice that from the second to the first example, various information is being flouted? This decides for me, first of all, which model should be used. Using the first template, he can never know when an image has been updated, because he has not this information - so simple.
– Daniel
I read @Thiagoyoithi’s question like this: Which is the best model, what I throw half my information away, or what not?
– Daniel
@Danielgomes can’t see what you saw.
– Maniero
You don’t read the
date_created
oftbl_image
how the date the image was created? Anddate_created
oftlb_category
as the date a new record was entered in the category? I see 7 items against 11!– Daniel
@Danielgomes but should be the same information, which even in the second would be redundant. But if it is not, if it can have a different date, then just create a column to save this date on the first.
– Maniero
And you think @Thiagoyoithi knows that? And he only committed one slip by duplicating all data? ;)
– Daniel
@Danielgomes there I do not know, but it is not difficult to perceive the time that is riding this.
– Maniero
Ahaha, I’m aware of that. I will think about how I will do, but you are right @Danielgomes, in the first way it is not possible to save the date when the image was created. However, this is not such useful information in the context of my application, as the image will be mandatory in the creation of the category. I think I can use the table very similar to the first form, keeping only the date of registration and update of the category.
– Thiago Yoithi
@Thiagoyoithi I don’t think you really need it, but why can’t you save the date? Only if it’s because there’s no field. Then just create it, if it’s useful.
– Maniero