-1

I'm trying to add foreign key from table customer. to an existing column of a table. where table name is cardvalidator column name is cid.

Using query-

alter table cardvalidator add constraint fk_cid foreign key (cid) references customer (cid);

But I'm getting an error cannot add or update child row in mysql. Can anyone help me to solve this

Tushar
  • 3,527
  • 9
  • 27
  • 49

1 Answers1

0

What the error means is that you already have rows in the table cardvalidator with values in column cid that do not exist in the corresponding referenced column in table customer. The reason for you to definite foreign key constraint on the child table cardvalidator is to make sure customer ids in the table always match those in the parent table customer. As this requirement fails, your constraint definition is failing.

Simple solution: If you are testing with the tables in your development environment, correct rows in cardvalidator that have customer ids that don't exist in customer table. Either you have to update cid column values in the table or add new customer rows in the customer table.

mvsagar
  • 1,998
  • 2
  • 17
  • 19