#1067 - Invalid default value for 'timestamp' - mysql

I transferred a WordPress website from a shared hosting plan using Plesk to a VPS server with a newer Plesk version. When trying to upload the MYSQL database I am getting the following error:
Error
SQL query: Copy
CREATE TABLE `XZu2B8_wc_reserved_stock` (
`order_id` bigint(20) NOT NULL,
`product_id` bigint(20) NOT NULL,
`stock_quantity` double NOT NULL DEFAULT 0,
`timestamp` datetime NOT NULL DEFAULT current_timestamp(),
`expires` datetime NOT NULL DEFAULT current_timestamp(),
PRIMARY KEY (`order_id`,`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
MySQL said: Documentation
#1067 - Invalid default value for 'timestamp'
Does anyone have any idea how to fix that?

This was introduced only in mysql 5.6
For mysql 5.5 you need following trigger and you need to remove the DEFAULTs
FIRST create the table:
CREATE TABLE `XZu2B8_wc_reserved_stock` (
`order_id` bigint(20) NOT NULL,
`product_id` bigint(20) NOT NULL,
`stock_quantity` double NOT NULL DEFAULT 0,
`timestamp` datetime NOT NULL,
`expires` datetime NOT NULL ,
PRIMARY KEY (`order_id`,`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
Then create the trigger
DELIMITER $$
CREATE TRIGGER before_insert_XZu2B8_wc_reserved_stock
BEFORE INSERT ON XZu2B8_wc_reserved_stock
FOR EACH ROW
BEGIN
IF NEW.`timestamp` IS NULL THEN
SET NEW.`timestamp`= NOW();
END IF;
IF NEW.`expires` IS NULL THEN
SET NEW.`expires`= NOW();
END IF;
END$$
DELIMITER ;

Related

my trigger error in mysql, hen help to fix

I can't understand why my trigger doesn't work.
first table:
CREATE TABLE `Payment` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`idPaymentMethod` int(20) DEFAULT NULL,
`createAt` timestamp NOT NULL DEFAULT current_timestamp(),
`updateAt` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE current_timestamp(),
`actived` tinyint(1) NOT NULL DEFAULT 1,
`numberInvoices` int(11) DEFAULT NULL,
`preferentialPaymentDay` int(11) DEFAULT NULL,
`invoicesFrequency` bigint(20) NOT NULL DEFAULT 0,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
second table:
CREATE TABLE `PaymentHistory` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`idPaymentMethod` int(20) DEFAULT NULL,
`idPayment` int(20) DEFAULT NULL,
`createAt` timestamp NOT NULL DEFAULT current_timestamp(),
`updateAt` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE current_timestamp(),
`actived` tinyint(1) NOT NULL DEFAULT 1,
`numberInvoices` int(11) DEFAULT NULL,
`preferentialPaymentDay` int(11) DEFAULT NULL,
`invoicesFrequency` bigint(20) NOT NULL DEFAULT 0,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
my trigger:
DELIMITER $$
CREATE
TRIGGER `update_history` AFTER
UPDATE
ON
`Payment`
FOR EACH ROW BEGIN
UPDATE
`PaymentHistory`
SET
id = OLD.id,
idPaymentMethod = old.idPaymentMethod,
createAt = old.createAt,
updateAt = old.updateAt,
actived = old.actived,
numberInvoices = old.numberInvoices
END$$
I get a lot of errors when I try to fix something.
Errors I get:
SQL Error [1064] [42000]: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'END$$' at line 1
SQL Error [1054] [42S22]: Unknown column 'OLD.id' in 'field list'
I am really lost.
I need all the updated data in the "Payment" table to be inserted or updated into the "PaymentHistory" table
Plase, i need a help.
You need to finish each command with a semicolon
DELIMITER $$
CREATE
TRIGGER `update_history` AFTER
UPDATE
ON
`Payment`
FOR EACH ROW BEGIN
UPDATE
`PaymentHistory`
SET
id = OLD.id,
idPaymentMethod = old.idPaymentMethod,
createAt = old.createAt,
updateAt = old.updateAt,
actived = old.actived,
numberInvoices = old.numberInvoices;
END$$
DELIMITER ;
See example
first, thanks to #nbk
i solved with yours help
i change de "after" to "before" and add the ";"
see;
TRIGGER `update_history` BEFORE
UPDATE
ON
`Payment`
FOR EACH ROW BEGIN
UPDATE
`PaymentHistory`
SET
idPayment = old.id
, idPaymentMethod = old.idPaymentMethod
, createAt = old.createAt
, updateAt = old.updateAt
, actived = old.actived
, numberInvoices = old.numberInvoices;
END

ERROR 1067 (42000): Invalid default value for 'date_time' in MySQL

I am fairly new and was trying to upload a sql dump file which was working perfectly on my local machine, into a cloud 9. But on importing the file i am getting this error
ERROR 1067 (42000): Invalid default value for 'date_time'
The sql command in the dump file thats giving this error is
--
-- Table structure for table `history`
--
CREATE TABLE `history` (
`id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`action` varchar(4) NOT NULL,
`symbol` varchar(20) NOT NULL,
`company` varchar(255) NOT NULL,
`shares` int(11) NOT NULL,
`price` decimal(65,4) NOT NULL,
`total` decimal(65,4) NOT NULL,
`date_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `history`
--
any help would be appreciated
You cannot use CURRENT_TIMESTAMP on update. Instead, change it to a TIMESTAMP.

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.

creating a trigger - declare variable - Cant get my trigger to work

This trigger is designed to update 'field_csvfilepath_value' to match 'filepath' in the files table (some table details below). But I cant get it to work, please help.
delimiter $$
CREATE TRIGGER csv_filpath
AFTER INSERT ON content_type_importcsv for each row
begin
declare p varchar(80)
set p := (SELECT filepath FROM content_type_importcsv join files where NEW.content_type_importcsv.field_csv1_fid = files.fid)
set NEW.field_csvfilepath_value = p
end$$
delimiter ;
This trigger is generating the following error:
Error Code: 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 'set p := (SELECT filepath FROM content_type_importcsv join files where NEW.conte' at line 5
I'm using mysql workbench 5.2
delimiter $$
delimiter $$
CREATE TABLE `content_type_importcsv` (
`vid` int(10) unsigned NOT NULL DEFAULT '0',
`nid` int(10) unsigned NOT NULL DEFAULT '0',
`field_csv1_fid` int(11) DEFAULT NULL,
`field_csv1_list` tinyint(4) DEFAULT NULL,
`field_csv1_data` text,
`field_csvfilepath_value` longtext,
PRIMARY KEY (`vid`),
KEY `nid` (`nid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8$$
CREATE TABLE `files` (
`fid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`uid` int(10) unsigned NOT NULL DEFAULT '0',
`filename` varchar(255) NOT NULL DEFAULT '',
`filepath` varchar(255) NOT NULL DEFAULT '',
`filemime` varchar(255) NOT NULL DEFAULT '',
`filesize` int(10) unsigned NOT NULL DEFAULT '0',
`status` int(11) NOT NULL DEFAULT '0',
`timestamp` int(10) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`fid`),
KEY `uid` (`uid`),
KEY `status` (`status`),
KEY `timestamp` (`timestamp`)
) ENGINE=MyISAM AUTO_INCREMENT=55 DEFAULT CHARSET=utf8$$
Apart from multiple syntax errors in your code you can't change column values of a row being inserted in AFTER trigger. You should use BEFORE event instead.
That being said your trigger can be boiled down to one-statement and therefore doesn't need BEGIN ... END block and change of a delimiter.
CREATE TRIGGER csv_fillpath
BEFORE INSERT ON content_type_importcsv
FOR EACH ROW
SET NEW.field_csvfilepath_value =
(
SELECT filepath
FROM files
WHERE fid = NEW.field_csv1_fid
LIMIT 1
);
Here is SQLFiddle demo
There are some syntax errors:
The one you listed is caused by the : in set p :=, you should remove it
Each line between begin and end$$ should end with ;
You can't update a NEW row in an after trigger, so it should be a before trigger