-1

I've got some question(s) How many tables should i create for my products ?

2???=products & products pics???

if yes then my mysql tables will be like that ? 1.products= p_id, p_name, p_descr, p_pieces, p_categ, p_subcateg(??) &

2.products_pics= pp_id, product_id, thumb, image, type (???)

Well im a little bit confused cause i think its not so necessary to use products_pics into the db anymore .

My product entries will be around 3.000 for the beginning . Can someone tell me what im going to write-do ??? Thank you very much !

  • _Note:_ PHPMyAdmin is just a web based management tool for managing mysql databases. Unless you're modifying that applications core, you're not actually working with PHPMyAdmin. – M. Eriksson Jan 25 '18 at 08:46
  • create only one table for the products and for image take p_image column. for multiple images of product insert formatted string as "image1.png,imgae2.png" – Sanjit Bhardwaj Jan 25 '18 at 08:47
  • 2
    @SanjitBhardwaj - You should _never_ insert multiple comma separated values into one column. That is a _very_ bad practices with some serious downsides. [Read about Database Normalization](https://www.essentialsql.com/get-ready-to-learn-sql-database-normalization-explained-in-simple-english/) – M. Eriksson Jan 25 '18 at 08:48
  • Create two tables. One with `products` and one with `product_images`. The product images tables should simply contain: `id, product_id, image`. The `product_id` is a foreign key to the `id` field in your `products` table. – M. Eriksson Jan 25 '18 at 08:49
  • @MagnusEriksson okay.. thanks for the information :) – Sanjit Bhardwaj Jan 25 '18 at 08:50
  • @MagnusEriksson the table you've suggested is okey..just give proper foreign relation and indexes.. – Darshan Jan 25 '18 at 08:50

2 Answers2

1

You should make 3 tables

  • products
  • images
  • products_images

In table products you have id_prod (primary key), name_prod, and other.

In table images you have id_img (primary key), name_img or img_src.

In table products_images you link images with products, and structure should be like id (primary), id_prod, id_img.

This way you will not have a duplicate content in tables.

-1

If you only have images / documents pertaining to products only, and you want to store them in your database, you can store the image as blob/longblob directly inside the products table as p_image for example. But when you want to retrieve it you will get the binary data, so you should write it to a file or convert it to view it as image under browser.

And if you have multiple images for the same product, you should create another table and use p_id as foreign key.

However, it is not advisable to store images in the database, for more information please refer to : Storing Images in DB - Yea or Nay?

Lalati
  • 316
  • 2
  • 14
  • 1
    Images are files and should be stored on the file system unless you have a _really_ good reason not to (there are situations when storing small images in the DB might make sense, even though I can't think of any right now). Also, even if the products only have one image now, specs change so it's better to do it more dynamic from the start (like having a separate product_image-table with filenames). – M. Eriksson Jan 25 '18 at 08:54
  • no need to save the images as blob and write it to a file each time..there's no need of it.. – Darshan Jan 25 '18 at 08:55
  • @MagnusEriksson in my answer i stated if marilou wanted to store them in the database, that's the way to do it – Lalati Jan 25 '18 at 08:57
  • Sure, but we should explain that this is a bad way of doing it. – M. Eriksson Jan 25 '18 at 08:57
  • @MagnusEriksson indeed! – Lalati Jan 25 '18 at 08:59
  • @MagnusEriksson updated the answer, and thanks for clarification ! – Lalati Jan 25 '18 at 09:03