SQL UPDATE Statement Examples
Let’s see the SQL update statement with examples.
First we will create a table, on which we will use the UPDATE Statement. To create the table, write the following query:
Query:
CREATE TABLE Customer(
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(50),
LastName VARCHAR(50),
Country VARCHAR(50),
Age int(2),
Phone int(10)
);
-- Insert some sample data into the Customers table
INSERT INTO Customer (CustomerID, CustomerName, LastName, Country, Age, Phone)
VALUES (1, 'Shubham', 'Thakur', 'India','23','xxxxxxxxxx'),
(2, 'Aman ', 'Chopra', 'Australia','21','xxxxxxxxxx'),
(3, 'Naveen', 'Tulasi', 'Sri lanka','24','xxxxxxxxxx'),
(4, 'Aditya', 'Arpan', 'Austria','21','xxxxxxxxxx'),
(5, 'Nishant. Salchichas S.A.', 'Jain', 'Spain','22','xxxxxxxxxx');
Select * from Customer;
The created table will look like this:
Update Single Column Using UPDATE Statement Example
Update the column NAME and set the value to ‘Nitin’ in the rows where the Age is 22.
Query:
UPDATE Customer SET CustomerName
= 'Nitin' WHERE Age = 22;
Output:
Updating Multiple Columns using UPDATE Statement Example
Update the columns NAME to ‘Satyam’ and Country to ‘USA’ where CustomerID is 1.
Query:
UPDATE Customer SET CustomerName = 'Satyam',
Country = 'USA' WHERE CustomerID = 1;
Output:
Note: For updating multiple columns we have used comma(,) to separate the names and values of two columns.
SQL UPDATE Statement
SQL UPDATE Statement modifies the existing data from the table.