How to revert rows to default column value in MySQL?

MySQLMySQLi Database

To revert rows to default column value, let us first create a demo table

mysql> create table defaultDemo
   -> (
   -> Id int
   -> );
Query OK, 0 rows affected (0.48 sec)

Insert some records in the table using insert command.

The query is as follows

mysql> insert into defaultDemo values(10);
Query OK, 1 row affected (0.25 sec)
mysql> insert into defaultDemo values(20);
Query OK, 1 row affected (0.13 sec)
mysql> insert into defaultDemo values(30);
Query OK, 1 row affected (0.14 sec)
mysql> insert into defaultDemo values(40);
Query OK, 1 row affected (0.11 sec)
mysql> insert into defaultDemo values(80);
Query OK, 1 row affected (0.18 sec)
mysql> insert into defaultDemo values(90);
Query OK, 1 row affected (0.14 sec)
mysql> insert into defaultDemo values(100);
Query OK, 1 row affected (0.10 sec)

Display all records from the table using select statement.

The query is as follows

mysql> select *from defaultDemo;

The following is the output

+------+
| Id   |
+------+
| 10   |
| 20   |
| 30   |
| 40   |
| 80   |
| 90   |
| 100  |
+------+
7 rows in set (0.00 sec)

Here is the query to revert rows to default column value in MySQL

mysql> update defaultDemo set Id=default where Id > 45;
Query OK, 3 rows affected (0.39 sec)
Rows matched: 3 Changed: 3 Warnings: 0

Check the table records once again.

The query is as follows

mysql> select *from defaultDemo;

The following is the output

+------+
| Id   |
+------+
| 10   |
| 20   |
| 30   |
| 40   |
| NULL |
| NULL |
| NULL |
+------+
7 rows in set (0.00 sec)
raja
Published on 29-Mar-2019 10:24:07
Advertisements