I'm doing an insert in mysql and searching the internet I found the replace into that if the record exists it deletes it and enters the new one, now try it in mysql and it gave me a foreign key error since the registry is associated to another table, as the structure of the foreign key must be so that the replace into can be executed.
Error: # 1451 - Can not delete or update parent row: a foreign key constraint fails ( u261045981_app
. PERSONAS
, CONSTRAINT PERSONAS_ibfk_1
FOREIGN KEY ( PA_PAIS_ID
) REFERENCES PAISES
( PA_PAIS_ID
))
I insert the insert into a table called countries that is associated with people
Table structure
CREATE TABLE 'PAISES' (
'PA_PAIS_ID' int(11) NOT NULL,
'PA_NOMBRE' varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
'PA_MONEDA' varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
'PA_CODIGO_ISO' varchar(12) COLLATE utf8_unicode_ci DEFAULT NULL,
'PA_DECIMALES' smallint(6) DEFAULT NULL,
'PA_SEPARADOR_DECIMAL' char(1) COLLATE utf8_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
COMMENT='Paises en los que estara disponible la aplicacion';
ALTER TABLE 'PAISES'
ADD PRIMARY KEY ('PA_PAIS_ID');
CREATE TABLE 'PERSONAS' (
'PE_USU_ID' int(11) NOT NULL COMMENT 'id incremental del usuario',
'PA_PAIS_ID' int(11) DEFAULT NULL COMMENT 'FK tabla pais, permite
asociar pais al usuario',
'PE_NOMBRE' varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
'PE_CORREO' varchar(128) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT
'correo de la persona, campo unico y usuario que se pide al iniciar
sesion',
'PE_CLAVE' varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT
clave del usuario',
'PE_ESTADO' smallint(6) DEFAULT NULL COMMENT 'estado del usuario, 1
es activo y 0 es inactivo y no puede ingresar a la aplicacion',
'PE_FECHA_CREACION' timestamp NULL DEFAULT current_timestamp() ON
UPDATE current_timestamp() COMMENT 'fecha en la cual se crea la
cuenta del usuario'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
COMMENT='Tabla donde se almacena los datos de los usuarios';
ALTER TABLE 'PERSONAS'
ADD PRIMARY KEY ('PE_USU_ID'),
ADD KEY 'PA_PAIS_ID' ('PA_PAIS_ID');
ALTER TABLE 'PERSONAS'
ADD CONSTRAINT 'PERSONAS_ibfk_1' FOREIGN KEY ('PA_PAIS_ID')
REFERENCES 'PAISES' ('PA_PAIS_ID');
Finally the insert to table countries
REPLACE INTO 'PAISES' ('PA_PAIS_ID', 'PA_NOMBRE', 'PA_MONEDA','PA_CODIGO_ISO', 'PA_DECIMALES', 'PA_SEPARADOR_DECIMAL')
VALUES (1, 'Chile', 'Peso Chileno', 'Clp', 0, NULL);