Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm looking to do MySQL dumps on a nightly basis. I already have a solution for outputting just a diff of the data, but what I need is a way to import the data back into MySQL so the data in MySQL is UPDATED, instead of INSERTED if it's already there. For example, here's a sample table:

Id | User | Account_Value | Created_At | Updated_At
1  | Bob  | $100          | 2009-01-01 | 2009-01-01
2  | Ed   | $200          | 2009-01-01 | 2009-01-01

Then the next day, the table looks like this:

Id | User | Account_Value | Created_At | Updated_At
1  | Bob  | $50           | 2009-01-01 | 2009-01-02
2  | Ed   | $200          | 2009-01-01 | 2009-01-01

When I do the data export on the second day, it will correctly output something like:

INSERT into ACCOUNTS VALUES (1,'Bob', '$50', '2009-01-01', '2009-01-02')

However, this will conflict with what's already there. What I want is to have it overwrite what's already there. Thoughts?

share|improve this question
up vote 3 down vote accepted

You can add the "on duplicate key update" clause to the insert statements:

share|improve this answer
Sigh. That was easy. Well done. – aronchick Mar 1 '10 at 21:23

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.