What am I doing wrong with this code in mysql

0

Hi, I am a 2nd year student at ASIR and I have an exercise in the creation of tables, data insertion and queries that I must deliver shortly and I have been stuck for more than I review, I do not give with the problem. I appreciate any help.

The error I get when creating the second table. It tells me that the NumDpto column does not exist but it is that it is a foreign key does not have to exist in the same table (Employees but in Departments)
Thank you very much.

    
asked by adal 04.12.2018 в 15:54
source

2 answers

5

Your problem is in the index of the Employees table not in the FK. It is not NumDpto if not NDep

Change it to

index (NDep)
    
answered by 04.12.2018 в 16:06
1

Your code should look like this:

CREATE TABLE Departamentos(
    NumDpeto INT NOT NULL PRIMARY KEY,
    NomDepto VARCHAR(30),
    Localizacion VARCHAR(30)
)ENGINE=INNODB;

CREATE TABLE Empleados(
    NEmp INT NOT NULL PRIMARY KEY,
    NomEmp VARCHAR(30) NOT NULL,
    Empleo VARCHAR(30),
    FechaCont DATE,
    Supvs INT,
    Salario INT,
    Ndep INT,
    Comis INT,
    NumDepto INT NOT NULL,
    INDEX(Ndep),
    CONSTRAINT fk_empleado_depto FOREIGN KEY(NumDepto) REFERENCES Departamentos(NumDpeto)
)ENGINE=INNODB; 


CREATE TABLE GradoSal(
    Grado INT NOT NULL PRIMARY KEY,
    SalarioMin INT NOT NULL,
    SalarioMax INT NOT NULL
)ENGINE=INNODB;

What I did was:

  • in the second table, called Empleados declare a CONSTRAINT that has NumDepto as a foreign key associated with NumDepto of the parent table; which in this case is Departamentos
  • Assign a foreign key name called fk_empleado_depto
  • answered by 04.12.2018 в 16:13