您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

MySQL设置当前时间为默认值方法

2024/3/11 14:58:29发布45次查看
由于mysql目前字段的默认值不支持函数,所以用create_time datetime default now()的形式设置默认值是不可能的。
代替的方案是使用timestamp类型代替datetime类型。
current_timestamp :当我更新这条记录的时候,这条记录的这个字段不会改变。
current_timestamp on update current_timestamp :当我更新这条记录的时候,这条记录的这个字段将会改变。即时间变为了更新时候的时间。
(注意一个update设置一个列为它已经有的值,这将不引起timestamp列被更新,因为如果你设置一个列为它当前的值,mysql为了效率而忽略更改。)如果有多个timestamp列,只有第一个自动更新。
下面为您介绍mysql设置当前时间为默认值的实现全步骤
数据库:test_db1
创建表:test_ta1
字段:
id 编号(自增 且为主键),
createtime 创建日期(默认值为当前时间)
方法一、用alert table语句创建:
 代码如下 复制代码
use test_db1;   create table test_ta1(   id mediumint(8) unsigned not nulll auto_increment,   createtime datetime,   primary key (id)   )engine=innodb default charset=gbk;   alert table test_ta1 change createtime createtime timestamp not null default now();
方法二、直接创建:
 代码如下 复制代码
use test_db1;   create table test_ta1(   id mediumint(8) unsigned not nulll auto_increment,   createtime timestamp not null default current_timestamp,   primary key (id)   )engine=innodb default charset=gbk;
方法三、使用可视化工具(如 mysql-front)创建
右击createtime属性
把type属性值改为timestamp
default 属性选择
以上就是mysql设置当前时间为默认值的方法介绍
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product