On 6/5/06, <b class="gmail_sendername">William O'Higgins Witteman</b> <<a href="mailto:william.ohiggins-H217xnMUJC0sA/PxXw9srA@public.gmane.org">william.ohiggins-H217xnMUJC0sA/PxXw9srA@public.gmane.org</a>> wrote:<div><span class="gmail_quote"></span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I have a database that needs updating, and I just wanted to see if<br>anyone has done something like this before.<br><br>Here's the problem - I have a constant string in one column that needs<br>to be updated to a different string, for all record.
<br><br>In pseudocode, what I want to do is this:<br> foreach row in table<br> in column A, swap "something" with "somethingelse" leaving the rest<br> of the column's content alone<br><br>I think that this could be done with a single, fairly simple command,
<br>but I'd hate to screw up the whole database by getting it wrong.<br><br>Anyone have any suggestions? Thanks.</blockquote><div><br>To simply update a column for an entire table,<br><br> UPDATE table SET column = someTransformation(column);
<br><br>Since there's no WHERE clause, this operates over the entire table.<br><br>If the transformation is more complicated, you may need to write a short Perl script to do the same thing .. the advantage to that is you can do the transformation and check that it works first, without actually updating the database.
<br><br><br>Now in terms of being able to go back in case you got it wrong, there are a number of options.<br><br>1. Backup the table first, and restore it if things go wrong;<br>2. Create another column, and copy the original column to the new column, and mess with the new column;
<br>3. In Postgres, I would bracket my attempts with BEGIN WORK and COMMIT (if it worked) or ROLLBACK (if not). I'm not as aware of whether that's offered in MySQL yet, and which version of MySQL you have.<br><br><br>Finally, make sure your test ("Did it work?') is ironclad -- you don't want to make the change then discover it's wrong for male students born in Saskatchewan. Or something. Good luck.
<br><br></div></div>-- <br>Alex Beamish<br>Toronto, Ontario<br>
<br>