登陆

建表的时候 对datetime的报错,不知道为什么?

[SQL] create table s7_note

(

  id mediumint unsigned not null auto_increment comment 'Id',

  title varchar(100) not null comment '标题',

  content longtext not null comment '内容',

  addtime datetime not null default current_timestamp comment '添加时间',

  ip int not null  comment 'IP地址',

  primary key(id)

)engine=InnoDB default charset utf8 comment '留言表';

[Err] 1067 - Invalid default value for 'addtime'


# PHP
元曦元曦1546 天前786 次浏览

全部回复(3)我要回复

  • 逸先生

    逸先生2017-08-24 10:38:16

    create table s7_note
    (
      id mediumint unsigned not null auto_increment comment 'Id',
      title varchar(100) not null comment '标题',
      content longtext not null comment '内容',
      addtime datetime not null default '' comment '添加时间',
      ip int not null  comment 'IP地址',
      primary key(id)
    )engine=InnoDB default charset utf8 comment '留言表';

    #将default的值更换一下就好,最简单的方式

    回复
    1
  • 天蓬老师

    天蓬老师2017-08-21 17:18:45

    datetime与current_timestamp,理论上来说,他们的值是相同的。但是current_timestamp在读写时有一个自动转换的过程,并且它们之间的取值范围也不一样的。你可以将current_timestamp理解为一个变量也是可以用的,因为它在读写时要进行转换,可以把它想像成有一个date()函数在背后偷偷的对它进行格式化操作。这时它就变成了一个表达式了。

    建议最好升级一下。或者换一个值或常量来初始化datetime。

    回复
    1
  • 天蓬老师

    天蓬老师2017-08-21 12:00:06

    请升级到MySQL 5.6+

    回复
    1
  • 元曦

    没有其它替代办法么?

    元曦 · 2017-08-21 14:14:07
  • 取消回复发送