SQL multiple primary key tables, duplicate values of the query data when inserting data is duplicated?

Source: Internet
Author: User


SQL Multiple primary key tables, when the inserted data is duplicated, are prompted to violate the primary KEY constraint that cannot be inserted by the error. So, how do I find duplicate values for the inserted data?



Workaround: Use Group by

Suppose there is a table #a , there are saleid,vendorid,comid,price,saleprice,quantity and other fields.

The primary key is: Saleid,vendorid,comid three of them. Assume that the data source that you insert #a may be duplicated.

namely: Saleid,vendorid,comid Three fields are the same field, then insert #a The primary key violation is reported, and the primary key constraint is violated.

If you want to find out #a The values for inserting duplicates in the table can be queried using the following statement:

Select Saleid,vendorid,comid,count (*) from#a GROUP by Saleid,vendorid,comid have Count (*) >1

The above statement will follow three primary keys Saleid,vendorid,comid Group, if Saleid,vendorid,comid The same records are counted in the same number of rows. the having condition behind Group By is the query that the same record is greater than 1 rows.


This article is from the "Remote _chen" blog, make sure to keep this source http://chenshirong.blog.51cto.com/3945427/1884101

SQL multiple primary key tables, duplicate values of the query data when inserting data is duplicated?

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.