Signup/Sign In

Using UPDATE SQL command

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.


UPDATE command

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;

WHERE is used to add a condition to any SQL query, we will soon study about it in detail.

Lets take a sample table student,

student_idnameage
101Adam15
102Alex
103chris14
UPDATE student SET age=18 WHERE student_id=102;
S_idS_Nameage
101Adam15
102Alex18
103chris14

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.


Updating Multiple Columns

We can also update values of multiple columns using a single UPDATE statement.

UPDATE student SET name='Abhi', age=17 where s_id=103; 

The above command will update two columns of the record which has s_id 103.

s_idnameage
101Adam15
102Alex18
103Abhi17

UPDATE Command: 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.