PHP, Relationship Too Much for Many

Asked

Viewed 47 times

0

My doubt is in the construction of Logic.

How do I, so that I determine item, can have more than one value?

EX: I have the table of vehicles and the table of inputs. Each vehicle can have more than one type of input ( Gasoline, Alcohol )

At the moment, the vehicles table is only with an input:

ID----- VEHICLE-INPUT

01----HHR000--GASOLINE.

I even thought about putting a column for each input, but I would like something more practical. This way:

ID----- VEHICLE-INPUT

01----HHR000--GASOLINE / ETHANOL / LUBRICATING OIL.

I can’t do it.

Thanks qq help

1 answer

0

If you’re using a relational database, you can’t use multivariate attributes, and the solution you thought really isn’t the most practical one. In relational databases, to deal with situations like this, tables are created called, in theory, weak entities.

In your case, you would have to create a third table (let’s call Veiculoinsumo - generally the name of the weak entities are formed by the name of the tables that gave rise to it), whose primary key would be formed by the primary key of the table of vehicles and the table of inputs, that is, by the field ID of the two tables.

In your queries and operations in the database, if you wanted to obtain or perform instructions on the data of this relation N:N, you would have to consider this new table created.

In a practical example, the table Veiculoinsumo would be (changed attribute name ID of the two other tables in the new table so as not to get confused):

ID_VEICULO--- ID_INSUMO

01 --- 01

01 --- 02

Whereas in the table of inputs the ID 01 corresponds to gasoline and 02 to alcohol, the above data indicate that the vehicle of ID 01 uses both gasoline and alcohol.

Using this solution, you can simulate the use of multi-valued attributes in relational databases.

  • Thank you for answering. Now I can understand better. But in this case, how to insert the data in the table Veiculoinsumo? In the registration page, I will have to create a registration page for vehicles and inform the ids of inputs? Like, Id_car, Model such, we inputs would be 1,2,1,1?? Pq, if it is in this logic, wouldn’t the msm who directly inform the input name? Thanks for the help.

  • Or would I have to enter data twice at msm temp in the Database? Once in the table vehicles, with the vehicle information, at the same time I enter the data 1,2,3 in the table Vehicles?

  • That would be more or less what you wrote in the second comment. What you would normally do in your case is: in vehicle registration, there would be an option (as a select that allows you to select multiple options - v. https://www.w3schools.com/html/tryit.asp?filename=html_elem_select_multiple) for the user to select the inputs that the vehicle uses. In the code that registers the data in the database, you first enter the vehicle data and, with the new ID of the registered vehicle that was generated in the database, you enter in the table Vehicle (+)

  • And the Ids of inputs selected by the user.

Browser other questions tagged

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