UPDATE statement in MySQL
Hello and
welcome everyone. In this article, we will learn about the UPDATE statement in
MySQL. Let’s go.
MongoDB with .Net Core course link => https://www.udemy.com/mongodb-with-net-core-sagar-jaybhay/
C# basic course click on: https://www.udemy.com/learn-csharp-with-sagar-jaybhay/
Web API 2- .Net Core In depth In 5 Hours click
on: https://www.udemy.com/web-api-2-net-core-in-depth-in-5-hrs-with-sagar-jaybhay/
The MySQL
UPDATE statement is used to modify the values of the existing row in a table.
You can also update a single row or multiple rows at the same time using UPDATE
command. UPDATE command also used with WHERE clause. When using WHERE clause
you can update a specific record that you want to update.
Following
is the UPDATE statement syntax that provided MySQL:
UPDATE table_name
SET column_name_1 = values_1,
column_name_2 = values_1, …
WHERE condition;
Here is
UPDATE –
this command updates the values or data or records in the table.
table_name
– this is table name which table data you want to update.
SET – it
specifies which columns record you want to update with the new values. For
update, many rows record you can use a comma separated.
column_name_1
and column_name_2 – this column name is which column records you want to
update.
values_1
and values_2 – this one is you can set new values for an update on previous
values.
WHERE –
this WHERE clause using specific record will update.
condition
– specify the condition for the update.
For
Example –
Step 1:
Here is
the, we will create a table for an employee in the database.
CREATE
TABLE IF NOT EXISTS employee (
e_id
int (10) PRIMARY KEY,
e_firstname
varchar (50) NOT NULL,
e_lastname
varchar (50) NOT NULL,
e_age
int (10),
e_city
varchar (250)
e_sex
char (1)
e_phnumber
int (15) NOT NULL
)
Step 2:
Here we
insert record in the employee table.
INSERT INTO employee (
‘e_id’, ’e_firstname’, ’e_lastname’,
’e_age’, ’e_city’, ’e_sex’,
’e_phnumber’
)
VALUES
(
1, ‘Hone’, ‘Reus’, 25, ‘San Jose’, ‘M’, ‘ +
1 555 555 1111’
),
(
2, ‘Cries’, ’Fence’, 23, ’Fresno’, ’F’, ’
+1 555 555 1111’
);
Step 3:
Now we
want to update the second record from the employee table.
Here is
the second row
‘e_id’ ’e_firstname’ ’e_lastname’ ’e_age’ ’e_city’ ’e_sex’
’e_phnumber’
2 Cries Fence 23
Fresno F +1 555 555 1111
So we
want to update employee city ‘Fresno’ to update city ‘Oakland’ and also update
employee age ’23’ to ‘26’ from the above records.
UPDATE employee
SET e_age = 26, e_city = ‘Oakland’
WHERE e_id = 2;
Then it
will update record.
‘e_id’ ’e_firstname’ ’e_lastname’ ’e_age’ ’e_city’ ’e_sex’ ’e_phnumber’
2 Cries Fence 26 Oakland F +1 555 555 1111
For
another example without using WHERE clause:
‘e_id’ ’e_firstname’ ’e_lastname’ ’e_age’ ’e_city’ ’e_sex’ ’e_phnumber’
1 Hone Reus 25 San Jose
M +1 555 555 1111
2 Cries Fence 23 Fresno F +1 555 555 1111
Here we want to update city
for all employee is Oakland then you can write the following query:
UPDATE employee
SET e_city = ‘Oakland’;
Then the entire employee’s
city will update to ‘Oakland’
‘e_id’ ’e_firstname’ ’e_lastname’ ’e_age’ ’e_city’ ’e_sex’ ’e_phnumber’
1 Hone Reus 25 Oakland M +1 555 555 1111
2 Cries Fence 23 Oakland F +1 555 555 1111
In this
article, we are learned about the UPDATE statement. You can update one or more
records at a time. Using WHERE clause you can specify condition.
MongoDB with .Net Core course link => https://www.udemy.com/mongodb-with-net-core-sagar-jaybhay/
C# basic course click on: https://www.udemy.com/learn-csharp-with-sagar-jaybhay/
Web API 2- .Net Core In depth In 5 Hours click
on: https://www.udemy.com/web-api-2-net-core-in-depth-in-5-hrs-with-sagar-jaybhay/
Comments
Post a Comment