The UPDATE command is used in MySQL to update records. With it, the SET command is used to set new values. Let us first create a table −
mysql> create table DemoTable ( EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY, EmployeeName varchar(50), EmployeeSalary int ); Query OK, 0 rows affected (0.57 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(EmployeeName,EmployeeSalary) values('Chris',56780); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(EmployeeName,EmployeeSalary) values('Robert',45670); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(EmployeeName,EmployeeSalary) values('Mike',87654); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(EmployeeName,EmployeeSalary) values('David',34569); Query OK, 1 row affected (0.11 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+------------+--------------+----------------+ | EmployeeId | EmployeeName | EmployeeSalary | +------------+--------------+----------------+ | 1 | Chris | 56780 | | 2 | Robert | 45670 | | 3 | Mike | 87654 | | 4 | David | 34569 | +------------+--------------+----------------+ 4 rows in set (0.00 sec)
Following is the query to update records and set new values −
mysql> update DemoTable set EmployeeSalary=EmployeeSalary+12346; Query OK, 4 rows affected (0.14 sec) Rows matched: 4 Changed: 4 Warnings: 0
Let us check the table records −
mysql> select *from DemoTable;
This will produce the following output −
+------------+--------------+----------------+ | EmployeeId | EmployeeName | EmployeeSalary | +------------+--------------+----------------+ | 1 | Chris | 69126 | | 2 | Robert | 58016 | | 3 | Mike | 100000 | | 4 | David | 46915 | +------------+--------------+----------------+ 4 rows in set (0.00 sec)