ITPub博客

首页 > Linux操作系统 > Linux操作系统 > MySQL设置当前时间为默认值的方法

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

原创 Linux操作系统 作者:season0891 时间:2013-01-23 17:38:33 0 删除 编辑

MySQL设置当前时间为默认值的问题我们经常会遇到,下面就为您介绍MySQL设置当前时间为默认值的实现全步骤,希望对您能有所启迪。

数据库:test_db1

创建表:test_ta1

两个字段:id              (自增 且为主键),

createtime 创建日期(默认值为当前时间)

方法一、是用alert table语句:

  1. use test_db1;  
  2.  
  3. create table test_ta1(  
  4.  
  5. id mediumint(8) unsigned not nulll auto_increment,  
  6.  
  7. createtime datetime,  
  8.  
  9. primary key (id)  
  10.  
  11. )engine=innodb default charset=gbk;  
  12.  
  13. alert table test_ta1 change createtime createtime timestamp not null default now();  
  14.  

方法二、直接创建方便:

  1. use test_db1;  
  2.  
  3. create table test_ta1(  
  4.  
  5. id mediumint(8) unsigned not nulll auto_increment,  
  6.  
  7. createtime timestamp not null default current_timestamp,  
  8.  
  9. primary key (id)  
  10.  
  11. )engine=innodb default charset=gbk;  
  12.  

方法三、可视化工具如 mysql-front

右击createtime属性

把Type属性值改为timestamp

default 属性选择

以上就是MySQL设置当前时间为默认值的方法介绍。

【编辑推荐】

MySQL分表处理的实现方法

MySQL授权表使用示例

MySQL多表删除的实现

MySQL独立表空间的优缺点

mysql数据库大小写的问题讨论

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/90618/viewspace-753142/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-06-10

  • 博文量
    790
  • 访问量
    2098041