MYSQL query question

Posted on 2011-05-11
Last Modified: 2012-05-11
I have a varchar field in table Table1 called pid that is set to primary key.  There is supposed to not be any duplicates here.

what syntax do I use to:

1.)  find and list any duplicates
2.)  is there a field type (int etc.) that will throw an error if you try to insert a duplicate #?
Question by:pda4me
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    Because you are using a varchar, keep in mind that the DB will not look solely at the numbers (or letters) to determine duplicity. Just a single strategic space can make the difference between two keys. For example, the following are all different values:

    123 456
     123456(leading space)
    123456 (trailing space)

    Open in new window

    LVL 33

    Expert Comment

    If there is a primary key constraint on the field, there won't be any duplicates.  Otherwise, this query will find the dupes:

    select pid
    from Table1
    group by pid
    having count(*)>1

    Author Comment

    ah, makes sense...would int be a better choice?  the values are strictly numerical...
    LVL 33

    Accepted Solution

    IMO yes, int would be better, assuming there are no values bigger than 2 billion -- in which case use bigint instead.
    LVL 74

    Assisted Solution

    by:käµfm³d 👽
    would int be a better choice
    It would probably be the easiest to implement. If you *really* wanted (or had a business reason) to keep the varchar, then you could probably write an update/insert trigger, but I think changing the type would be a lot simpler.

    Author Closing Comment

    Thanks!  Awesome help!
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    NP. Glad to help  = )

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Suggested Solutions

    As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
    If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now