A.C.Manikandan
A.C.Manikandan

Reputation: 227

current_timeStamp is not working

I tried this code

CREATE TABLE users (
    userId INT PRIMARY KEY AUTO_INCREMENT NOT NUll,
    account VARCHAR(200) NOT NULL,
    password varchar(200) NOT Null,
    isActive varchar(10) NOT NUll,
    
    createdDate DATETIME DEFAULT CURRENT_TIMESTAMP() NOT NUll,
    updatedDate DATETIME 
);

but the following error will come

1067 - Invalid default value for 'createdDate'

thanks

Upvotes: 5

Views: 18454

Answers (3)

carol
carol

Reputation: 319

Try this code, it should work.

CREATE TABLE users (
userId INT PRIMARY KEY AUTO_INCREMENT NOT NUll,
account VARCHAR(200) NOT NULL,
password varchar(200) NOT Null,
isActive varchar(10) NOT NUll,
createdDate DATETIME DEFAULT CURRENT_TIMESTAMP NOT NUll,
updatedDate DATETIME 
);

Upvotes: -1

Vipin Jain
Vipin Jain

Reputation: 3756

Use simply CURRENT_TIMESTAMP instead CURRENT_TIMESTAMP()

CREATE TABLE users ( 
    userId INT PRIMARY KEY AUTO_INCREMENT NOT NUll, 
    account VARCHAR(200) NOT NULL, password varchar(200) NOT Null, 
    isActive varchar(10) NOT NUll,
    createdDate DATETIME DEFAULT CURRENT_TIMESTAMP NOT NUll,
   updatedDate DATETIME 
);

In addition, you can initialize or update any TIMESTAMP column to the current date and time by assigning it a NULL value, unless it has been defined with the NULL attribute to permit NULL values.

For more knowledge click the link http://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html

Upvotes: 5

Aashick
Aashick

Reputation: 91

Change the datatype datetime to timestamp it will work.

CREATE TABLE users ( userId INT PRIMARY KEY AUTO_INCREMENT NOT NUll, 
account VARCHAR(200) NOT NULL, 
password varchar(200) NOT Null, 
isActive varchar(10) NOT NUll,
createdDate **timestamp** DEFAULT CURRENT_TIMESTAMP() NOT NUll,
updatedDate DATETIME )

Upvotes: 0

Related Questions