what is the best schema for this database scenario? - mysql

i have a database scenario that there is a product and user and market and all of this have a image and the image have name , alt , description so what is the best structure for the database schema
making only one table for image and reference product and user and market to it
make a images table for each module like Product_images , user_images , Market_images and so on
thanks in advance

Product, User, Market and Image table will be the good choice.
Image id will be mapped to respective table like product, user and market.
All the image attributes should be kept in image table

Related

database dynamic field(custom attribute)

Hi I have a table structure as
skills
id
name
users
id
name
and many to many relation
skill_user
skill_id
user_id
different users will have different skill which they will select from skills table thats fine. My problem is that there will be few custom skills(custom attributes) that user will fill in a blank text box. How do i manage this kind of problem please help. I am doing this is php/Laravel/Mysql
You can attach multiple values like this:
$user->skill()->attach($request->get('id'),array('customskill' => $request->get('customskill')));

Storing pictures AND a profile picture in MYSQL?

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.

Relational database design for photography website

I'm creating a database for a photography website and I want it to allow three main things -
Allow the owner/admin to create client accounts and credentials,
Specifying which photos should go into three different portfolio galleries on the site, and,
Displaying a unique client's photos (and only their photos!) to them when they log in.
This is my first database design ever - based on responses below, I've added that emphasis ;) and edited the design as below.
IMAGESimage_id,filename,description,client_id,date_uploaded,
USERS/CLIENTS
client_id,
client_name
username,
password,
PORTFOLIO
portfolio_id,
portfolio_name,
PORTFOLIO_IMAGES
id,
image_id,
portfolio_id,
Am I correct in thinking that the final id in PORTFOLIO_IMAGES would allow me to display one image in multiple galleries?
Thanks
As it is your first DB-Design and as you may have mentioned in the comments here is something essential missing: ER-Diagram. This helps a lot understanding what's going on.
ER-Diagram
Synonyms: User=Account, Image=Photo, Gallery=Portfolio
Known Roles: "Admin", "Client"
Examples for Rights: "Create Account", "Delete Account", "Watch images", "Add Gallery", "Remove Gallery", "Upload image", "Delete image", ...
Table Design
User
id
name
password
Image
id
user_id
filename
description
upload_date
Image_Gallery
image_id
gallery_id
Gallery
id
name
User_Role
user_id
role_id
User_Right
user_id
right_id
Role
id
name
Role_Right
role_id
right_id
Right
id
name
You may want to remove all the things with Right if it is enough to separate user privileges by Role.
Within the tables images and users, you will be referencing the clients id, not the name.
I would create a separate table for the galleries, as clients tend to have new wishes every three month. So you maybe need to add more galleries.
table "galleries"
id
name
table "image_is_in_gallery"
image_id
gallery_id
PRIMARY(image_id, gallery_id)
You might want to consider normalization.
Assuming that usernames are unique - two people can't have the same username, come on - then you can eliminate "id" in the Users table in order to help prevent update/insert/delete anomalies (doing this would almost certainly put Users into BCNF, and likely DKNF - this is a good thing).
Clients is fine. What is the difference between Clients and Users, though? Really... seems similar to me.
Make sure that references are done using foreign key constraints, and I think that should be better.
EDIT:
Based on the new design, I have these suggestions:
Change Clients/Users into three tables:
ClientNames
- ClientID (PK)
- ClientName
ClientUsernames
- ClientID (PK)
- Username
UsernamePasswords
- Username (PK)
- Password
This is safe and says that one Client/User has one name, one Client/User has one Username, and one Username has one Password. I don't see another good decomposition (in the sense that it's going to be in a tight normal form).
You can eliminate one of these tables by eliminating the synthetic "ClientID" key, if you want. There are disadvantages to this, and it may not be possible (some people do have the same name!).
The problem here is that it is likely that ClientID, ClientName, and UserName determine each other in a way that isn't amenable to stuffing them in the same table.
use client id instead of client_name on the images and users table
Add another table, portfolio with at least name and id columns
Add another table, portfolio_images with two columns, image_id and portfolio_id. This will allow the feature mentioned by #Alex in the comments
response to edit
You can do the one image in multiple portfolios by querying PORTFOLIO_IMAGES and JOINing with images or portfolios as necessary. For example, if you want to display the wedding portfolio (psuedo-code)
SELECT filename,...
FROM images img
INNER JOIN portfolio_images pimg on img.image_id = portfolio_images.image_id
WHERE pimg.portfolio_id = <whatever the id is for wedding portfolio>

