I'm coming from a non-SQL database (MongoDB), and I'm building up a new project, but with MySQL (a relational Database).
I'm making a restaurant system, and in this system, there's a page where you can create a product and describe its ingredients.
In MongoDB, (non-SQL database), you can make this schema for your document, (I'll describe it with JSON just to give the point):
// imagine this is a document of a product stored in the products collection
{
name: "Burger",
price: 200,
ingredients: [
{ name: 'tomato', qty: 2 }, // qty: ie quantity
{ name: 'cheese', qty: 1 },
{ name: 'bread', qty: 2 },
{ name: 'meat', qty: 1 },
]
}
In an SQL relational database, however, you'll have to create something similar to this:
At a first glance, it looks ugly, but after thinking for a bit, now I'm starting to get used to it.
Firstly, I want to ask a question regarding this approach:
- Is it the way to go for this problem?
I'm sure you have come across this scenario hundreds of times.
Second, I have another question:
Now the user have pressed the "create" button to create his product, and he sent the request to the server to process this request,
How am I going to insert these rows into the database?!
Is it by doing a for loop and sending 5 requests to the database:
- the first request, is to create the row with the ID#1 in the product-ingredients table.
- the second request, is to create the row with the ID#2 in the product-ingredients table.
- the third request, is to create the row with the ID#3 in the product-ingredients table.
- the fourth request, is to create the row with the ID#4 in the product-ingredients table.
- the fifth and final request, is to create the row with the ID#1 in the products table.
This looks crazy, a little bit.
That's what my brain is saying.
It's why I'm particularly asking this question.
Later, there will an update product page, and the similar for loop with a bunch of requests is going to be executed on the database.
The question now is HOW.
I appreciate everyone who's going to take my brain hand into the world of relational databases. 🌹