I am trying to make a page where the user has his own photos but also his own profile photo. I am uploading the actual photos to a file server then inserting the photo names into a MYSQL Database. So my table so far is,
photos
-photo_id
-photo_name
-user_id
So how do I declare a photo as a profile photo? What is the common method? Do I create a different table?
Depending on what other photos are in that same table it might make sense to put profile photo filenames in a separate table or to create another column in your table for a boolean photo_type. Read up on designing data structures and normal forms.
Related
I've created a db in mySQL that stores many things, but specially images. The table name for storing the images is image, like so:
image (image_id, title, caption, filename, published_date, ...)
It's been almost 2 years and i've uploaded almost 5000 images into the table.
Now, i want to add a new functionality. I want to group similar images so when im looking at an image, i can also have the option to look at images that are similar.
I'm not sure if i need a new table or should i use the same table or both. Any ideas/suggestions on how it should be?
You should create another table with that similiarities.
Why can't it be the same table ? One record in You table can have many similiar records (so it will be adding many columns to that table or for every similiarity there will be another row in YOur table. So the only logical option is to create another table.
IdFromMainTable | IdOfSimiliarRecord
Later on You can show that similiarites in a view easily joining that table by IdFromMainTable. Or both IdOfSimiliarRecord and IdFromMainTable. [depends if u want to add 2 records for similiar records or just one for similiar pair]
It sounds like what you want to do is create tags for the images. There are a number of ways to do that, but adding the tags to the same table would prevent a whole lot of joins from taking place and would likely be faster. You could just store the tags as JSON (if you're using MySQL =>7.5.8).
I am storing images in file system and updating the image path in database.
Table structure
photo_id,user_id,photo_no,photo_url
Users can upload maximum of 5 photos. photo_no is the field where i store the order. The first photo will be always the main photo. But users can make any photo as main photo and they can also arrange the photo.
If the user wants to make the 3rd photo as main photo(photo_no 1). Then i have to update photo_no 3 to some dummy temp number and update photo_no 1 with 3 and update temp number with 1. This way i can swap the photos. But i don't think this to be a good method. Can any suggest a good way to do that.
is possible to handle this situation with a good table design?
If the photos order can remain the same I would suggest that you introduce a new flag (column). So you can have:
photo_id, user_id, photo_no, photo_url, is_main_photo
and just put 1 for the main photo and 0 for other photos.
Let's say I'm making a shopping website and need to store paths for these types of images:
User Avatars x 250,000
Product Images x 10,000,000
Customer Photos x 100,000
Forum Uploads x 50,000
Where the number of images in each category is constantly growing.
Would it be better for me to create a separate table for each type of image, or to put them all into a single table with a type column to distinguish between them?
The reason I'm thinking it might be better to put them into separate tables is that the number of product images, for example, will always be significantly higher than the number of user images. So when a user changes their avatar, or when a page is loaded where avatars are retrieved from the database, then wouldn't it save a lot of time adding/retrieving the record if the tables were separate?
Depends. If it is just an URI you could store it in the table where the actual data belongs. So for the user's avatar, use a field in the user table.
If you have more properties for the image and fields are shared amongst image types, there is no harm in saving them in the same image table.
Another thing to keep in mind is the frequency of changing the field values. If for some type rows are frequently changed or deleted / added, it might be better performance wise to group volatile data together.
I'm developing website in which will be categorized advertisements. In each category will be possible different fields of input (example: for car there will be motor size, for cat there will be a race). So I'm thinking how to build database to manage this (I will use MYSQL database). One way you can see in attached picture, I know that also is solution to create table for each values datatape, but I'm wondering that it will slow down a website. This solution which is in picture will generate empty fields in sp_advertisement_value table what isn't good also.
What is in your opinion the best solution? Maybe there is something else?
p.s. Here is a link to database market.
You can store it like name/value pairs (more or less same to what you is described in the image you attached).
A simple schema would be a table having two columns name and value. Instead of having a column for each data type like value_int, value_string etc. have one single column value who's data type can be varchar (or Text as seems fit to you). You can do all the data conversion in your application code as per your needs.
You can do some normalization here too for instance instead of saving name you can make a separate lookup table named parameters having id, name and other related information and have the parameter_id in the table where you are storing parameter values.
I have a classifieds system I'm working on.
People are able to add photos to a classified, but I only display one when displaying the list of classifieds.
To do that, I have a linking table between classifieds and photos that has a "is_main" boolean field.
When someone deletes one of their classified photos, I want to:
1) See if there is more than that photo tied to the classified.
2) If there is, update the next photo and set that "is_main" field to TRUE.
Just trying to find out the most efficient way to do this.
In my opinion you should not set a 'is_main' flag in the photo table but add the ID of that photo to the classified record. Finding the 'main' Photo for display purposes is much faster (this happens often, I assume).
When you delete a photo from the photo table you need to see if it is the one referenced in the classifieds table or not. If it is, then you can select the next photo associated with that (e.g. by using min(PhotoID) or min(Created), where created is the datetime when so photo is added to the photo table.
Conclusion: the is _main property of a photo is not a flag on the photo table but rather a FK in the classifieds table.
In my opinion, I find storing image names in the database a bit redundant.
I'd rather make a directory named after classified id, and store corresponding images there.
So, if user deletes a photo, he just deletes a photo.
To keep one photo as main one, I'd call it main.jpg.
if it's going to be deleted, I'd read directory content and rename whatever photo came first.
that's all