Updating boolean value in MySQL?


To update boolean value, you can use SET. Let us first create a table −

mysql> create table DemoTable
   (
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, isMarried boolean
   );
Query OK, 0 rows affected (0.58 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(isMarried) values(false);
Query OK, 1 row affected (0.16 sec)

mysql> insert into DemoTable(isMarried) values(true);
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable(isMarried) values(true);
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable(isMarried) values(false);
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 −

+----+-----------+
| Id | isMarried |
+----+-----------+
|  1 |         0 |
|  2 |         1 |
|  3 |         1 |
|  4 |         0 |
+----+-----------+
4 rows in set (0.00 sec)

Following is the query to update boolean value −

mysql> update DemoTable set isMarried = !isMarried where Id=4;
Query OK, 1 row affected (0.17 sec)
Rows matched: 1 Changed: 1 Warnings: 0

Let us display the table records once again −

mysql> select *from DemoTable;

This will produce the following output. Here, we updated the boolean value for Id 4 −

+----+-----------+
| Id | isMarried |
+----+-----------+
|  1 |         0 |
|  2 |         1 |
|  3 |         1 |
|  4 |         1 |
+----+-----------+
4 rows in set (0.00 sec)

Look at the above sample output, the row with Id 4 has been updated.

raja
Published on 17-May-2019 18:02:01
Advertisements