check for duplicate data before insert

后端 未结 3 1741
悲&欢浪女
悲&欢浪女 2020-12-22 12:29

I\'m creating a java swing program where users can create a record, but I want to be able to check for duplicates before allowing the users to create the record. Here\'s my

3条回答
  •  醉酒成梦
    2020-12-22 12:51

    If you want DutyName and volNric to have unique values, then do so with a unique constraint/index:

    create index idx_assignrequests_dutyname_volnric on assignrequests(dutyname, volnric);
    

    Then, when you do the insert, you can let it fail. Or, you can just ignore it using on duplicate key update:

    INSERT into assignrequests(reqId, dutyName, volNric)"
        VALUES ('" + id + "','" + dutyName + "','" + volNric + "')
        ON DUPLICATE KEY UPDATE dutyName = VALUES(dutyName);
    

    The column being updated is being set to itself -- so the operation doesn't do anything.

提交回复
热议问题