SQL UPDATE

This statement is  to modify/update the existing records in a table. You can use the WHERE clause with the UPDATE query to update the selected rows, otherwise all the rows would be affected.

 

Syntax:

UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;

 

Example Database:

This is  customers  table.

id name Location
1 Kailash Delhi
2 Amit Haryana
3 Siddarth Noida
4 Vikas Noida

 


 

UPDATE Table

The following query will update the location for a customers whose id number is 2 in the table.

Syntax:

UPDATE customers SET location = 'Gurgaon' WHERE id = 1;

 

Result:

id name Location
1 Kailash Gurgaon
2 Amit Haryana
3 Siddarth Noida
4 Vikas Noida

 

UPDATE Multiple Records

If you want to modify multiple column like- name and the location column values in the customers table, you need to use the WHERE clause as the UPDATE query would be enough as shown in the following code block.

Syntax:

UPDATE customers SET name='Hemant', location='Goa' WHERE id = 1;

 

Result:

id name Location
1 Hemant Goa
2 Amit Haryana
3 Siddarth Noida
4 Vikas Noida