DEV Community

nadirbasalamah
nadirbasalamah

Posted on

MySQL tutorial - 5 Updating the data

In MySQL, data inside the table can be updated using UPDATE query. When updating the data, the data type must match with the data type that already specified in the column.

This is the basic query structure of UPDATE query. Notice that the WHERE clause is used to identify which data needs to be updated. If WHERE clause is not used, all of the data inside the table will be updated.

UPDATE table_name SET column_name(s) = value WHERE condition
Enter fullscreen mode Exit fullscreen mode

This is the example of updating a data inside shop table with id equals 1.

UPDATE shop SET price = 12.5 WHERE id = 1;
Enter fullscreen mode Exit fullscreen mode

If the update operation is completed, there is a number of rows affected inside the table.

The data update operation can be done for many columns. this is the example of updating a quantity and product's name in a data that has an id equals 2;

UPDATE shop SET quantity = 15, product_name = "Low Fat Milk" WHERE id = 2;
Enter fullscreen mode Exit fullscreen mode

I hope this article is helpful for learning SQL, If you have any thoughts or comments you can write in the discussion section below.

Top comments (0)