SQL Federated primary Key check weight

Source: Internet
Author: User

The last day of 2014, today when importing data to the database, there is a problem, is the joint primary key to go heavy.

The thing is, there is a table m, I want to find a table that imports a lot of data, and I need to set the fields a (int) and B (int) union as the primary key.

But when I set the primary key, I was told I had duplicate content and couldn't set the primary key.

Normally, the combination of these two fields is unique and should not be duplicated, in order to find duplicate data and see what is wrong.

Adopt distinct Way

But found

Select COUNT (distinct name, id) from A

This sentence in SQL does not apply, http://www.cnblogs.com/rainman/archive/2013/05/03/3058451.html this inside write, of course, SQL will also error.

So only two fields can be converted and re-assembled into a single field for querying

Select cast(A as varchar(Ten))+cast(B as varchar(Ten)) Newkey fromMGroup  by cast(A as varchar(Ten))+cast(B as varchar(Ten)) having Count(cast(A as varchar(Ten))+cast(B as varchar(Ten)))>1

The code itself is not difficult, that is, to convert the idea, can not only focus on the two int type of primary key, re-assemble to facilitate the query.

SQL Federated primary Key check weight

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.