要更改MySQL表列的默认值,可以使用CHANGE命令。语法如下-
alter table yourTableName change yourCoumnName youColumnName datatype not null default Value;
为了理解上述语法,让我们创建一个表。以下是查询-
create table DefaultDemo −> ( −> ArrivalTime timestamp −> );
这是描述带有默认列的表的查询-
desc DefaultDemo;
以下是输出-
+-------------+-----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+-----------+------+-----+---------+-------+ | ArrivalTime | timestamp | YES | | NULL | | +-------------+-----------+------+-----+---------+-------+ 1 row in set (0.11 sec)
现在,您可以将MySQL表列更改为默认值。查询如下-
ALTER TABLE DefaultDemo CHANGE ArrivalTime ArrivalTime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP; Records: 0 Duplicates: 0 Warnings: 0
借助insert命令检查是否插入了默认值。查询如下-
insert into DefaultDemo values();
显示记录-
select *from DefaultDemo;
以下是输出-
+---------------------+ | ArrivalTime | +---------------------+ | 2018-12-07 11:31:00 | +---------------------+ 1 row in set (0.00 sec)