Can't alter mysql column default timestamp value to lowest possible value? - mysql

I'm trying to alter an existing MySQL column in a specific table.
I want to change the column, which is of type timestamp, to the earliest possible value as the default value.
I'm using MySQL version 5.5.40
According to the MySql Docs, timestamp range value can be:
The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
But when I try to alter the table column to '1970-01-01 00:00:01' I get an error.
Here's the query that fails:
ALTER TABLE user MODIFY COLUMN latest_login_date timestamp default '1970-01-01 00:00:01' NOT NULL;
Gives:
Error Code: 1067. Invalid default value for 'latest_login_date'
Also tried other values a few seconds later - still no good.
The first value that worked for me was this:
ALTER TABLE account_social_integration MODIFY COLUMN latest_login_date timestamp default '1970-01-01 02:00:01' NOT NULL;
Can someone explain please?

Related

MySQL DATE field with default CURDATE(). NOT DATETIME

It is possible to set default value on DATE (NOT DATETIME) column in MySQL 5.7 to current date?
I try this (generated by Workbench):
ALTER TABLE `db`.`table` CHANGE COLUMN `column` `column` DATE NOT NULL DEFAULT CURDATE() ;
but not works for me.
(no data in table)
No, you cannot. The documentation is pretty clear on this:
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 TIMESTAMP and DATETIME columns. See Section 12.3.5, “Automatic
Initialization and Updating for TIMESTAMP and DATETIME”.
You can do one of the following:
Set up a column with a default value for the DATETIME. Create view that extracts the date as a separate column.
Create an insert trigger to set the date column.
There is a way you can do this if you have another column that has a for example a datetime field with a default of NOW(). See this post:

Database timestamps in sql

Creating a database table in MySQL. I have created two fields to grab timestamps.
created_at timestamp default '0000-00-00 00:00:00',
updated_at timestamp default now() on update now(),
When I update the database, both fields are updating to the current timestamp. Any thoughts on how to prevent this from happening? I am not providing the 'created_at' field when I update -- I am also providing 'null' for the updated_at field to auto update.
Depending on the version of MySQL you should be able to use the following for your default value on the updated_at field:
CURRENT_TIMESTAMP on UPDATE CURRENT_TIMESTAMP
Your created_at field should just have CURRENT_TIMESTAMP as the default.
I would also set both to the datatype datetime instead of timestamp.
Timestamp initialisation can be an ugly beast in mysql!
Based on the commment below you have mysql v5.6.4 or earlier:
I can't have two current_timestamps declared without getting an error -- and although it's default is 0 -- it is producing a current timestamp upon creation(without me feeding in the timestamp) – Spencer Rohan
You can have a single timestamp field that are initialised and/or updated to the current timestamp when you insert / update your record. In earlier versions this had to be the 1st timestamp field in the table.
I would simply set the created_at to be a nullable column with a default value of null because according to mysql documentation on timestamp initialization:
In other words, a TIMESTAMP column defined to permit NULL values auto-initializes only if its definition includes DEFAULT CURRENT_TIMESTAMP
By changing the default value to null you go around any problems you may have with zero dates and various sql modes.
created_at timestamp null default null,

Column name is not taking timestamp as default value

I have two columns created_at and updated_at in same table. Both the columns have data type as datetime. Now when I am trying to modify the column data types as follows -
alter table nodestatics modify column updated_at datetime default current_timestamp;
alter table nodestatics modify column created_at datetime default current_timestamp;
It is showing the following error
Error Code : 1067 Invalid default value for 'updated_at' (0 ms taken)
Error Code : 1067 Invalid default value for 'created_at' (0 ms taken)
My mysql version is 5.5.41-0ubuntu0.14.04.1 (Ubuntu)
It is hard to reference documentation in a comment:
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.
Either upgrade to 5.6.5. Or use TIMESTAMP instead of DATETIME.
Use TIMESTAMP instead of DATETIME
Should be something like this:
ALTER TABLE nodestatics MODIFY COLUMN updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
ALTER TABLE nodestatics MODIFY COLUMN created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
Or you can use TRIGGERS.
CREATE TRIGGER myTable_OnInsert BEFORE INSERT ON `tblMyTable`
FOR EACH ROW SET NEW.dateAdded = NOW();
You can look at similar topic. Happy coding!
Try this
alter table nodestatics modify column created_at timestamp default current_timestamp;

Invalid default value for timestamp type column in Toad

I am working in ToadForMySQL and I have a table that I created where there are 2 timestamp type columns:
MatchStartDate
MatchEndDate
I had set the default for the MatchStartDate to CURRENT_TIMESTAMP and MatchEndDate was set as 0000-00-00 00:00:00. However, now I want to set each column with a default value of: 1970-01-01 00:00:00
However, when I try to do that for the MatchStartDate column I get the following error:
Invalid default value for 'MatchStartDate'
Here is the SQL Script:
ALTER TABLE Think.DirectMailList
CHANGE MatchStartDate MatchStartDate TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT '1970-01-01 00:00:00';
Any suggestions/direction would be appreciated. Thanks.
Not a valid TIMESTAMP; you need to add 1 second.
The DATE, DATETIME, and TIMESTAMP Types
The TIMESTAMP data type is used for values that contain both date and time parts.
TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
Example:
CREATE TABLE DirectMailList (MatchStartDate TIMESTAMP);
ALTER TABLE DirectMailList
CHANGE MatchStartDate MatchStartDate TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT '1970-01-01 00:00:01';
sqlfiddle

mysql timestamp column

Is it possible to define a timestamp column in a MySQL table that will automatically be updated every time a field in the same row is modified? Ideally this column should initially be set to the time a row was inserted.
Cheers,
Don
You can use the timestamp column as other posters mentioned. Here is the SQL you can use to add the column in:
ALTER TABLE `table1` ADD `lastUpdated` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ;
This adds a column called 'lastUpdated' with a default value of the current date/time. When that record is updated (lets say 5 minutes later) that timestamp will automatically update to the current time.
That is the default functionality of the timestamp column type. However, note that the format of this type is yyyymmddhhmmss (all digits, no colons or other separation).
EDIT: The above comment about the format is only true for versions of MySQL < 4.1... Later versions format it like a DateTime
This is what I have observed (MySql 5.7.11) -
The first TIMESTAMP column in the table gets current timestamp as the default value. So, if you do an INSERT or UPDATE without supplying a value, the column will get the current timestamp.
Any subsequent TIMESTAMP columns should have a default value explicitly defined. If you have two TIMESTAMP columns and if you don't specify a default value for the second column, you will get this error while trying to create the table -
ERROR 1067 (42000): Invalid default value for 'COLUMN_NAME'
A MySQL timestamp is set with creation or update time only if their default value is set as it. ALTER TABLE some_table ADD when TIMESTAMP DEFAULT CURRENT_TIMESTAMP.
Otherwise it works just like a DateTime field, only that it's relative to 1970/01/01 UTC, so it's an absolute point in time not depending on a specific timezone as is DateTime.