Invalid default value for 'createdAt' DATETIME DEFAULT NOW() - mysql

I am running the following query in mariadb/ mysql engine.
CREATE TABLE IF NOT EXISTSQuotes(idINTEGER NOT NULL auto_increment ,quoteTEXT,authorVARCHAR(255),genreVARCHAR(255),tagTEXT,createdAtDATETIME DEFAULT NOW(),updatedAtDATETIME DEFAULT NOW(), PRIMARY KEY (id)) ENGINE=InnoDB;
However, I am getting the following error:
An error occurred while creating the table:
{Invalid default value for 'createdAt'}
Here is the result of sql mode:
show variables like 'sql_mode' ;
STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ALLOW_INVALID_DATES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Any help is appreciated.

From https://mariadb.com/kb/en/library/datetime/ :
MariaDB starting with 10.0.1 Since MariaDB 10.0.1, DATETIME columns
also accept CURRENT_TIMESTAMP as the default value.
Note that NOW() is not mentioned, but the effect is the same.

Related

MySql 5.7 Timestamp works in windows but not on linux

I am trying to create a table using the script down below and getting the error
Error Code: 1067. Invalid default value for 'LAST_MODIFIED_TS.
In my understanding after 5.6 you could create more than one timestamp column also it is not necessary to provide default values . Another thing is it is not barfing at Created_TS which is just one line before it.
Also the same script works on windows but not on linux ubuntu , the version of mysql running on both of them is 5.7
CREATE TABLE testdb.test (
ID BIGINT NOT NULL AUTO_INCREMENT,
DESCRIPTION VARCHAR(300) NOT NULL,
CREATED_TS TIMESTAMP NOT NULL,
LAST_MODIFIED_TS TIMESTAMP NOT NULL,
PROPERTY_TYPE VARCHAR(1) DEFAULT 'S',
last_modified timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
CONSTRAINT test_pk PRIMARY KEY
(ID) ) ENGINE=InnoDB ;
Make sure you don't have NO_ZERO_DATE sql_mode variable set (http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_no_zero_date) by running the following query:
show variables like 'sql_mode';

#1067 - Invalid default value for 'date'

I have the following field in a CREATE TABLE statement:
`date` DATETÄ°ME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
But I get the following error:
Error: #1067 - Invalid default value for 'date'
How can i fix that problem?
MySQL 5.6.5 added that default syntax for datetime, previously it was only available for TIMESTAMP.
In other words, you most likely need to change your datatype or upgrade your MySQL.

What is the reason why MySQL is telling me "Invalid default value for 'postdate'"?

I have 2 Windows servers running MySQL, one of them running version 5.1 and the other running version 5.7. I am trying trying to copy a database from the MySQL 5.7 over to the 5.1 on the other server and believe it's the difference in versions (new syntax in 5.7?) that is causing this error, but I could be wrong.
After Exporting through phpMyAdmin the database I in the 5.7 version and trying to Import in the 5.1 version I'm getting the error
MySQL said: Documentation #1067 - Invalid default value for
'postdate'
on the command
CREATE TABLE IF NOT EXISTS `jobs` (
`id` mediumint( 9 ) NOT NULL ,
`title` varchar( 200 ) DEFAULT NULL ,
`descr` varchar( 5000 ) DEFAULT NULL ,
`postdate` datetime DEFAULT CURRENT_TIMESTAMP
) ENGINE = InnoDB AUTO_INCREMENT =5 DEFAULT CHARSET = utf8mb4;
Any idea why?
In 5.1, the default value has to be a constant value (e.g. NULL is acceptable) except for the timestamp type where current_timestamp is allowed. I.e., for a date or datetime you cannot use current_date, now or current_timestamp.
So you either stick to the datetime type for your postdate column and you have to give up current_timestamp as a default value (you can maybe set up a trigger for the purpose, see examples here), or - depending on your requirements - consider using timestamp (which has a different range of values).
The corresponding section of the manual says:
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 a TIMESTAMP column.

Cannot use functions and variables in DEFAULT constraint when creating a new database table

I'm trying to create a new database using the CREATE command. Here's the SQL:
CREATE TABLE test(
rok_utworzenia timestamp DEFAULT CURRENT_DATE
);
But it throws an error saying
'#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 )' at line 2'
It works if I change the DEFAULT constraint to a constant, but not if I try to use any function or variable. Why is that so?
Use this:
CREATE TABLE test( rok_utworzenia timestamp DEFAULT CURRENT_TIMESTAMP );
From MySQL site:
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 a
TIMESTAMP column.
Try this CURRENT_TIMESTAMP:
CREATE TABLE test( rok_utworzenia timestamp DEFAULT CURRENT_TIMESTAMP );

TIMESTAMP not valid and #1064 error [duplicate]

I got a stupid problem with SQL that I can't fix.
ALTER TABLE `news`
ADD `dateAdded` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP AUTO_INCREMENT ,
ADD PRIMARY KEY ( `dateAdded` )
Error:
(#1067)Invalid default value for 'dateAdded'
Can somebody help me?
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.
relevant docs: http://dev.mysql.com/doc/refman/5.0/en/data-type-defaults.html
You can work around this by setting a post-insert trigger on the table to fill in a "now" value on any new records.
CURRENT_TIMESTAMP is version specific and is now allowed for DATETIME columns as of version 5.6.
See MySQL docs.
Also do note when specifying DATETIME as DATETIME(3) or like on MySQL 5.7.x, you also have to add the same value for CURRENT_TIMESTAMP(3). If not it will keep throwing 'Invalid default value'.
I had the same issue,
following fix solved my problem.
Select Type as 'TIMESTAMP'
DON'T ENTER ANYTHING IN LENGTH/VALUES FIELD. KEEP IT BLANK
Select CURRENT_TIMESTAMP as Default value.
I am using MySQL ver 5.5.56
I have mysql version 5.6.27 on my LEMP and CURRENT_TIMESTAMP as default value works fine.
mysql version 5.5 set datetime default value as CURRENT_TIMESTAMP will be report error
you can update to version 5.6 , it set datetime default value as CURRENT_TIMESTAMP
Change the type from datetime to timestamp and it will work!
I had the same issue for mysql 5.5.56-MariaDB - MariaDB Server
Hope it can help... sorry if depricated
I solved mine by changing DATE to DATETIME