5

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

Community
  • 1
  • 1
A.C.Manikandan
  • 227
  • 2
  • 3
  • 12

3 Answers3

4

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

Vipin Jain
  • 3,686
  • 16
  • 35
0

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 )
Irshad
  • 3,071
  • 5
  • 30
  • 51
Aashick
  • 91
  • 2
  • 15
0

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 
);
carol
  • 311
  • 2
  • 4
  • 14