105
T-SQL Update Statement
The T-SQL UPDATE statement is used to modify the existing records in the database.
We use the WHERE clause with the UPDATE query to update the particular rows. Otherwise, all rows would be affected.
Syntax of Update Statement:
Syntax of UPDATE query with WHERE clause is given below:
We combine N number of conditions by using the AND or OR operators.
Example:
See the EMPLOYEES table having the following records –
ID | Name | AGE | ADDRESS | Salary |
---|---|---|---|---|
001 | Rahul | 23 | Kota | 22000.00 |
002 | Clinton | 22 | Mumbai | 15000.00 |
003 | Kamal | 31 | Delhi | 25000.00 |
004 | Chitra | 28 | Kanyakumari | 65000.00 |
005 | Santanu | 26 | Madhya Pradesh | 38500.00 |
006 | Savitri | 24 | Bhopal | 4500.00 |
007 | Manii | 30 | Indonesia | 15000.00 |
The below command is an example, which would update ADDRESS for a customer whose ID is 6 ?
EMPLOYEES table will now have the following records ?
ID | Name | AGE | ADDRESS | Salary |
---|---|---|---|---|
001 | Rahul | 23 | Kota | 22000.00 |
002 | Clinton | 22 | Mumbai | 15000.00 |
003 | Kamal | 31 | Delhi | 25000.00 |
004 | Chitra | 28 | Kanyakumari65000.00 | |
005 | Santanu | 26 | Madhya Pradesh | 38500.00 |
006 | Savitri | 24 | Pune | 4500.00 |
007 | Manii | 30 | Indonesia | 15000.00 |
If we want to modify all ADDRESS and SALARY columns in the EMPLOYEES table, we have to use the WHERE clause. UPDATE query is given as follows –
EMPLOYEES table will have the below records.
ID | Name | AGE | ADDRESS | Salary |
---|---|---|---|---|
001 | Rahul | 23 | Kota | 22000.00 |
002 | Clinton | 22 | Mumbai | 15000.00 |
003 | Kamal | 31 | Delhi | 25000.00 |
004 | Chitra | 28 | Kanyakumari | 65000.00 |
005 | Santanu | 26 | Madhya Pradesh | 38500.00 |
006 | Savitri | 24 | Bhopal | 4500.00 |
007 | Manii | 30 | Goa | 15000.00 |
Next TopicDELETE Query