Inserting into a mysql table and overwritng any current data

前端 未结 5 926
醉话见心
醉话见心 2020-12-16 19:48

I am inserting some data into a table, but it occasionally clashes with other data in the table (ie. it has the same primary key).
I would like to be able to just over

相关标签:
5条回答
  • 2020-12-16 19:59

    You can use REPLACE INTO in MySQL to do this.

    REPLACE INTO table
    SET name = 'Treffynnon'
    
    0 讨论(0)
  • 2020-12-16 20:10

    Just a little cheatsheet.
    Mysql has 3 different scenarios for handling unique key duplicates:

    If you want to...

    • do nothing - use INSERT IGNORE
    • delete existing and create new - use REPLACE INTO
    • update existing - use ON DUPLICATE UPDATE
    0 讨论(0)
  • 2020-12-16 20:13

    You can use replace statement instead of insert. Look at http://dev.mysql.com/doc/refman/5.1/en/replace.html

    0 讨论(0)
  • 2020-12-16 20:14

    Look up "on duplicate key update".

    http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

    0 讨论(0)
  • 2020-12-16 20:18

    MySQL has a "INSERT ... ON DUPLICATE KEY UPDATE" command. You can find it here: http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

    INSERT INTO `table` VALUES ('a', 'b') ON DUPLICATE KEY UPDATE `field1`='a', `field2`='b'
    
    0 讨论(0)
提交回复
热议问题