SQL: insert all other columns of rows with the same primary key updated.

Source: Internet
Author: User

SQL: insert all other columns of rows with the same primary key updated.

In SQL writing, we often encounter such problems. During each insert operation, you must determine whether the data table already exists. For the same data, all the columns except the primary key are the same. If no data exists, the data is inserted. If yes, the data is updated. In this situation, we do not know whether this column exists, so there is a problem with insertion and update, so some people choose to delete the corresponding id, and then re-insert all, the problem here is that the primary key auto-increment and deletion will produce a huge hole, which is not a good design.

 

In MySQL, I use the following methods:

Insert into case_law (case_id, laws_id) (select 1, 2 from dual where not EXISTS (select case_id, laws_id from case_law where case_id = 1 and laws_id = 2)

The above simple means can be used to check whether the insertion exists.

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.