foreign wrongly entered

1

I have been with this error for hours:

Error Code: 1005. Can not create table prueba . montaje (errno: 150 "Foreign key constraint is incorrectly formed")

Can someone tell me what I have wrong with my code?

CREATE TABLE Montador
(ID_Montador int NOT NULL,
RUT VARCHAR (13) NOT NULL,
primary key (ID_Montador),
Foreign key (RUT) references Persona (RUT));

CREATE TABLE Modelo_Dormitorio
(Cod_Modelo VARCHAR (30) NOT NULL,
Nombre_Dormitorio VARCHAR (50) NOT NULL,
primary key (Cod_Modelo));

CREATE TABLE Montaje
(Cod_Modelo int NOT NULL,
ID_Montaje int NOT NULL,
Fecha_Montaje VARCHAR(10) NOT NULL,
ID_Montador int NOT NULL,
primary key (ID_Montaje),
FOREIGN KEY (ID_Montador) references Montador (ID_Montador),
FOREIGN KEY (Cod_Modelo) references Modelo_Dormitorio (Cod_Modelo));
    
asked by Alex 14.10.2018 в 04:01
source

2 answers

1

When you create foreign keys, you must take into consideration that the fields chosen to function as llaves foráneas must be of the same type; then your answer should be like this

CREATE TABLE Montador
(ID_Montador int NOT NULL,
RUT VARCHAR (13) NOT NULL,
primary key (ID_Montador),
Foreign key (RUT) references Persona (RUT));

CREATE TABLE Modelo_Dormitorio
(Cod_Modelo VARCHAR(30) NOT NULL,
Nombre_Dormitorio VARCHAR (50) NOT NULL,
primary key (Cod_Modelo));

CREATE TABLE Montaje
(Cod_Modelo VARCHAR(30) NOT NULL,
ID_Montaje int NOT NULL,
Fecha_Montaje VARCHAR(10) NOT NULL,
ID_Montador int NOT NULL,
primary key (ID_Montaje),
FOREIGN KEY (ID_Montador) references Montador (ID_Montador),
FOREIGN KEY (Cod_Modelo) references Modelo_Dormitorio (Cod_Modelo));

DETAILS

  
  • table model_dormitorio has a column called Cod_Modelo that is a VARCHAR(30)
  •   
  • in the montage table you have a foreign key called Cod_Modelo but instead of putting it as VARCHAR(30) you put it as a type    INT then that's why it tells you that the foreign key is formed of   wrong way
  •   
  • Even though VARCHAR(10) and VARCHAR(30) are equivalent because they are of the same type, they are not equivalent because they are of different lengths, so   you must place same type of data and same length, I mean in   both tables place VARCHAR(30)
  •   
        
    answered by 14.10.2018 в 04:49
    0

    In this foreign key:

    FOREIGN KEY (Cod_Modelo) references Modelo_Dormitorio (Cod_Modelo));
    

    The problem is that Cod_Modelo in Modelo_Dormitorio is a Varchar(30) and in Montaje is a int .

    They can not be different.

        
    answered by 14.10.2018 в 04:10