您可以为此使用DEFAULT命令。以下是语法-
alter table yourTableName change yourColumnName yourColumnName TINYINT(1) DEFAULT 1 NOT NULL;
让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, UserName varchar(20), UserAge int, isMarried tinyint(1) );
让我们检查表的描述-
mysql> desc DemoTable;
这将产生以下输出-
+-----------+-------------+------+-----+---------+----------------+ | 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)
以下是将tinyint默认值更改为1的查询-
mysql> alter table DemoTable change isMarried isMarried TINYINT(1) DEFAULT 1 NOT NULL; Records: 0 Duplicates: 0 Warnings: 0
让我们再次检查表的描述。tinyint的默认值已更改为1-
mysql> desc DemoTable;
这将产生以下输出-
+-----------+-------------+------+-----+---------+----------------+ | 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)