Home  >  Q&A  >  body text

When creating a table, a datetime error is reported. I don’t know why?

[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'


元曦元曦2639 days ago1423

reply all(3)I'll reply

  • 逸先生

    逸先生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 '留言表';

    #Just change the default value, the simplest way

    reply
    1
  • 天蓬老师

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

    datetime and current_timestamp, theoretically, their values ​​are the same. However, current_timestamp has an automatic conversion process when reading and writing, and the value ranges between them are also different. You can think of current_timestamp as a variable, because it needs to be converted when reading and writing. You can think of it as having a date() function that secretly formats it behind the scenes. At this point it becomes an expression.

    It is recommended to upgrade. Or change a value or constant to initialize datetime.

    reply
    1
  • 天蓬老师

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

    Please upgrade to MySQL 5.6+

    reply
    1
  • 元曦

    Is there no other alternative?

    元曦 · 2017-08-21 14:14:07
  • Cancelreply