Header Ads Widget

SQL Update Statement

SQL Update Statement

The SQL UPDATE statement is used to modify the data that is already in the database. The condition in the WHERE clause decides that which row is to be updated.

Syntax

  1. UPDATE table_name  
  2. SET column1 = value1column2 = value2, ...  
  3. WHERE condition;  

Sample Table

EMPLOYEE

EMP_IDEMP_NAMECITYSALARYAGE
1AngelinaChicago20000030
2RobertAustin30000026
3ChristianDenver10000042
4KristenWashington50000029
5RussellLos angels20000036
6MarryCanada60000048

Updating single record

Update the column EMP_NAME and set the value to 'Emma' in the row where SALARY is 500000.

Syntax

  1. UPDATE table_name    
  2. SET column_name = value   
  3. WHERE condition;   

Query

  1. UPDATE EMPLOYEE   
  2. SET EMP_NAME = 'Emma'   
  3. WHERE SALARY = 500000;  

Output: After executing this query, the EMPLOYEE table will look like:

EMP_IDEMP_NAMECITYSALARYAGE
1AngelinaChicago20000030
2RobertAustin30000026
3ChristianDenver10000042
4EmmaWashington50000029
5RussellLos angels20000036
6MarryCanada60000048

Updating multiple records

If you want to update multiple columns, you should separate each field assigned with a comma. In the EMPLOYEE table, update the column EMP_NAME to 'Kevin' and CITY to 'Boston' where EMP_ID is 5.

Syntax

  1. UPDATE table_name    
  2. SET column_name = value1column_name2 = value2    
  3. WHERE condition;  

Query

  1. UPDATE EMPLOYEE   
  2. SET EMP_NAME = 'Kevin'City = 'Boston'   
  3. WHERE EMP_ID = 5;  

Output

EMP_IDEMP_NAMECITYSALARYAGE
1AngelinaChicago20000030
2RobertAustin30000026
3ChristianDenver10000042
4KristenWashington50000029
5KevinBoston20000036
6MarryCanada60000048

Without use of WHERE clause

If you want to update all row from a table, then you don't need to use the WHERE clause. In the EMPLOYEE table, update the column EMP_NAME as 'Harry'.

Syntax

  1. UPDATE table_name    
  2. SET column_name = value1;  

Query

  1. UPDATE EMPLOYEE   
  2. SET EMP_NAME = 'Harry';  

Output

EMP_IDEMP_NAMECITYSALARYAGE
1HarryChicago20000030
2HarryAustin30000026
3HarryDenver10000042
4HarryWashington50000029
5HarryLos angels20000036
6HarryCanada60000048

Post a Comment

0 Comments