1

Following is my SQL query, it throws an error:-

CREATE TABLE IF NOT EXISTS USER_PROFILE(Id INT PRIMARY KEY AUTO_INCREMENT, date DATETIME NOT NULL DEFAULT NOW) ;

It says Invalid default value for 'date'.

I've tried synonyms for NOW() as well, namely CURRENT_TIMESTAMP, but still the same error.

How can I create a column date with default value current time?

On the documentation page, it says to assign this way

CREATE TABLE t1 (
  ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  dt DATETIME DEFAULT CURRENT_TIMESTAMP
);
1
  • I changed my type to TIMESTAMP and it worked, but now I'm stucked upon creating an index. My query is CREATE INDEX IF NOT EXISTS LINK_INDEX on Table(url_hash,content_hash,date_). If I execute it woithout IF NOT EXISTS, it works fine. Whats the issue>? Commented Mar 10, 2015 at 15:11

2 Answers 2

5

From the document

The DEFAULT value clause in a data type specification indicates a default value for a column. With one exception, the default value must be a constant; it cannot be a function or an expression. This means, for example, that you cannot set the default for a date column to be the value of a function such as NOW() or CURRENT_DATE. The exception is that you can specify CURRENT_TIMESTAMP as the default for TIMESTAMP and DATETIME columns

So no function is allowed in the default value hence the first query is failing.

Again from the document

As of MySQL 5.6.5, TIMESTAMP and DATETIME columns can be automatically initializated and updated to the current date and time (that is, the current timestamp). Before 5.6.5, this is true only for TIMESTAMP, and for at most one TIMESTAMP column per table. The following notes first describe automatic initialization and updating for MySQL 5.6.5 and up, then the differences for versions preceding 5.6.5.

Before 5.6.5, this is true only for TIMESTAMP

So your mysql version is less than 5.6.5 hence the 2nd query is failing too.

So you need to create the table as

CREATE TABLE IF NOT EXISTS 
USER_PROFILE
(
  Id INT PRIMARY KEY AUTO_INCREMENT, 
  date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ;
Sign up to request clarification or add additional context in comments.

2 Comments

I changed my type to TIMESTAMP and it worked, but now I'm stucked upon creating an index. My query is CREATE INDEX IF NOT EXISTS LINK_INDEX on Table(url_hash,content_hash,date_). If I execute it woithout IF NOT EXISTS, it works fine. Whats the issue>?
I usually do as alter table table_name add index some_index_name(col1,col2) you may try the same. Check the doc here dev.mysql.com/doc/refman/5.6/en/create-index.html if not exists does not supported yet
0

It might be that DATE, as a reserved word, is confusing it by the time it gets to the DEFAULT clause. Try a different name and if that works, try quoting "date".

1 Comment

I changed my type to TIMESTAMP and it worked, but now I'm stucked upon creating an index. My query is CREATE INDEX IF NOT EXISTS LINK_INDEX on Table(url_hash,content_hash,date_). If I execute it woithout IF NOT EXISTS, it works fine. Whats the issue>?

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.