Let's take an example of a real-world problem. These days, Facebook provides an option for Editing your status update, how do you think it works? Yes, using the Update SQL command.
Let's learn about the syntax and usage of the
UPDATE command is used to update any record of data in a table. Following is its general syntax,
UPDATE table_name SET column_name = new_value WHERE some_condition;
WHEREis used to add a condition to any SQL query, we will soon study about it in detail.
Lets take a sample table student,
UPDATE student SET age=18 WHERE student_id=102;
In the above statement, if we do not use the
WHERE clause, then our update query will update age for all the columns of the table to 18.
We can also update values of multiple columns using a single
UPDATE student SET name='Abhi', age=17 where s_id=103;
The above command will update two columns of the record which has
UPDATECommand: Incrementing Integer Value
When we have to update any integer value in a table, then we can fetch and update the value in the table in a single statement.
For example, if we have to update the
age column of student table every year for every student, then we can simply run the following
UPDATE statement to perform the following operation:
UPDATE student SET age = age+1;
As you can see, we have used
age = age + 1 to increment the value of age by 1.
NOTE: This style only works for integer values.