Change One Cell's Data in MySQL?


Update only one cell’s data with the help of UPDATE command. The syntax is as follows −

UPDATE yourTableName yourColumnName=yourNewValue where
yourColumnName=yourOldValue;

To understand the above concept, let us first create a table. The query to create a table is as follows −

mysql> create table changeCellsData
   -> (
   -> Id int,
   -> Name varchar(100),
   -> Age int
   -> );
Query OK, 0 rows affected (0.81 sec)

Insert some records in the table using insert command. The query is as follows −

mysql> insert into changeCellsData values(101,'Mike',23);
Query OK, 1 row affected (0.12 sec)

mysql> insert into changeCellsData values(103,'Bob',25);
Query OK, 1 row affected (0.18 sec)

mysql> insert into changeCellsData values(105,'Sam',27);
Query OK, 1 row affected (0.12 sec)

mysql> insert into changeCellsData values(106,'Carol',21);
Query OK, 1 row affected (0.24 sec)

Display all records from the table using select statement. The query is as follows −

mysql> select *from changeCellsData;

Output

+------+-------+------+
| Id   | Name  | Age  |
+------+-------+------+
| 101 | Mike   | 23   |
| 103 | Bob    | 25   |
| 105 | Sam    | 27   |
| 106 | Carol  | 21   |
+------+-------+------+
4 rows in set (0.00 sec)

Here is the query to change cell data using update and set command. We are updating the last record of column “Id”.

The query is as follows −

mysql> update changeCellsData set Id=107 where Id=106;
Query OK, 1 row affected (0.19 sec)
Rows matched: 1 Changed: 1 Warnings: 0

Now you can check the cell data has been changed or not using select command. The query is as follows −

mysql> select *from changeCellsData;

Output

+------+-------+------+
| Id   | Name  | Age  |
+------+-------+------+
| 101  | Mike  |   23 |
| 103  | Bob   |   25 |
| 105  | Sam   |   27 |
| 107  | Carol |   21 |
+------+-------+------+
4 rows in set (0.00 sec)

Updated on: 30-Jul-2019

325 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements