4

I am using mysqldump to dump some records in a table:

mysqldump mydatabase mytable --where="field=value"

this emits script that includes drop table mytable statement.

Since I use the --where option, when I replay the script, I want to overwrite only these records that satisfy the condition field=value. I want the emitted script to delete just these records:

delete from `mytable` where field=value

is there a way to achieve this?

1 Answer 1

4

Specify to not use any creation SQL with --no-create-info

mysqldump mydatabase mytable --no-create-info --where="field=value"

If you want to overwrite the records on reload of the mysqldump, just add --replace

mysqldump mydatabase mytable --no-create-info --replace --where="field=value"

I suggested --replace because REPLACE is a mechanical DELETE and INSERT.

Give it a Try !!!

4
  • thanks. this removes drop/create table. but how do I add the delete statement?
    – akonsu
    Jul 22, 2013 at 19:21
  • Updated my answer... Jul 22, 2013 at 20:34
  • thanks! suppose after I create a dump, a few more records got added to the table. I need to delete all records which satisfy the condition, and put the ones from the dump. I appreciate your help. looks like I cannot do it with just command line switches.
    – akonsu
    Jul 22, 2013 at 21:06
  • probably of little use to the OP today, but for those also searching for an ability to quickly create the delete statements from an insert, this Vim command/function may be a good help: vi.stackexchange.com/q/16428/11715
    – ljs.dev
    Jun 20, 2018 at 1:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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