Change tinyint default value to 1 in MySQL?

MySQLMySQLi Database

You can use DEFAULT command for this. Following is the syntax −

alter table yourTableName change yourColumnName yourColumnName TINYINT(1) DEFAULT 1 NOT NULL;

Let us first create a table −

mysql> create table DemoTable
(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   UserName varchar(20),
   UserAge int,
   isMarried tinyint(1)
);
Query OK, 0 rows affected (0.80 sec)

Let us check the description of table −

mysql> desc DemoTable;

This will produce the following output −

+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| Id        | int(11)     | NO   | PRI | NULL    | auto_increment |
| UserName  | varchar(20) | YES  |     | NULL    |                |
| UserAge   | int(11)     | YES  |     | NULL    |                |
| isMarried | tinyint(1)  | YES  |     | NULL    |                |
+-----------+-------------+------+-----+---------+----------------+
4 rows in set (0.21 sec)

Following is the query to change tinyint default value to 1 −

mysql> alter table DemoTable change isMarried isMarried TINYINT(1) DEFAULT 1 NOT NULL;
Query OK, 0 rows affected (1.29 sec)
Records: 0 Duplicates: 0 Warnings: 0

Let us check the description of table once again. The default value of tinyint has been changed to 1 −

mysql> desc DemoTable;

This will produce the following output −

+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| Id        | int(11)     | NO   | PRI | NULL    | auto_increment |
| UserName  | varchar(20) | YES  |     | NULL    |                |
| UserAge   | int(11)     | YES  |     | NULL    |                |
| isMarried | tinyint(1)  | NO   |     | 1       |                |
+-----------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
raja
Published on 29-Apr-2019 07:56:30
Advertisements