Which datatype should I use for flag in MySQL?


To set a flag, you can set the type as tinyint(1) type.

 Following is the syntax −

yourColumnName tinyint(1) DEFAULT 1;

Let us first create a table −

mysql> create table DemoTable
(
   ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   ClientName varchar(20),
   isMarried tinyint(1) DEFAULT 1
);
Query OK, 0 rows affected (0.64 sec)

Insert some records in the table using insert command −

mysql> INSERT INTO DemoTable(ClientName,isMarried) values('Larry',0);
Query OK, 1 row affected (0.16 sec)
mysql> INSERT INTO DemoTable(ClientName) values('David');
Query OK, 1 row affected (0.12 sec)
mysql> INSERT INTO DemoTable(ClientName,isMarried) values('Mike',1);
Query OK, 1 row affected (0.19 sec)
mysql> INSERT INTO DemoTable(ClientName) values('Carol');
Query OK, 1 row affected (0.14 sec)

Following is the query to display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output that displays one of the column, which is set as flag −

+----------+------------+-----------+
| ClientId | ClientName | isMarried |
+----------+------------+-----------+
| 1        | Larry      | 0         |
| 2        | David      | 1         |
| 3        | Mike       | 1         |
| 4        | Carol      | 1         |
+----------+------------+-----------+
4 rows in set (0.00 sec)

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements