-1

I have this code:

CREATE TABLE IF NOT EXISTS DIRECCIONES
(
    Cedula varchar(10) primary key,
    Direccion varchar(50)
);

ALTER TABLE DIRECCIONES
    ADD CONSTRAINT pkpersona1 FOREIGN KEY (Cedula) REFERENCES PERSONA(Cedula),
    ADD CONSTRAINT pkbodega1 FOREIGN KEY (Cedula) REFERENCES BODEGA(id_bodega);

I get this error:

Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails.

How can I solve it?

These are the definitions for PERSONA and BODEGA:

CREATE TABLE IF NOT EXISTS PERSONA(
  Cedula varchar(10) PRIMARY KEY,
  Nombre varchar(20),
  Apellido_Paterno varchar(20),
  Apellido_Materno varchar(20)
);
CREATE TABLE IF NOT EXISTS BODEGA(
    id_bodega varchar(10) primary key,
    admin varchar(10) not null,
    direccion varchar (50) not null
);

and they're all populated

  • 4
    Primary keys don't reference other tables, they uniquely identify records _inside_ the table in which they are defined. _Foreign_ keys reference the primary keys in other tables. Your design has a problem. – Tim Biegeleisen Aug 24 '20 at 04:00
  • 1
    Is your table PERSONA and BODEGA already populated ? – Atif Aug 24 '20 at 04:07
  • 1
    Show us the PERSONA and BODEGA table definitions. – jarlh Aug 24 '20 at 06:21
  • 1
    It seems unlikely that `cedula` should relate to the `id` of another table. Your best bet is to give the full details of the `bodega` table and explain (giving examples) *exactly* how rows in `direcciones` relates to rows in `bodega`. – MatBailie Aug 24 '20 at 06:22
  • This is a faq. Please before considering posting read your textbook and/or manual & google any error message or many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names & site:stackoverflow.com & tags; read many answers. If you post a question, use one phrasing as title. Reflect your research. See [ask] & the voting arrow mouseover texts. – philipxy Aug 25 '20 at 02:44
  • Please in code questions give a [mre]--cut & paste & runnable code, including smallest representative example input as code; desired & actual output (including verbatim error messages); tags & versions; clear specification & explanation. Give the least code you can that is code that you show is OK extended by code that you show is not OK. (Debugging fundamental.) For SQL that includes DBMS & DDL (including constraints & indexes) & input as code formatted as a table. [ask] Pause work on the overall goal, chop code to the 1st expression not giving what you expect & say what you expect & why. – philipxy Aug 25 '20 at 02:44
  • I already edited the question with the definitions of BODEGA and PERSONA. – Viviana Vera Aug 25 '20 at 03:09
  • Still no [mre]. I even summarized that link in a comment, act on all of the comment at least. But I also said this is a faq, act on my faq comment. PS See [How do comment @replies work?](https://meta.stackexchange.com/questions/43019/how-do-comment-replies-work) to learn to use @x to notify one non-sole non-poster commenter x re a comment. – philipxy Aug 25 '20 at 03:44
  • Possible duplicate of [Mysql error 1452 - Cannot add or update a child row: a foreign key constraint fails](https://stackoverflow.com/q/1253459/3404097) – philipxy Aug 25 '20 at 03:51

2 Answers2

0

ERROR 1452: Cannot add or update a child row: a foreign key constraint fails

if this be the case. data in table PERSONA and BODEGA that link to DIRECCIONES should not be exists because DIRECCIONES(not has any data). if it exists then it violate the rule of foreign key.

0

on the line --> ADD CONSTRAINT pkbodega1 FOREIGN KEY (Cedula) REFERENCES BODEGA(id_bodega); the reference is not found, the column Cedula probably does not exist in the Table BODEGA.

LordFirus
  • 5
  • 1
  • 1
    It doesn't matter if `bodega.cedula` doesn't exist. That code only requires that `direcciones.cedula` and `bodega.id_bodega` exists. – MatBailie Aug 24 '20 at 06:24