Effects of foreign keys on data updates

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.