Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

1 Answer 1

up vote 3 down vote accepted

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

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

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

Your Answer

 
discard

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.