What is the exact maximum value for TIMESTAMP in MySQL? - mysql

The documentation states that -
the range for TIMESTAMP values is '1970-01-01 00:00:01.000000' to '2038-01-19 03:14:07.999999'
But when I get this when I try to enter the maximum value:
mysql> insert into integration_table (`TIMESTAMP`) VALUES ('2038-01-19 03:14:07.999999');
ERROR 1292 (22007): Incorrect datetime value: '2038-01-19 03:14:07.999999' for column 'TIMESTAMP' at row 1
If I incrementally decrease the value, the maximum value that will work is '2038-01-19 03:14:07.499999'
MySQL version is mysql Ver 8.0.11 for Linux on x86_64 (MySQL Community Server - GPL) (running in Docker)
Column definition is `TIMESTAMP` timestamp NULL DEFAULT NULL,
Table definition is ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
I know that MySQL supports microseconds since version 5.6.4, so that's not the issue.
Am I misunderstanding something or is this some configuration issue?
Thanks

Defining the column as TIMESTAMP means it does not support microseconds, so .5 and above will be rounded up, which makes this value out of range.
The column needs to be defined as TIMESTAMP(6) to support 6 digit microseconds.

Related

MySQL datetime data saving error for 15+ years data

In MySQL 8.0.28 we have a table column named expiry_date as datetime filed.
When we try to insert data greater than 15 years, it fires
ERROR 1292: 1292: Incorrect datetime value: '2038-01-20 03:36:33' for column 'expiry_date' at row 1
For 2038-01-19 03:36:33 date, its inserting to DB.
Table ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC
We can not change data-type as this table is in production system.
What is causing this issue?
Following threads were checked, but not addressing our issue :
incorrect-datetime-value-while-saving-data-to-mysql
incorrect-datetime-value-database-error-number-1292
error-in-mysql-when-saving-data

MySQL 5.7.26 weird issue... cannot set datetime to: 2020-03-21 00:00:00

I cannot insert this date in my table: 2020-03-21!
I mean I can set 20 and 22 just fine, but when I set 21 it fails with Error:
Incorrect datetime value: '2020-03-21 00:00:00'
Using PDO I get:
PDOException::("SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '2020-03-21 00:00:00' for column 'when' at row 1")
I'm really confused, so any help is appreciated!
Server version: 5.7.26 - MySQL Community Server (GPL)
Protocol version: 10
Table:
CREATE TABLE `test` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`when` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4;
Queries:
This works just fine:
INSERT INTO `test` (`when`) VALUES ('2020-03-20 00:00:00');
This returns ERROR:
INSERT INTO `test` (`when`) VALUES ('2020-03-21 00:00:00');
Incorrect datetime value: '2020-03-21 00:00:00' for column 'when' at row 1
Same Error using phpMyAdmin
#MadhurBhaiya answered this as a comment,
The issue was "Daylight Saving", basically that time didn't exist for my server timezone.
Fixed it by changing timezone to UTC (which doesn't have Daylight Saving)
Also, please note that this issue only happens for TIMESTAMP field (doesn't happen for DATETIME)

MySQL (Workbench?) alters my scripts

I am creating a table into my MySQL database with this script, using MySQL Workbench:
CREATE TABLE `schema`.`Foo` (
[..],
`CreationDate` TIMESTAMP NOT NULL,
`LastUpdateDate` TIMESTAMP NOT NULL,
[..]
);
The problem is that at each commit (including UPDATE statements), the column CreationDate of the impacted rows is modified.
Using MySQL Workbench, I reverse engineered the table and get the following creation script:
CREATE TABLE `Foo` (
[..],
`CreationDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`LastUpdateDate` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
[..]
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
What the f***?
What kind of configuration within the server can alter my script this way? How can I create my table without these constraints?
I am using MySQL Workbench 6.3.8 Build 1228 CE (64 bits) Community and the server is 5.5.55.0+deb, running on Debian.
If you don't specify a default value, MySQL will pick one for you.
https://dev.mysql.com/doc/refman/5.7/en/data-type-defaults.html
In particular, you've hit these (emphasis mine):
For date and time types other than TIMESTAMP, the default is the appropriate “zero” value for the type. This is also true for TIMESTAMP if the explicit_defaults_for_timestamp system variable is enabled (see Section 5.1.5, “Server System Variables”). Otherwise, for the first TIMESTAMP column in a table, the default value is the current date and time. See Section 11.3, “Date and Time Types”.

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.

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

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?