SQL UPDATE

The UPDATE statement is used to update the records in a table.

It updates the data records based on the condition.

 

The syntax for UPDATE statement is written as

UPDATE table_name

SET column_name=expression

WHERE condition;

 

Example:

Let us take an example

Student Table:

StudentIDStudent_NameAddressCity
1NithinBank StreetHyderabad
2BharathWhite FieldBangalore
3GouthamAvenue colonyHyderabad

 

UPDATE Student

SET City=’Chennai’

WHERE Student_id=’3’;

 

Now the result after updating the value will be as follows:

StudentIDStudent_NameAddressCity
1NithinBank StreetHyderabad
2BharathWhite FieldBangalore
3GouthamAvenue colonyChennai

 

Updating multiple fields:

If you want to update more than one field at once then you should separate each field expression with a comma.

Syntax will be as follows

UPDATE table_name

SET column1=expressoin1, column2=expression2

WHERE condition;

 

Example

Let us consider a Student Table

Student Table:

StudentIDStudent_NameAddressCity
1NithinBank StreetHyderabad
2BharathWhite FieldBangalore
3GouthamAvenue colonyHyderabad

 

UPDATE Student

SET City=’Chennai’, Address=’Court road’

WHERE Student_id=’3’;

 

Now the result after updating the value will be as follows:

StudentIDStudent_NameAddressCity
1NithinBank StreetHyderabad
2BharathWhite FieldBangalore
3GouthamAvenue colonyChennai

 

While using UPDATE statement condition is compulsory as it would change every data in the table.