SQL Update: How To Update Multiple Rows or Columns in SQL
We use sql update operation while we want to update a table record(s). Let’s say you have a Table which named as Employee. This Employee table contains following data;
Now if you want to update the Email Address column for an Employee which EmployeeID is 1 to something like firstname.lastname@example.org, then you have to execute the following sql command:
UPDATE Employee SET EmailAddress = ‘email@example.com’ WHERE EmployeeID = 1
This update sql command will update a single row for this Employee Table.
SQL Update Multiple Columns:
Let’s say you wants to update multiple column value like you wants to change the Last Name,Salary and Email Address for Goe Robert. Then you have to execute the below sql update multiple columns query:
UPDATE Employee SET LastName = ‘Peter’, EmailAddress = 'firstname.lastname@example.org', Salary=54000 WHERE EmployeeID = 3
You will notice here that, i have used single quotation for string type value like Last Name or Email Address. But when we have used number type or numeric value, we don’t need to use the single quotation.
SQL Update Multiple Rows:
Now if you want to update all the employee salary to 60000 at once by running a single sql update commands, then you need to execute this below sql update multiple rows commands:
UPDATE Employee SET Salary = 60000
You can see here that, we don’t used any where clause because we want sql command to apply for each row of your employee table. Which means, if you wants to apply any update which need to apply for each row of that table, then you don’t need to use the WHERE part.
Note: Be careful when you use any update sql commands in production or live database because, one mistake can change the data of that table.
I hope, now you have a clear understanding of sql update commands. Let me know your opinion in the comment section.