Error in setting the default value to CURRENT_TIMESTAMP [duplicate] - mysql

This question already has answers here:
Invalid default value for 'dateAdded'
(8 answers)
Closed 6 years ago.
I am having problem while altering a table. I need a column with data type DATETIME to take default value as current date/time and on update also it should automatically update it's value to current date/time. I am writing the following SQL
ALTER TABLE `groups`
CHANGE COLUMN `modified` `modified` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
I am getting the following error message.
Error Code: 1067. Invalid default value for 'modified'
The MySQL version I'm using is 5.5.49 on a Ubuntu 14.04.1 system.
Please let me know how this can be fixed.

Prior to MySQL 5.6.5, you can only use the CURRENT_TIMESTAMP default value for columns of type TIMESTAMP.
See https://stackoverflow.com/a/9005872/1293303

Most probably this is because you already have another column with CURRENT_TIMESTAMP as default.
In MySQL versions prior to 5.6 this is a problem:
Why there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT clause?

Related

How to update current time stamp in MS SQL Server? [duplicate]

This question already has answers here:
Need a datetime column in SQL Server that automatically updates when the record is modified
(5 answers)
Closed 1 year ago.
What is the equivalent of the following in MS SQL server syntax?
CREATE TABLE `new_table` (
`updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)
I have tried:
update myTable set updated = CURRENT_TIMESTAMP;
Any update on table with timestamp data type will automatically update it's row timestamp value. If anything is missing in your question, please add more data.

How to add 'created_at' and 'updated_at' columns? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I need to add 'updated_at' and 'created_at' columns to some already existing table in MySQL database. I've added those colums using MySQL Workbench, but what query should I use to make them work properly? Thanks in advance ;)
According to the reference manual* you can use the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP clauses in column definitions:
With both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP,
the column has the current timestamp for its default value and is
automatically updated to the current timestamp.
whereas:
With a DEFAULT clause but no ON UPDATE CURRENT_TIMESTAMP clause, the
column has the given default value and is not automatically updated to
the current timestamp.
So, for example, you could use:
CREATE TABLE t1 (
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
To add the columns to an already existing table you can use:
ALTER TABLE t1
ADD COLUMN created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
ADD COLUMN updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
Note: Link provided refers to MySQL 8.0. The syntax is the same for previous versions as well. There is some difference though for versions prior to 5.6.5. Just quoting from the manual again:
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.
Try to use a Trigger. As example, when creating a new entry, the trigger will be activated and will execute, like an event in c# or an Action listener in Java!
With them, you can update that new entry with a creation date, or an edited date when you manipulated an entry. See this Documentation on how to use MySql Triggers on w3resource.

MySQL error when trying to add new columns [duplicate]

This question already has an answer here:
Create table fail in mysql when using CURDATE() as default
(1 answer)
Closed 7 years ago.
Trying to insert new columns into my blog posts table as part of my sites update.
Here is the mysql code I am using:
ALTER TABLE blogposts
ADD (
postType varchar(20),
postDate datetime NOT NULL DEFAULT CURDATE(),
postTime datetime NOT NULL DEFAULT CURTIME()
);
It throws this error though when executed:
#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 'CURDATE(),
postTime datetime NOT NULL DEFAULT CURTIME()
)' at line 4
I assume that curdate() is the issue, but do not understand why since it is a valid mysql function.
This is not a duplicate of this as was proposed., the solution in that post did not fix my problem and instead threw this error:
#1067 - Invalid default value for 'postDate'
When using code:
ALTER TABLE blogposts
ADD (
postType varchar(20),
postDate datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
postTime datetime NOT NULL DEFAULT CURTIME()
);
So for reference, this is not a duplicate question it seems.
Default values for DATETIME columns are not available before MySQL Server 5.6.5.
In 5.6.5 and later, the value you specify as default must be CURRENT_TIMESTAMP or an equivalent expression returning a datetime value.
https://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html
Because I want them separated out by default for easier calling from the backend.
That seems like a little bit of a frivolous motivation, when you can SELECT TIME(c1) AS teh_time, DATE(c1) AS teh_date if needed, and almost any other operation you wanted to do, like datetime math, time zone conversions or DATE_FORMAT() would require them to be recombined.

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

Invalid default value for 'dateAdded'

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