eHow launches Android app: Get the best of eHow on the go.

How To

How to Perform an UPDATE MySQL Query

Contributor
By eHow Contributing Writer
(2 Ratings)

With a MySQL database, the UPDATE command is used to change some columns of a row already in the database. Usually, this is done to keep information in a database current.

Difficulty: Moderate
Instructions
  1. Step 1

    Use the INSERT command to input data. To perform an update MySQL query, it's imperative to have a row in the database to change, preferably with a unique ID referring to that row.

  2. Step 2

    Make sure that every ID in the table is unique by using an INTEGER AUTO_INCREMENT column. UPDATE commands can update the wrong row if a unique ID isn't used. When using an ID field, that field isn't included in the INSERT command, as the AUTO_INCREMENT attribute takes care of that field for you. Example:
    INSERT INTO customers name,ph_number,balance VALUES ("John Doe","555-5555",0.0);

  3. Step 3

    Find the data again using a SELECT query. Here, to update the balance, you first have to pull the old balance out of the table. Since all you are interested in is the balance and the ID number, those are the only two columns you need to pull out. Later, the application code using these queries will add to or subtract from the balance. Example:
    SELECT id,balance FROM customers WHERE name='John Doe' AND ph_number="555-5555";

  4. Step 4

    Update the data. Perform the UPDATE MySQL Query using the UPDATE command. Now that you have the ID and the updated data, you have all the information you need to proceed. UPDATE looks like a combination between INSERT and SELECT. This example supposes the balance and ID are something you got from the previous query. Example:
    UPDATE customers SET balance=12.34 WHERE id=22;

  5. Step 5

    Avoid making mistakes in the WHERE clause, since you can inadvertently update more than one row. For example, if you used this query instead of the previous one, it would accidentally update the balance of every customer named John Doe. Example:
    UPDATE customers SET balance=12.34 WHERE name="John Doe";

Tips & Warnings
  • It's very useful to have a unique ID to refer to each row. Using a unique ID makes it easier to find data with a SELECT query when you want to update it later in the life of the database.
  • The ID field needs to be saved from the previous query to use again so you are absolutely sure the row you're updating is the one you intended.
Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Get Free Internet Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy .   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License. † requires javascript

Demand Media
eHow_eHow Technology and Electronics