19

I need to create mysql table with default value on column CURRENT_DATE()

I try

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_DATE(),
  PRIMARY KEY (`id`),
  UNIQUE KEY `person` (`ip`,`date`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

but it is writting an error

Query: CREATE TABLE `visitors` ( `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, `ip` VARCHAR(32) NOT NULL, `browser` VARCHAR(500) NO...

Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CURRENT_DATE() NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `person` (`ip`,`date`)
) ENGINE=INNODB AUTO_INCREMENT=1' at line 7

Execution Time : 0 sec
Transfer Time  : 0 sec
Total Time     : 0.063 sec

what is the problem?

I need to uniques only date not with full time info...

can you help me?

3
  • This is the default behaviour of a TIMESTAMP column. You don't actually need to specify DEFAULT in this case. Commented Dec 22, 2015 at 14:32
  • TIMESTAMP and DATE do not have the same format, Timestamp is a number while date is a formatted date. Choose one or the other. Commented Dec 22, 2015 at 14:37
  • Why don't you try 'date' DATE NOT NULL DEFAULT CURRENT_DATE() if you only need year, month and day. Commented Jul 28, 2017 at 6:17

3 Answers 3

46

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;
Sign up to request clarification or add additional context in comments.

3 Comments

no CURRENT_TIMESTAMP is setting full time informations with hour,minutes and secunds too. I need only year,month and day
One option is to set it up as a timestamp, as shown, but when you actually retrieve the data, just cast it as a date, which will truncate the hours, minutes and seconds. SELECT DATE(date) will return it in the format you appear to want, while still preserving the behavior you want for the column.
I understand but I need to unique IP with year, month and date so if I unique it as timestamp after 2-3 second ago it will insert again new row... I need unique becouse after refresh if IP is exists today it must not write in table so becouse I need only date format
0

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

1 Comment

NOW() will be consider as CURRENT_TIMESTMAP in mysql
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.