You find yourself in a typical relationship n,m
.
In this case, your problem of products included in sales is the same problem of students enrolled in disciplines:
In this case, the above scheme is in the Entity-Relationship Model, or MER. This model is very useful for making quick data modeling, and also for having a deep understanding of the business involved. However, not everything is flowers. SQL is not directly compatible with MER, because SQL follows the Relational Model, MR, based on relational algebra.
To turn from MER to MR, it is very important to know the aridity of the relationship. In your case, the interesting part is in the relationship many-to-many, in English Many-to-Many.
More about turning MER into MR in this answer
In a relationship Many-to-Many, you have m
entities of a type relating to n
entities of the other type. In this case, you create an intermediate table with foreign keys for the two entity tables. Example:
I sold a drill and two saws.
My table of entity produto
:
produto
| prodid | prodnome |
+--------+-----------+
| 1 | serrote |
| 2 | furadeira |
My table of entity pedido
:
pedido
| pedid |
+-------+
| 1984 |
My table for relationship:
item
| itemid | prodid | pedid | quant | valor |
+----------+--------+-------+-------+--------+
| 1783 | 1 | 1984 | 2.0 | 10.00 |
| 2123 | 2 | 1984 | 1.0 | 150.00 |
To find out which products (and their quantities) are in the 1984 application:
SELECT
prod.prodnome,
i.quant
FROM
pedido ped
inner join item i on (ped.pedid = i.pedid)
inner join produto prod on (i.prodid = prod.prodid)
WHERE
ped.pedid = 1984
In my case, I am modeling according to needs I have already passed in the company where I work. There are cases where you want to sell the same item two or more times in the same order (for example, as bonus item, exchange and sale itself), so I define your primary key as itemid
. In most cases, however, a relationship n,m
usually does not have the repetition of entities.
You need an intermediate relation table
ITENS
, which contains the reference for the product and for the application in question. This is a relationship table, but nothing prevents it from having its own attributes, such as product item 2 being sold at R$50 a unit, being sold 2 units– Jefferson Quesado
For this I would need to predefine a maximum number of products in the cart, which would be the number of columns of the new table ITEMS? So it would register the id of each product
– Jhonatan Pereira
I work with a sales force system, and I must say that it is not necessary to make any limitation on the number of columns. I don’t think you understand how a relationship table works; I’m typing a more complete answer on the subject
– Jefferson Quesado
I understood, but I couldn’t see it. Thank you
– Jhonatan Pereira
I hope my answer has removed your doubts
– Jefferson Quesado
Yes, you did, I’m doing the tests
– Jhonatan Pereira
I updated my answer by placing a link that works with the MER to MR conversion algorithm
– Jefferson Quesado
Thanks, it worked here!
– Jhonatan Pereira