problems when inserting in a bd

3

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 , CONSTRAINT tarjetas_tipo_tarjeta_FK FOREIGN KEY ( id ) REFERENCES tipo_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.

    
asked by Victor Gimenez 30.07.2018 в 05:37
source

2 answers

1

Your error is here:

ALTER TABLE 'tarjetas'
  ADD CONSTRAINT 'tarjetas_tipo_tarjeta_FK' FOREIGN KEY ('id') 
  REFERENCES 'tipo_tarjeta' ('id');

You are indicating that the column that is foreign key is the id (auto-incremental column of the% table% co_), when the table goes from tarjetas records will start to give you problems, because it would already create orphan records. The relationship must be between the 4 column in the tipo table and the tarjetas column in the id table.

The restriction should then be written like this:

ALTER TABLE 'tarjetas'
  ADD CONSTRAINT 'tarjetas_tipo_tarjeta_FK' FOREIGN KEY ('tipo') 
  REFERENCES 'tipo_tarjeta' ('id');
  

Since the erroneous restriction already exists, it should be deleted from the   following mode:

ALTER TABLE 'tarjetas'
  DROP FOREIGN KEY  'tarjetas_tipo_tarjeta_FK'
     

And then create it again correctly:

ALTER TABLE 'tarjetas'
  ADD CONSTRAINT 'tarjetas_tipo_tarjeta_FK' FOREIGN KEY ('tipo') 
  REFERENCES 'tipo_tarjeta' ('id');
    
answered by 30.07.2018 / 05:46
source
0

I told you that I tested your code with the following code and it is functional

CREATE TABLE 'tipo_tarjeta' (
  'id' int(11) NOT NULL AUTO_INCREMENT,
  'descripcion' varchar(20) NOT NULL,
  PRIMARY KEY ('id')

);

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')
);

ALTER TABLE 'tarjetas'
  ADD CONSTRAINT 'tarjetas_tipo_tarjeta_FK' FOREIGN KEY ('id') 
  REFERENCES 'tipo_tarjeta' ('id');

INSERT INTO 'tipo_tarjeta' ('id', 'descripcion') VALUES
(1, 'Regular'),
(2, 'Estudiante'),
(3, 'Discapacitado'),
(4, 'Adulto Mayor');

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);
  

What I see as a problem is that you try to add a FK to a table   that does not yet exist; that is, you want to add the fk    tarjetas_tipo_tarjeta_FK to the table cards you are declaring   first, but that fk REFER to the table type_card that just   at that moment it does not exist.

     

That is, tables that do not have foreign keys go first in your   statement of your BD and the tables that have foreign keys go just   after

    
answered by 30.07.2018 в 05:51