SQL UPDATE command

SQL UPDATE command is used to update existing rows in the database.
The command structure is the following:
UPDATE <tableName> SET <columnName>=<newValue> WHERE <condition>;
If we don’t put WHERE condition, then it will update all rows, so we should be very carefully to put the WHERE condition.

For example if we have the following table:


MariaDB [firstDB]> SELECT * FROM employee;
+------------+-----------+-----------------+--------+
| first_name | last_name | employee_number | salary |
+------------+-----------+-----------------+--------+
| Julia      | Roberts   |               1 |  50000 |
| Edison     | Ford      |               2 |  45000 |
| Irina      | Shayk     |               3 |  60000 |
| Yigal      | Arnon     |               4 |  29000 |
+------------+-----------+-----------------+--------+
4 rows in set (0.00 sec)

And we want to update Yigal Arnon’s salary to 35000:


MariaDB [firstDB]> UPDATE employee SET salary=35000 WHERE first_name="Yigal" AND last_name="Arnon";
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [firstDB]> SELECT * FROM employee;
+------------+-----------+-----------------+--------+
| first_name | last_name | employee_number | salary |
+------------+-----------+-----------------+--------+
| Julia      | Roberts   |               1 |  50000 |
| Edison     | Ford      |               2 |  45000 |
| Irina      | Shayk     |               3 |  60000 |
| Yigal      | Arnon     |               4 |  35000 |
+------------+-----------+-----------------+--------+
4 rows in set (0.00 sec)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s