Increment column value ‘ADD’ with MySQL SET clause


Since the column value ‘ADD’ is already a reserved word, therefore you need to use backticks around the word ADD like `ADD`.

Let us see an example and create a table −

mysql> create table DemoTable779 (
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   `ADD` int
);
Query OK, 0 rows affected (0.47 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable779(`ADD`) values(54);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable779(`ADD`) values(89);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable779(`ADD`) values(66);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable779(`ADD`) values(58);
Query OK, 1 row affected (0.10 sec)

Now you can display all records from the table using select statement −

mysql> select *from DemoTable779;

This will produce the following output -

+----+------+
| Id | ADD  |
+----+------+
| 1  | 54   |
| 2  | 89   |
| 3  | 66   |
| 4  | 58   |
+----+------+
4 rows in set (0.00 sec)

Following is how you can increment the column ‘ADD’ −

mysql> update DemoTable779
set `ADD`=`ADD`+10;
Query OK, 4 rows affected (0.20 sec)
Rows matched: 4 Changed: 4 Warnings: 0

Let us check table records once again −

mysql> select *from DemoTable779;

This will produce the following output -

+----+------+
| Id | ADD  |
+----+------+
| 1  | 64   |
| 2  | 99   |
| 3  | 76   |
| 4  | 68   |
+----+------+
4 rows in set (0.00 sec)
raja
Published on 09-Sep-2019 11:36:27
Advertisements