Does UPDATE overwrite values if they are identical in MySQL


No, MySQL UPDATE won’t overwrite values if they are identical. Let us first create a table −

mysql> create table DemoTable
(
   StudentId int,
   StudentMathMarks int,
   StudentMySQLMarks int
);
Query OK, 0 rows affected (0.46 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(1,56,78);
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable values(2,88,99);
Query OK, 1 row affected (0.15 sec)
mysql> inse
rt into DemoTable values(3,34,98);
Query OK, 1 row affected (0.13 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-----------+------------------+-------------------+
| StudentId | StudentMathMarks | StudentMySQLMarks |
+-----------+------------------+-------------------+
|         1 |               56 |                78 |
|         2 |               88 |                99 |
|         3 |               34 |                98 |
+-----------+------------------+-------------------+
3 rows in set (0.00 sec)

Following is the query to update values. The values set are already present in that specific column i.e. student id 2, math marks 88 and SQL marks 99 for student id 2 −

mysql> update DemoTable set StudentId=2,StudentMathMarks=88,StudentMySQLMarks=99 where StudentId=2;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0

Look at the above UPDATE command, the output, 0 rows affected means UPDATE does not overwrite values if they are identical.

Updated on: 04-Oct-2019

394 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements