Impact of primary key on data update

Source: Internet
Author: User

Tag: The person string set succeeds Deb causes the code lang to be set

The primary key must be unique in the same table, and if the primary key specified at the time of the data update is duplicated with the data already in the table, the exception that violates the PRIMARY KEY constraint is caused. The Fnumber field in the T_debt table is the primary key if we execute the following sql:

UPDATE T_Debt set FNumber = "2" WHERE FPerson="Tom"

Since a record with a fnumber field of 2 already exists in the table, an error message similar to the following is reported when running this sql:

Violates the primary KEY constraint "PKt_debt1920bf5c". cannot be in the object "dbo." Insert duplicate key in T_DEBT.

And if we set a distinct value for fnumber, it will be inserted successfully, execute the following sql:

UPDATE T_Debt set FNumber = "8" WHERE FPerson="Tom"

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.

Impact of primary key on data update

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.