I have 2 tables in my database one of them is cards and the other type cards basically after normalizing the table cards I am left with a type fk that links to the card type pk, then insert data in this last one like this its structure more down then I wanted to insert several data in the table cards and I gave error of
1452 - Can not add or update a child row: a foreign key constraint fails (
prueba
.tarjetas
, CONSTRAINTtarjetas_tipo_tarjeta_FK
FOREIGN KEY (id
) REFERENCEStipo_tarjeta
(id
))
basically because of the foreign key that it has but I do not see where the error is, could you give me a hand? below my SQLs
Here is the table:
CREATE TABLE 'tarjetas' (
'id' int(11) NOT NULL AUTO_INCREMENT,
'uid' varchar(16) NOT NULL,
'fecha_expedicion' date NOT NULL,
'fecha_vencimento' date NOT NULL,
'estado' tinyint(2) NOT NULL,
'saldo_actual' decimal(12,0) NOT NULL,
'tipo' int(11) NOT NULL,
PRIMARY KEY ('id'),
UNIQUE KEY 'numero' ('uid')
);
CREATE TABLE 'tipo_tarjeta' (
'id' int(11) NOT NULL AUTO_INCREMENT,
'descripcion' varchar(20) NOT NULL,
PRIMARY KEY ('id')
);
ALTER TABLE 'tarjetas'
ADD CONSTRAINT 'tarjetas_tipo_tarjeta_FK' FOREIGN KEY ('id')
REFERENCES 'tipo_tarjeta' ('id');
Data insertion:
INSERT INTO 'tipo_tarjeta' ('id', 'descripcion') VALUES
(1, 'Regular'),
(2, 'Estudiante'),
(3, 'Discapacitado'),
(4, 'Adulto Mayor');
here lies the problem
INSERT INTO 'tarjetas'
('id', 'uid', 'fecha_expedicion', 'fecha_vencimento', 'estado', 'saldo_actual', 'tipo')
VALUES
(NULL, 'AE3D6C9A', '2017-08-09', '2018-08-09', 0, 50000, 1),
(NULL, '7E5B6C9A', '2017-08-09', '2018-08-09', 1, 350000, 1),
(NULL, "0EEE6B9A", "2017-08-09", "2018-08-09", "1", 0, 1)
Interestingly, if I insert this whole scheme it works perfectly but if I insert several more records at the same time, it jumps back Obs. if I insert one record at a time I do not have that problem
I would be very useful a hand thank you.