Am I going overboard with MySql tables?

While developing I am adding databases with each major addition.
My latest is an image gallery program, I'm asking myself all these questions/
A simplified version of possible tables
Image (id, name, path, state)
Gallery (id, name, path, state)
Image Gallery Relationship (image id, gallery id)
Image User Relationship (image id, user id)
Image Venue Relationship (image id, venue id)
Image Event Relationship (image id, event id)
Gallery User Relationship (image id, user id)
Gallery Venue Relationship (image id, venue id)
Gallery Event Relationship (image id, event id)
I'm thinking the same photo can be owned/taken by a user, assigned to an event and to a venue, in a gallery from the users photos, in a gallery related to the event, in a gallery related to the venue.
Now if I want to use the same database to store the profile picture of a user, would I create another table for that? Should I put the profile picture id into the user table? Should I put the photo ownership into the Image table? But most likely a major part of the pictures won't be uploaded by users. What about the "profile picture" for a an Event or venue? Or the cover picture of a a gallery? Am I creating to many tables or not enough?
sidenote Generally when creating columns I think if it is a required, single value attribute then place it in the table, If it could be null or associated with multiple things it should probably be in another table. But I'm at 60 tables now and I'm not even half way through my program.
These day's I'm really lacking a mentor, so all advice is greatly appreciated
Pic related, it's my current database
also If my question can be more generalized to help future people with similar questions I don't mind edits
Whatever you decide, you should frame that and hang it in the gallery.
You may want to simplify it. Look at it differently: consider one central things table to which the image might be associated and then find common ground for those things. Add a type field (gallery, venue, event). Maybe you'll add an extra field or two, but in the end it will greatly simplify your work and the database. I'm going through the same soul searching on something I'm creating.
It all depends on the exact relationships between all your objects. Your description was a little bit fuzzy, so I'm going to be making some assumptions here, but here's what I get out of it:
Main rule: Only use join tables (like Gallery_Image_Relationship (gallery_id, image_id)) when you have a many-to-many relationship; I suspect gallery-image is a good example. A gallery contains many images, and an image can belong to many galleries. For anything else, though, they're just needless complication.
For the rest of your relationships, just put a foreign key in one of your tables. If it's one-to-many, the table is obvious: A user can upload many images, but an image can only be uploaded by one user? Then your images table gets an uploader_id column.
That said, here's what that would turn into in your DB:
users:
id,
name,
profile_image_id, #A user can only have one profile pic (at a time)
etc.
images:
id,
name,
uploader_id, #It can only be uploded by one person
etc.
galleries:
id,
name,
cover_image_id,
etc.
images_galleries: # 'cause images:galleries is many:many
image_id,
gallery_id
(I have no idea what language SO thinks that was. It highlighted user, by, is, and the apostrophe...)
It gets more subjective when you're trying to tie images to venues and/or events. Do you expect most/all of your images to be of a particular event? If so, then you'll probably want an event_id column in your image table. Same goes for venues.
But if you're expecting lots of images not to be of any specific event, then those columns would just be a waste of DB space. So would a gallery typically be tied to a single event/venue? If so, then that's where your foreign keys belong.
And if neither of those fit too well, then your join tables are still an option. I doubt the normalization diehards would approve, but sometimes you don't need to be picky.
Hope that helps!

Find out the Product Sold

I have a query regarding a table structure.We are using single SQL SERVER 2008 Database for two online selling websites.i.e.,The products which the two websites uses are same,but the description about the products are different.For example,we will sell a "Toy" of same price and model on both websites but with different description.At present I used two different id for websites say,Id "1" for Website 1 and Id as "2" for website 2.And also populated the Product table with diiferent Id's for same product along with the description and website id.
Now the problem is I need to find out how many "Toy" has been sold out in both websites together.
Can any one help me out?Should I introduce a separate table structure to relate the Productid?
It seems to me your choices are
minimal change to achieve your purpose
produce a properly normalised design
A minimal change might be that table you propose
A normalised design might be
product
code
standard_description
standard_price
website
code
description
website_product
website_code
product_code
description
price
order
id
website_code
...
order_line
order_number
line_number
product_code
quantity
...
That way the same product has the same code on both websites but you can have differing descriptions (and prices if necessary)