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:

StudentID Student_Name Address City
1 Nithin Bank Street Hyderabad
2 Bharath White Field Bangalore
3 Goutham Avenue colony Hyderabad

 

UPDATE Student

SET City=’Chennai’

WHERE Student_id=’3’;

 

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

StudentID Student_Name Address City
1 Nithin Bank Street Hyderabad
2 Bharath White Field Bangalore
3 Goutham Avenue colony Chennai

 

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:

StudentID Student_Name Address City
1 Nithin Bank Street Hyderabad
2 Bharath White Field Bangalore
3 Goutham Avenue colony Hyderabad

 

UPDATE Student

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

WHERE Student_id=’3’;

 

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

StudentID Student_Name Address City
1 Nithin Bank Street Hyderabad
2 Bharath White Field Bangalore
3 Goutham Avenue colony Chennai

 

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