UPDATE SQL command

The Update command can be used to update any data record in a table. This can be done with the syntax shown below;

UPDATE table_name SET column_name = new_value WHERE some_condition;

In the syntax above, ‘where’ is used to add a condition to any SQL query, you will learn about it in detail in subsequent tutorials.

 

Take a look at this sample table  of ‘student’;

student_idnameage
11Alexa18
12Nick 
13Ads Chan 

 

UPDATE student SET age=21 WHERE student_id=11;

 

After the update, your table will look like this;

student_idnameage
11Alexa21
12Nick 
13Ads Chan 

 

Updating Multiple Columns

Values of multiple columns can also be updated by using a single ‘update’ statement.

UPDATE student SET name='Ada Chan', age=17 where s_id=13; 

The command above is used to update two columns of the record which has s_id 13.

 

UPDATE Command: Incrementing Integer Value

In this case, when you are required to update any integer value in a table, then you can fetch and update the value in the table in a single statement.

Take, for instance, if you want to update the age column of the ‘student’ table every year for every student, then you can simply run the following Update statement to perform the following operation:

UPDATE student SET age = age+1; 

From above, you can see that we’ve used age = age + 1 to increment the value of age by 1