mysql updated_at use milliseconds since EPOCH as default ON UPDATE - mysql

In my app I am using Sequelize. Now I want to use milliseconds since EPOCH for the updated_at column, so that it looks like:
-------------
updated_at
-------------
1571838511364
-------------
The data definition now looks like:
CREATE TABLE `projects` (
`id` char(36) NOT NULL,
`name` varchar(45) NOT NULL,
`description` varchar(450) NOT NULL,
`active` tinyint(1) NOT NULL DEFAULT '1',
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
I have done this to the created_at as it is easy to set the default value with Sequelize, but for updated_at, how do I set the default value for ON UPDATE?
I know
SELECT UNIX_TIMESTAMP(current_timestamp()) will give me the current millisecond, so I have:
updatedAt: {
type: Sequelize.STRING,
allowNull: false,
defaultValue: Sequelize.literal(
'UNIX_TIMESTAMP(current_timestamp()) ON UPDATE UNIX_TIMESTAMP(current_timestamp())'
),
},
And this will give me error:
Executing:
ALTER TABLE `ab`.`projects`
CHANGE COLUMN `updated_at` `updated_at` VARCHAR(45) NOT NULL DEFAULT UNIX_TIMESTAMP(current_timestamp()) ON UPDATE UNIX_TIMESTAMP(current_timestamp()) ;
ERROR 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 'UNIX_TIMESTAMP(current_timestamp()) ON UPDATE UNIX_TIMESTAMP(current_timestamp()' at line 2
I also tried default value to be:
'UNIX_TIMESTAMP ON UPDATE UNIX_TIMESTAMP'
but I got the same sql error anyway.
How do I achieve my goal so that ON UPDATE will use the current millisecond value?

default value in mysql must a be literal , only exception is using CURRENT_TIMESTAMP() .
https://dev.mysql.com/doc/refman/5.6/en/data-type-defaults.html#data-types-defaults-explicit
unix_timestamp(CURRENT_TIMESTAMP()) is different than CURRENT_TIMESTAMP()
If you want milli-seconds or microsecond you must add a precision to datetime and current_timestamp .
You table will become this with a precision of 4 digits .
CREATE TABLE `projects` (
`id` char(36) NOT NULL,
`name` varchar(45) NOT NULL,
`description` varchar(450) NOT NULL,
`active` tinyint(1) NOT NULL DEFAULT '1',
`created_at` datetime(4) NOT NULL DEFAULT CURRENT_TIMESTAMP(4),
`updated_at` datetime(4) NOT NULL DEFAULT CURRENT_TIMESTAMP(4) ON UPDATE CURRENT_TIMESTAMP(4),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ;
A example with sqlfiddle ( http://sqlfiddle.com/#!9/3a85ff/1 )

Related

Cannot convert MySQL Table from MyISAM to InnoDB

I am trying to convert a MySQL Table from MyISAM to InnoDB.
I use the following command:
ALTER TABLE `wp_wpr_rucss_used_css` ENGINE=InnoDB
But get the following error:
#1067 - Invalid default value for 'modified'
Why? How to solve it?
Update
I run the following command
SHOW CREATE TABLE `wp_wpr_rucss_used_css`
and
get the following result:
CREATE TABLE `wp_wpr_rucss_used_css` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`url` varchar(2000) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '',
`css` longtext COLLATE utf8mb4_unicode_520_ci,
`unprocessedcss` longtext COLLATE utf8mb4_unicode_520_ci NOT NULL,
`retries` tinyint(1) NOT NULL DEFAULT '1',
`is_mobile` tinyint(1) NOT NULL DEFAULT '0',
`modified` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`last_accessed` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `url` (`url`(150),`is_mobile`),
KEY `modified` (`modified`),
KEY `last_accessed` (`last_accessed`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci
Seems you have 0000-00-00 00:00:00 as the default value So
generally, The problem is because of sql_modes. Please check your current sql_modes by command:
show variables like 'sql_mode';
remove the sql_mode "NO_ZERO_IN_DATE,NO_ZERO_DATE" if you have. Then run your alter command.

MySQL 8.0.13: Default Value as uuid not working

I am trying to set the Default value as UUID() in MySQL version 8.0.13. But upon successful execution, the default value resets to NOT NULL.
MySQL version:
Here is my CREATE TABLE script
CREATE TABLE `session` (
`id` binary(16) NOT NULL DEFAULT (UUID_TO_BIN(UUID(), TRUE)),
`start_timestamp` timestamp NOT NULL,
`end_timestamp` timestamp NULL DEFAULT NULL,
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`status` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
the log output on table generation,
SQL script was successfully applied to the database.
The TABLE definition post execution:
CREATE TABLE `session` (
`id` binary(16) NOT NULL,
`start_timestamp` timestamp NOT NULL,
`end_timestamp` timestamp NULL DEFAULT NULL,
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`status` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
I am not able to figure out why this could happen when documentation clearly mentions that parenthesis enclosed functions are allowed.
This is unfortunately a bug with default expressions for primary key columns, Expression Default is made NULL during CREATE TABLE query, if field is made PK.
It is fixed in MySQL 8.0.19:
For a column defined as a PRIMARY KEY in a CREATE TABLE statement, a default value given as an expression was ignored. (Bug #29596969, Bug #94668)
As a workaround (if you cannot upgrade), you can add the primary key afterwards with an ALTER TABLE-statement:
CREATE TABLE `session` (
`id` binary(16) NOT NULL DEFAULT (UUID_TO_BIN(UUID(), TRUE)),
`start_timestamp` timestamp NOT NULL,
`end_timestamp` timestamp NULL DEFAULT NULL,
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`status` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
ALTER TABLE `session` ADD PRIMARY KEY(`id`);
I needed the column to not be a binary one. So, in my case, I declared it like this:
`id` char(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT (UUID()),
For anyone who needs it to store UUIDs with default values in a char column.

Invalid default value for 'timestamp'

i am getting error in my database. i am encountering invalid default value for timestamp.
here's my database:
CREATE TABLE IF NOT EXISTS `post` (
`id` int(11) NOT NULL,
`text` varchar(10000) NOT NULL,
`threadId` int(11) NOT NULL,
`userId` int(11) NOT NULL,
`dateCreated` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`timestamp` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`isModified` tinyint(4) NOT NULL DEFAULT '0'
) ENGINE=InnoDB AUTO_INCREMENT=171 DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `category` (
`id` int(11) NOT NULL,
`name` varchar(100) NOT NULL,
`timestamp` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`color` varchar(10) DEFAULT '#00bcd4',
`icon` varchar(100) NOT NULL DEFAULT 'https://mymonas.com/forum/category_icon/ic_question.png'
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;
I was having the same problem, I changed type from "datetime" to "timestamp" and It worked. I have mysql 5.5.52.
Mysql_error
I have the same issue in sql_mode.
Make query:
show variables like 'sql_mode' ;
You need to remove the "NO_ZERO_IN_DATE,NO_ZERO_DATE" from sql_mode.
SET sql_mode = '';
Use CURRENT_TIMESTAMP() instead CURRENT_TIMESTAMP
i.e.
CREATE TABLE IF NOT EXISTS `post` (
`id` int(11) NOT NULL,
`text` varchar(10000) NOT NULL,
`threadId` int(11) NOT NULL,
`userId` int(11) NOT NULL,
`dateCreated` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP(),
`timestamp` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP(),
`isModified` tinyint(4) NOT NULL DEFAULT '0'
) ENGINE=InnoDB AUTO_INCREMENT=171 DEFAULT CHARSET=latin1;
Now() works as well
From the MySQL 5.5 manual:
"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."
The changes in MYSQL 5.6.x that allow the functionality are documented here:
"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."
So, this means you are using an older version of mysql, either you can use datetime data type of upgrade your mysql version
Answered by #max Sherbakov worked but I think its risky,
if you execute SET sql_mode = ''; query.
Because if you or other users SET any different variables in sql_mode
like NO_ENGINE_SUBSTITUTION check other SQL MODES
by changing sql_mode values in my.ini file
OR
using SET sql_mode = 'YOUR_VARIABLE_LIST'; query
it worked for you current situation
but create problem in other projects.
To view current sql mode use following query
show variables like 'sql_mode' ;

Create table with default values giving error

Using MySql Workbench 6.3 Build version 6.3.6.
I am trying to create a table with Default constraint but its giving me error.
Here is the script
Create Table `Migration_Log2` (
`Id` Int NOT NULL AUTO_INCREMENT,
`FilePath` varchar(1000) NOT NULL,
`FileName` varchar(100) NOT NULL,
`IsSent` bool NOT NULL DEFAULT '0',
`CreatedDate` DateTime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`ModifiedDate` DateTime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`SendAttemptMade` int NOT NULL DEFAULT '0',
`Message` Text DEFAULT NULL,
PRIMARY KEY (`Id`),
KEY `migration_log_Id_UNIQUE` (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
Error Message
Error Code: 1067. Invalid default value for 'CreatedDate' 0.000 sec
This may be due to some strict constraint on the data type check on database server.
I would suggest to change type of field CreatedDate from datetime to timestamp.
I had faced similar issue in a VPS for my website.
Your CURRENT_TIMESTAMP might have been appending the microseconds in the output.
Try to use: CURRENT_TIMESTAMP(0) as the default value.
SELECT CURRENT_TIMESTAMP, CURRENT_TIMESTAMP(0), CURRENT_TIMESTAMP(1), CURRENT_TIMESTAMP(2);
The microseconds mattered, possibly. See the differences.

MYSQL script import error

I am getting an error when I try to import data through the command:
mysql -u root -p"root" cvdb < "cvdb.sql"
The error I am getting is:
ERROR 1064 (42000) at line 72: 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 '(14) NOT NULL,
Created datetime NOT NULL default ' 0000-00-00 00:00:00 ',
' at line 14
The code of my SQL file is:
DROP TABLE IF EXISTS `activity`;
CREATE TABLE `activity` (
`AllDay` enum('YES','NO') default 'NO',
`ActivityID` int(11) unsigned NOT NULL auto_increment,
`Type` int(11) NOT NULL default '0',
`Priority` int(11) NOT NULL default '0',
`Status` int(11) NOT NULL default '0',
`Title` varchar(255) default NULL,
`DueDate` datetime default NULL,
`CompletedDate` datetime default NULL,
`Details` text,
`Creator` int(11) NOT NULL default '0',
`Owner` int(11) default NULL,
`ModifiedBy` int(11) default NULL,
`Modified` timestamp(14) NOT NULL,
`Created` datetime NOT NULL default ' 0000-00-00 00:00:00 ',
`Start` datetime default NULL,
`End` datetime default NULL,
`AttachmentType` enum('NONE','FILE','LINK') NOT NULL default 'NONE',
`Location` varchar(25) default NULL,
`visibility` enum('PRIVATE','PUBLIC') NOT NULL default 'PRIVATE',
`Notes` varchar(255) default NULL,
PRIMARY KEY (`ActivityID`),
UNIQUE KEY `ActivityID` (`ActivityID`),
KEY `Type` (`Type`),
KEY `Priority` (`Priority`),
KEY `Status` (`Status`),
KEY `Creator` (`Creator`),
KEY `Owner` (`Owner`),
KEY `ModifiedBy` (`ModifiedBy`),
KEY `Location` (`Location`)
) ENGINE=InnoDB;
the error is here Modified timestamp(14) NOT NULL. you should remove (14) from timestamp.
your partial DDL,
`ModifiedBy` int(11) default NULL,
`Modified` timestamp NOT NULL,
`Created` datetime NOT NULL default '0000-00-00 00:00:00',
`Start` datetime default NULL,
From mysql doc
Incompatible change: In very old versions of MySQL (prior to 4.1), the
TIMESTAMP data type supported a display width, which was silenty
ignored beginning with MySQL 4.1. This is deprecated in MySQL 5.1, and
removed altogether in MySQL 5.5. These changes in behavior can lead to
two problem scenarios when trying to use TIMESTAMP(N) columns with a
MySQL 5.5 or later server:
...
You should try to handle potential issues of these types proactively
by updating with ALTER TABLE any TIMESTAMP(N) columns in your
databases so that they use TIMESTAMP instead, before performing any
upgrades.
So try to remove the (14)
in
Modified timestamp(14) NOT NULL
or as said in doc, try to make alter table statements on your timestamp columns before import.