Creation of tables with cardinality 1 : N

Asked

Viewed 239 times

2

I have two tables, PRODUTOS and INSUMOS. To create a product I need several inputs, I would like to know how I should proceed with this relationship in Mysql. I must create a table called COMPOSICAO and in it work only with foreign key, this would be one way, there are other?

How should I proceed with this kind of worry in Laravel?

  • This will depend on the context or the scope. For you what this would be COMPOSICAO? Enjoy and make a [tour] too.

  • I’ll give you an example of how I imagine COMPOSICAO, let’s say you will manufacture a car, which items make up the car, engine, gearbox, windshield etc.

  • Gives a survey on normal forms, in your case it applies to 3a normal form.http://aprendaplsql.com/datamodelling/

2 answers

1


Usually you need an auxiliary table of association or mooring or intersection of products with inputs (this is called associative entity), where you make the link between them. I imagine it’s this composition you’re talking about, but I’m not sure I don’t have details.

DER com entidade associativa

In essence you will have the product code and the input code in each line and that will already be the primary key. It is common to have a secondary key with the inverse input-product. I do not know if that is what you mean by foreign key.

I don’t know what to do, but I imagine I’ll have to create a chart like that on his model. I know some will tend to want to graph objects in memory. For me this is a mistake, play the database model and be happier.

  • If I do as I mentioned above I won’t be doing wrong??

  • I imagine it’s all right, if you do as I said.

-1

If an input does not repeat itself in more than one product, you can put a foreign key in the Inputs table,

Insumos (*id*, nome, idproduto)
Produto (*id*, nome)

Thus, you guarantee that no other product will have that input.

Otherwise, you are correct. You must have an auxiliary table of type Composition,

Composição (*idproduto, idinsumo*)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.