A foreign key is a constraint that points to an existing data in another table, so the foreign key value must be present in the target table. If the updated data does not exist in the target table, the FOREIGN KEY constraint exception is violated. The Fperson field in the T_debt table is a foreign key to the FName field of the table T_person, if we execute the following sql:
UPDATE T_Debt set FPerson = "Merry" WHERE FNumber="1"
Because there is no data row in the T_person table that is equal to "Merry" in the FName field, the database system will report an error message similar to the following:
The UPDATE statement conflicts with the foreign KEY constraint "FKt_debtfperson__1a14e395". The conflict occurred in the database "demo", table "dbo." T_person ", column" FName ".
And if we set the value of the FName field that already exists in the T_person table for the Fperson field, it will be inserted successfully, executing the following sql:
UPDATE T_Debt set FPerson = "Lili" WHERE FNumber="1"
This sentence of SQL can be successfully executed correctly. Perform a SELECT * from t_debt to view the data in the table:
You can see that the data has been updated correctly in the table.
Effects of foreign keys on data updates