2.SQL UPDATE Statement

 

The UPDATE statement is used to modify the existing records in a table.

UPDATE Syntax

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

Demo Database

Below is a selection from the "Customers" table in the Northwind sample database:

CustomerIDCustomerName

CityPostalCodeCountry
1

Jamdhade Tushar

Pune12209India
2Jamdhade Atharv

Newasa05021India
3Jamdhade Gaurav

MP05023India
4

Jamdhade Sagar

UPWA1 1DPIndia
5Jamdhade Tush

NewasaS-958 22India

UPDATE Table

The following SQL statement updates the first customer (CustomerID = 1) with a new contact person and a new city.

Example

UPDATE Customers
SET CustomerName = 'Tush ', City= 'Newasa'
WHERE CustomerID = 1;

The selection from the "Customers" table will now look like this:

CustomerIDCustomerName

CityPostalCodeCountry
1

Tush

Newasa12209India
2Jamdhade Atharv

Newasa05021India
3Jamdhade Gaurav

MP05023India
4

Jamdhade Sagar

UPWA1 1DPIndia
5Jamdhade Tush

NewasaS-958 22India



UPDATE Multiple Records

It is the WHERE clause that determines how many records will be updated.

The following SQL statement will update the ContactName to "Juan" for all records where country is "Mexico":

Example

UPDATE Customers
SET ContactName='Juan'
WHERE Country='Mexico';





Post a Comment

Previous Post Next Post