1

Below is the query that I userd to execute to create a table:

CREATE TABLE `users_authentication` (
`id` int(11) NOT NULL,
`users_id` int(11) NOT NULL,
`token` varchar(255) NOT NULL,
`expired_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;  

But it shows an error message like this:

1067 - Invalid default value for 'expired_at'

4
  • Perhaps you need column data type timestamp? Commented Apr 12, 2017 at 7:55
  • I executed the same query on my machine it is working. Commented Apr 12, 2017 at 8:10
  • @jarlh I wanna add date and time thats all Commented Apr 12, 2017 at 8:17
  • @dsharew but its not working for me Commented Apr 12, 2017 at 8:17

3 Answers 3

2

Use TIMESTAMP instead of DATETIME. In some versions of MySQL, it is not possible to use CURRENT_TIMESTAMP with DATETIME.

So, you can replace your script for this, for MySQL versions previous to 5.6.5:

CREATE TABLE `users_authentication` (
`id` int(11) NOT NULL,
`users_id` int(11) NOT NULL,
`token` varchar(255) NOT NULL,
`expired_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`created_at` TIMESTAMP NOT NULL,
`updated_at` TIMESTAMP NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

Check here: How do you set a default value for a MySQL Datetime column?

The MySQL doc:

As of MySQL 5.6.5, TIMESTAMP and DATETIME columns can be automatically initialized 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.

MySQL Doc

Sign up to request clarification or add additional context in comments.

4 Comments

"Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause" This is the error now its showing
And Mysql Server version: 5.5.42
check again the doc and for at most one TIMESTAMP column per table. You can add only one timestamp column with current date by default.
Good. This is what i did :)
0

CURRENT_TIMESTAMP is only acceptable on TIMESTAMP fields. DATETIME fields must be left either with a null default value, or no default value at all - default values must be a constant value, not the result of an expression.

Reference - data-type-defaults

1 Comment

so can you please make this query executable somehow?
-1
CREATE TABLE users_authentication(
    id int(30) NOT NULL,
    user_id int(30) NOT NULL,
    token varchar(40),
    expired_at TIMESTAMP NOT NULL DEFAULT 0,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL
);

We cant use three TIMESTAMP like TIMESTAMP NOT NULL so we have to change this by setting different default value for all One Mysql Table with Multiple TIMESTAMP Columns

1 Comment

now check and if u not satisfy then i can add my screenshot query@GertArnold

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.