How Update Table With Example

aimtocode

UPDATE statement:

  • The UPDATE statement updates data values in a database.
  • UPDATE can update one or more records in a table.
  • Use the WHERE clause to UPDATE only specific records.

Syntax:


UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];

Let's assume we are having a CUSTOMER table as shown below:

ID NAME AGE ADDRESS SALARY
01 Prayag 21 Ranchi 35000
02 Sujeet 20 Delhi 25000
03 Rakesh 23 Chennai 225000
04 Pankaj 24 Patna 30000

The following query will update the ADDRESS for a customer whose ID number is 01 in the table.


SQL> UPDATE CUSTOMERS
SET ADDRESS = 'Ranchi'
WHERE ID = 01;


ID NAME AGE ADDRESS SALARY
01 Prayag 21 Bangalore 35000
02 Sujeet 20 Delhi 25000
03 Rakesh 23 Chennai 225000
04 Pankaj 24 Patna 30000

If you want to modify all the ADDRESS and the SALARY column values in the CUSTOMERS table, you do not need to use the WHERE clause as the UPDATE query would be enough as shown in the following code block.


SQL> UPDATE CUSTOMERS
SET ADDRESS = 'Ranchi', SALARY = 1500;

ID NAME AGE ADDRESS SALARY
01 Prayag 21 Ranchi 1500
02 Sujeet 20 Ranchi 1500
03 Rakesh 23 Ranchi 1500
04 Pankaj 24 Ranchi 1500
aimtocode