Help with this error please

Discussion in 'Databases' started by JEET, Sep 6, 2008.

  1. #1
    Hi,
    Can someone please tell me how to fix this error below:
    This comes up when inserting a record.
    Duplicate entry '0-1' for key 2

    query is this:
    INSERT INTO topics ( ID_TOPIC , isSticky , ID_BOARD, ID_FIRST_MSG , ID_LAST_MSG, ID_MEMBER_STARTED , ID_MEMBER_UPDATED , ID_POLL , numReplies , numViews , locked ) VALUES
    ('', 0, '1', '7',0, '15', 0, 0, 0, '551', 0)

    Table structure is this:

    ID_TOPIC mediumint(8) unsigned NOT NULL auto_increment,
    isSticky tinyint(4) NOT NULL default '0',
    ID_BOARD smallint(5) unsigned NOT NULL default '0',
    ID_FIRST_MSG int(10) unsigned NOT NULL default '0',
    ID_LAST_MSG int(10) unsigned NOT NULL default '0',
    ID_MEMBER_STARTED mediumint(8) unsigned NOT NULL default '0',
    ID_MEMBER_UPDATED mediumint(8) unsigned NOT NULL default '0',
    ID_POLL mediumint(8) unsigned NOT NULL default '0',
    numReplies int(10) unsigned NOT NULL default '0',
    numViews int(10) unsigned NOT NULL default '0',
    locked tinyint(4) NOT NULL default '0',
    PRIMARY KEY ( ID_TOPIC ),
    UNIQUE KEY lastMessage ( ID_LAST_MSG , ID_BOARD ),
    UNIQUE KEY firstMessage ( ID_FIRST_MSG , ID_BOARD ),
    UNIQUE KEY poll ( ID_POLL , ID_TOPIC ),
    KEY isSticky ( isSticky ),
    KEY ID_BOARD ( ID_BOARD )
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;



    Which values I need to change in above query so it works?
    Thanks :)
     
    JEET, Sep 6, 2008 IP
  2. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #2
    Value for ID_BOARD. You have already a record in the table with 0 isSticky and 1 ID_BOARD.
     
    mwasif, Sep 6, 2008 IP
  3. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #3

    Below is the backup of this table. Both records have "0" for sticky, and "1" for ID_BOARD. If the error is with these 2 fields, then why is the second record inserted properly?

    INSERT INTO topics ( ID_TOPIC , isSticky , ID_BOARD , ID_FIRST_MSG , ID_LAST_MSG , ID_MEMBER_STARTED , ID_MEMBER_UPDATED , ID_POLL , numReplies , numViews , locked ) VALUES
    (1, 0, 1, 1, 2, 0, 1, 0, 1, 1, 0),
    (2, 0, 1, 3, 3, 1, 1, 0, 0, 0, 0);

    I think it's some other field, but not sure which one...
    regards :)
     
    JEET, Sep 6, 2008 IP
  4. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #4
    Ok, This is solved. The problem was in ID_FIRST_MSG , ID_LAST_MSG fields.
    Value for those records are 0,0 and new records I was adding were also 0,0 . Changed it to -1,-1 and updated later.
    Thanks :)
     
    JEET, Sep 6, 2008 IP