Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report