Get the App
SLTechnology News&Howtos  ›  Database  › 

SQL tables with multiple primary keys, when there are duplicates in the inserted data, query the duplicate values of the data?

Shulou Source: shulou.com Published: 2022-06-01 16:07:17 09月22日 Update

SQL tables with multiple primary keys will prompt an error that cannot be inserted in violation of the primary key constraint when the inserted data is duplicated. So, how do I find the duplicate value of the inserted data?

Solution: use group by

Suppose you have a table # a with fields such as saleid,vendorid,comid,price,saleprice,quantity.

The main keys are: three saleid,vendorid,comid. Suppose that the data source inserted into # a may be duplicated.

That is, if all three fields of saleid,vendorid,comid are the same, the primary key conflict will be reported when inserting # a, which violates the primary key constraint.

If you want to find duplicate values inserted in table # a, you can query them with the following statement:

Select saleid,vendorid,comid,count (*) from # a group by saleid,vendorid,comid having count () > 1

The above statements are grouped according to the three primary key saleid,vendorid,comid, and if the records with the same saleid,vendorid,comid are all the same, then there are several rows of the same records. The having condition after Group by is to query out that the same record is larger than 1 row.

Tags: Data three fields queries identical statements multiple data sources methods times conditions errors reports conflicts grouping prompts statistics Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Technology Shulou Information MySQL MariaDB