Search code examples
mysqlforeign-keysforeign-key-relationship

I can't delete row after append it a foreign key


I have two table second and third

second_id(PRIMARY KEY)  second_name           
   1                    .........
   2                    .......
   3                    .........

third_id(PRIMARY KEY) third_name second_id(FOREIGN KEY for second.second_id)
      1                  .....                 1
      2                  .....                 1
      3                  .....                 1
      4                  .....                 2
      5                  .....                 2

Now i want to delete a row fromsecond where second_id=2 (DELETE FROM second WHERE second_id=2) but it does not work. It says Successful 0 row(s) affected What is more is, it happened after i append a foreign key to third.second_id (i added foreign key after table is created).


Solution

  • You will have to delete the rows in third table with second_id = 2 and then delete the rows from second with id = 2 or have ON DELETE CASCADE in the foreign key constraint