How should I relate two tables

2

I have a question about how to relate two tables.

The problem is I do not know what kind of relationship to create for this example.

Bearing in mind that in my FUEL table I have 2 records:

IDcombustible | Combustible
---------------------------
    1         | Diesel
    2         | Gasolina

Now I enter an engine and assign the type of fuel. The number 1 that is diesel.

idmotor | tipo_motor | combustible_idcombustible
------------------------------------------------
1       | '2.5cc'    | 1

Now the problem is that I need to enter another '2.5cc' engine but it exists with gasoline.

idmotor | tipo_motor | combustible_idcombustible
------------------------------------------------
1       | '2.5cc'    | 2

In doing so I realize that I again enter another record in the MOTOR table with the column tipo_motor with the value '2.5cc' but with the column combustible_idcombustible with value 2. I think it is not correct because there is redundancy of data.

What would be the correct way to relate the two tables?

    
asked by jeancarlos733 22.06.2016 в 06:19
source

3 answers

5

It seems that you need to create an additional table that supports your many to many relationship between tables motor and combustible . This would cause you to have less data redundancy. So:

CREATE TABLE combustible_motor (
    id INT PRIMARY KEY AUTO_INCREMENT,
    idCombustible INT NOT NULL,
    idMotor INT NOT NULL,
    FOREIGN KEY (idCombustible) REFERENCES combustible (idCombustible),
    FOREIGN KEY (idMotor) REFERENCES motor (idMotor)
);

And there you associate the records of both tables.

INSERT INTO combustible_motor (idCombustible, idMotor) VALUES
(1, 1), -- el primer 1 refiere a tu combustible Diesel y el segundo 1 al motor de 2.5cc
(2, 1) -- el 2 refiere a tu combustible Gasolina y el 1 al motor de 2.5cc;

Just in case, doing this means that you are going to have to remove the direct relationship between your table combustible to your table motor .

    
answered by 22.06.2016 / 06:30
source
2

An analysis of the following questions can show us the way: Can an engine use different types of fuel? Can a fuel be used in different engines? If both answers are true, you can use a "many to many" relationship which makes use of an intermediate table between engine and fuel, for example motor_combustible, which would contain the engine id and the fuel id (by removing the id from fuel of the motor table).

In this way, you would only store the relationships between an engine and a fuel. If you want to deepen on how to avoid redundancy of data, I invite you to research the normalization base data.

    
answered by 22.06.2016 в 06:41
1

Similar to what Luiggi Mendoza said, and without ignoring the database engine, you can do it with two tables, just like in your diagram:

CREATE TABLE combustible(
    idcombustible INT(11) AUTO_INCREMENT PRIMARY KEY,
    combustible VARCHAR(45) NOT NULL
    )
    ENGINE=INNODB;

Your second table would look like this:

CREATE TABLE motor(
        idmotor INT(10) AUTO_INCREMENT PRIMARY KEY,
        tipomotor VARCHAR(45) NOT NULL,
        idcombustible INT(11) NOT NULL,
        FOREIGN KEY (idcombustible ) REFERENCES combustible(idcombustible ) ON     DELETE RESTRICT ON UPDATE CASCADE    
        )
        ENGINE=INNODB;
    
answered by 01.03.2017 в 21:06