navigation

UPDATE


The SQL UPDATE Statement is used to modify the existing rows in a table.
Syntax:
Basic syntax of UPDATE statement is as follows:

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

You can combine N number of conditions.
      table_name -  the table name which has to be updated.
column_name1, column_name2.. - the columns that gets changed.
 value1, value2... - are the new values.

NP: You can use WHERE clause with UPDATE query to update selected rows. If you do not include the WHERE clause, column values for all the rows get affected.

EXAMPLE:
Below is the "EMPLOYEE1" table:

ID
NAME
START_DATE
END_DATE
SALARY
CITY
1
A
01/01/2016 00:00:00
01/06/2016 00:00:00
10,000
CHENNAI
2
B
02/01/2016 00:00:00
07/03/2016 00:00:00
10,000
BOMBAY
3
C
03/01/2016 00:00:00
08/06/2016 00:00:00
10,000
DELHI
4
D
01/01/2016 00:00:00
07/06/2016 00:00:00
10,000
PUNE
5
E
01/01/2016 00:00:00
09/06/2016 00:00:00
10,000
NOIDA
6
F
01/01/2016 00:00:00
08/06/2016 00:00:00
10,000
BHUBANESWAR
7
G
01/01/2016 00:00:00
07/06/2016 00:00:00
10,000
HYDERABAD
8
H
01/01/2016 00:00:00
06/06/2016 00:00:00
10,000
KOLKATA

1)To update the name and city of an employee in EMPLOYEE1 table whose ID = 8, the SQL update query would be like,

UPDATE EMPLOYEE1
SET NAME = ‘Z’, CITY = ‘NOIDA’
WHERE ID = 8;

ID
NAME
START_DATE
END_DATE
SALARY
CITY
1
A
01/01/2016 00:00:00
01/06/2016 00:00:00
10,000
CHENNAI
2
B
02/01/2016 00:00:00
07/03/2016 00:00:00
10,000
BOMBAY
3
C
03/01/2016 00:00:00
08/06/2016 00:00:00
10,000
DELHI
4
D
01/01/2016 00:00:00
07/06/2016 00:00:00
10,000
PUNE
5
E
01/01/2016 00:00:00
09/06/2016 00:00:00
10,000
NOIDA
6
F
01/01/2016 00:00:00
08/06/2016 00:00:00
10,000
BHUBANESWAR
7
G
01/01/2016 00:00:00
07/06/2016 00:00:00
10,000
HYDERABAD
8
Z
01/01/2016 00:00:00
06/06/2016 00:00:00
10,000
NOIDA

2)To update the city of all the employees, the query would be,
UPDATE EMPLOYEE1
SET CITY = 'Pune';

ID
NAME
START_DATE
END_DATE
SALARY
CITY
1
A
01/01/2016 00:00:00
01/06/2016 00:00:00
10,000
Pune
2
B
02/01/2016 00:00:00
07/03/2016 00:00:00
10,000
Pune
3
C
03/01/2016 00:00:00
08/06/2016 00:00:00
10,000
Pune
4
D
01/01/2016 00:00:00
07/06/2016 00:00:00
10,000
Pune
5
E
01/01/2016 00:00:00
09/06/2016 00:00:00
10,000
Pune
6
F
01/01/2016 00:00:00
08/06/2016 00:00:00
10,000
Pune
7
G
01/01/2016 00:00:00
07/06/2016 00:00:00
10,000
Pune
8
Z
01/01/2016 00:00:00
06/06/2016 00:00:00
10,000
Pune




No comments:

Post a Comment