Mysql中设置默认时间为当前值
Posted SaraMorning
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql中设置默认时间为当前值相关的知识,希望对你有一定的参考价值。
1、直接在创建表时添加该列并声明默认值,如下:
CREATE TABLE `table1` ( `id` int(11) NOT NULL, `createtime` timestamp NULL default CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
如果是在navicat下操作的话,设置字段的类型为timestamp,默认值写上CURRENT_TIMESTAMP,如下图:
2、在现有表中添加新列
ALTER TABLE table1 ADD COLUMN `createtime` timestamp NULL DEFAULT CURRENT_TIMESTAMP
3、 修改某一列为时间格式并添加默认值
alter table table1 change createtime newtime timestamp null default current_timestamp
以上是关于Mysql中设置默认时间为当前值的主要内容,如果未能解决你的问题,请参考以下文章
从 DB 获取数据以在下拉 HTML/CSS/MYSQL/PHP 中设置为默认值
麻烦问一下各位老师了 在mysql中设置int类型的字段 想把字段的默认值设置为-1 请问该怎么办?