简体   繁体   中英

Error with mysql relation foreign key

I try to relate tables mysql but I have this error

Error Code: 1215. Cannot add foreign key constraint

this is the mysql script

    CREATE DATABASE prototipo;
USE prototipo;

CREATE TABLE tb_tipo_usuario(
id int not null,
tipo varchar(30) not null,
constraint PK_tb_tipo_usuario_id primary key(id)
);

CREATE TABLE tb_usuarios(
id_usuario int(5) zerofill not null auto_increment,
usuario varchar(30) not null,
clave varchar(30) not null,
nombre varchar(30) not null,
apellido varchar(30) not null,
cedula varchar(30) not null,
cargo varchar(40) not null,
tipo_usuario int not null,
fecha_registro date not null,
constraint PK_tb_usuarios_id_usuario primary key(id_usuario),
constraint fk_tb_usuarios_tipo_usuario foreign key(tipo_usuario) references tb_tipo_usuario(id)

);


CREATE TABLE tb_tipo_pollo(
id_tipo_pollo int(2) zerofill not null auto_increment,
tipo_pollo varchar(30) not null,
constraint pk_tb_tipo_pollo_id_tipo_pollo primary key(id_tipo_pollo)
);

create table tb_parvada(
id_parvada int(5) zerofill not null,
cantidad int not null,
constraint pk_tb_parvada_id_parvada primary key(id_parvada)
);


create table tb_entrada_parvada(
id_entrada_parvada int(5) zerofill not null auto_increment,
tipo_pollo int not null,
cantidad int not null,
fecha_entrada date not null,
fecha_registro TIMESTAMP  DEFAULT CURRENT_TIMESTAMP,
comentario varchar(500) null,
id_usuario int not null,
constraint pk_tb_entrada_parvada_id_entrada_pollo primary key(id_entrada_parvada),
constraint fk_tb_entrada_parvada_tipo_pollo foreign key(tipo_pollo) references tb_tipo_pollo(id_tipo_pollo),
constraint fk_tb_entrada_parvada_id_entrada_parvada foreign key(id_entrada_parvada) references tb_parvada(id_parvada),
constraint fk_tb_entrada_parvada_id_usuario foreign key(id_usuario) references tb_usuarios(id_usuario)
);


create table tb_muerte_pollo(
id_muerte int(5) zerofill not null auto_increment,
id_parvada int not null,
cantidad int not null,
fecha date,
fecha_registro TIMESTAMP  DEFAULT CURRENT_TIMESTAMP,
motivo varchar(300),
id_usuario int not null,
constraint pk_tb_muerte_pollo primary key(id_muerte),
constraint fk_tb_muerte_pollo foreign key(id_parvada) references tb_parvada(id_parvada),
constraint fk_tb_muerte_pollo_id_usuario foreign key(id_usuario) references tb_usuarios(id_usuario)
);

The error is when I try add the table tb_entrada_parvada or the table tb_muerte_pollo I don't know how repair this error, just work when I delete the foreign key

The DataType and attributes of the column in the reference table should be same as the current table. You are missing ZeroFill for many columns. That why you are getting the error. Kindly Change it.

create table tb_entrada_parvada(
id_entrada_parvada int(5) zerofill not null auto_increment,
tipo_pollo int(2) zerofill not null,
cantidad int not null,
fecha_entrada date not null,
fecha_registro TIMESTAMP  DEFAULT CURRENT_TIMESTAMP,
comentario varchar(500) null,
id_usuario int(5) zerofill not null,
constraint pk_tb_entrada_parvada_id_entrada_pollo primary key(id_entrada_parvada),
constraint fk_tb_entrada_parvada_tipo_pollo foreign key(tipo_pollo) references tb_tipo_pollo(id_tipo_pollo),
constraint fk_tb_entrada_parvada_id_entrada_parvada foreign key(id_entrada_parvada) references tb_parvada(id_parvada),
constraint fk_tb_entrada_parvada_id_usuario foreign key(id_usuario) references tb_usuarios(id_usuario)
);

create table tb_muerte_pollo(
id_muerte int(5) zerofill not null auto_increment,
id_parvada int zerofill not null,
cantidad int not null,
fecha date,
fecha_registro TIMESTAMP  DEFAULT CURRENT_TIMESTAMP,
motivo varchar(300),
id_usuario int zerofill not null,
constraint pk_tb_muerte_pollo primary key(id_muerte),
constraint fk_tb_muerte_pollo foreign key(id_parvada) references tb_parvada(id_parvada),
constraint fk_tb_muerte_pollo_id_usuario foreign key(id_usuario) references tb_usuarios(id_usuario)
);

DEMO FIDDLE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM