Search code examples
sqlprimary-keynavicat

Primary Key problems when importing


I am building a database and have set field A, Release #, as the primary field. I am running into a problem where duplicate keys are found which gives error on import.
Specifically, release # "49221" is the value of a certain release from a certain website. On a different website, the release # is "0000000049221"

Release# 49221 is a completely different release than is "0000000049221". Is there a way for me to import both of them into the same table while still retaining field "release #" as the primary key?


Solution

  • This usually happens when implicit conversions are given, check the type of your primary key.

    For example:

    CREATE TABLE TEST
    (
      COLUMN_1 INT,
      COLUMN_2 VARCHAR(8)
    )
    
    INSERT INTO TEST
    VALUES (4566,'00004566')
    
    SELECT * FROM TEST WHERE COLUMN_1 = COLUMN_2
    

    The second column , it converts to int, and the zeros at the left dissapear, Because zeros at the left in a number has not value (as you know).

    Change the second column type to type varchar or char.