1

I want to insert array of string in my product table, i am very new in this mysql. like in mongodb i do it like this to store list of photos

const productSchema = mongoose.Schema({
    name : {type:String,trim : true},
    preview : String,
    photos : [String],
    description : String,
    isAccessory : Boolean,
    brand : String,
    price : Number

})

and this is my request body

{
        "id": "1",
        "name": "Men Navy Blue Solid Sweatshirt",
        "preview": "https://assets.myntassets.com/h_1440,q_100,w_1080/v1/assets/images/7579188/2018/11/5/08a7b230-ee8f-46c0-a945-4e835a3c01c01541402833619-United-Colors-of-Benetton-Men-Sweatshirts-1271541402833444-1.jpg",
        "photos": [
            "https://assets.myntassets.com/h_1440,q_100,w_1080/v1/assets/images/7579188/2018/11/5/08a7b230-ee8f-46c0-a945-4e835a3c01c01541402833619-United-Colors-of-Benetton-Men-Sweatshirts-1271541402833444-1.jpg",
            "https://assets.myntassets.com/h_1440,q_100,w_1080/v1/assets/images/7579188/2018/11/5/efc3d5b9-1bb3-4427-af53-7acae7af98951541402833591-United-Colors-of-Benetton-Men-Sweatshirts-1271541402833444-2.jpg",
            "https://assets.myntassets.com/h_1440,q_100,w_1080/v1/assets/images/7579188/2018/11/5/c7e58861-3431-4189-9903-9880f5eebd181541402833566-United-Colors-of-Benetton-Men-Sweatshirts-1271541402833444-3.jpg",
            "https://assets.myntassets.com/h_1440,q_100,w_1080/v1/assets/images/7579188/2018/11/5/66490b64-32de-44b4-a6e4-fe36f1c040051541402833548-United-Colors-of-Benetton-Men-Sweatshirts-1271541402833444-4.jpg",
            "https://assets.myntassets.com/h_1440,q_100,w_1080/v1/assets/images/7579188/2018/11/5/957be784-7c5d-4e90-ab9f-0928015b22891541402833645-United-Colors-of-Benetton-Men-Sweatshirts-1271541402833444-5.jpg"
        ],
        "description": "Navy solid sweatshirt with patchwork, has a round neck, long sleeves, straight hem",
        "isAccessory": false,
        "brand": "United Colors of Benetton",
        "price": 2599
    },

now how can i do this in sql like what the table design for it.

Akash Verma
  • 67
  • 2
  • 6
  • Does this answer your question? [How to store arrays in MySQL?](https://stackoverflow.com/questions/17371639/how-to-store-arrays-in-mysql) – Mostafiz Rahman Jun 06 '20 at 18:51
  • well even if i can create another table how can i insert this array of photos in that table , like do i have to use loop . and another solution i was thinking of was to store json.stringify array in my photos column rather than creating new table , so which one should be the right approach?@Mostafiz Rahman – Akash Verma Jun 06 '20 at 19:03

1 Answers1

0

As per my understanding I can think of two possible options:

Option-1:
MySQL supports storing JSON. Store the string array in as a value against the desired key.

Option-2:
Store the string value with JSON.stringify.

Mostafiz Rahman
  • 8,169
  • 7
  • 57
  • 74