EN
MySQL - UPDATE statement
3 points
In this article, we would like to show you how to use UPDATE
statement in MySQL.
Quick solution:
xxxxxxxxxx
1
UPDATE `table_name`
2
SET `column1` = value1, `column2` = value2, ...
3
WHERE condition;
Warning:
If you omit or construct wrong way the
WHERE
clause, all records will be updated!
To show how the UPDATE
statement works, we will use the following table:

Note:
At the end of this article you can find database preparation SQL queries.
In this example, we will update information about moderator Chris.
Query:
xxxxxxxxxx
1
UPDATE `users`
2
SET `name` = 'Christopher', `role` = 'admin'
3
WHERE `id` = 2;
Output:

Use below queries to prepare database and test UPDATE
statement.
create_tables.sql
file:
xxxxxxxxxx
1
CREATE TABLE `users` (
2
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
3
`name` VARCHAR(100) NOT NULL,
4
`role` VARCHAR(15) NOT NULL,
5
PRIMARY KEY (`id`)
6
)
7
ENGINE=InnoDB;
insert_data.sql
file:
xxxxxxxxxx
1
INSERT INTO `users`
2
(`name`, `role`)
3
VALUES
4
('John', 'admin'),
5
('Chris', 'moderator'),
6
('Kate', 'user'),
7
('Denis', 'moderator');