How to create mysql table with column timestamp default current_date?

夙愿已清 提交于 2019-12-03 03:03:57

Use CURRENT_TIMESTAMP function instead of CURRENT_DATE() function

Try this:

DROP TABLE IF EXISTS `visitors`;
CREATE TABLE `visitors` (
  `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  `ip` VARCHAR(32) NOT NULL,
  `browser` VARCHAR(500) NOT NULL,
  `version` VARCHAR(500) NOT NULL,
  `platform` ENUM('w','l','m') NOT NULL,
  `date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `person` (`ip`,`date`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

you just have to replace CURRENT_DATE() by NOW() in your query. I tried it and it's look ok.

This may be a little late but I think it might be helpful to someone else.

My approach has been to use getdate() like this:

[TimeStamp] [datetime] NOT NULL CONSTRAINT [DF_myTable_TimeStamp] DEFAULT (getdate()).

Where [TimeStamp] is the column in question.

The result is for example: 2017-11-02 11:58:34.203

To trim this, I use the following

declare @mydate datetime

set @mydate = '2017-11-02 11:58:34.203'

SELECT try_convert(nvarchar(20), @mydate, 120)

This final result is: 2017-11-02 11:58:34

You can actually set this in MSSQL Management Studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!