ERROR 1067 (42000) Invalid default value '0000-00-00 00:00:00' - mysql

Importing an SQL file via SSH on Ubuntu server using SQL Ver 14.14 Distrib 5.7.20, for Linux (x86_64) using EditLine wrapper.
The line that the error points to is this line:
CREATE TABLE `wp_users` (
The line that has user_registered is this line:
`user_registered` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
Not sure what is wrong.
The whole CREATE TABLE command as requested:
CREATE TABLE `wp_users` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_login` varchar(60) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_pass` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_nicename` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_email` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_url` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_registered` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`user_activation_key` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_status` int(11) NOT NULL DEFAULT '0',
`display_name` varchar(250) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`ID`),
KEY `user_login_key` (`user_login`),
KEY `user_nicename` (`user_nicename`),
KEY `user_email` (`user_email`)
) ENGINE=MyISAM AUTO_INCREMENT=53 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ;

I guess you're using MySQL 5.7 (or higher). There the default value for SQL mode is among others NO_ZERO_IN_DATE,NO_ZERO_DATE.
You can check this with
SHOW GLOBAL VARIABLES LIKE 'sql_mode';
/* or...*/
SHOW SESSION VARIABLES LIKE 'sql_mode';
Either you remove above mentioned values from the sql mode, or you just make your column nullable and store NULL instead of 0000-00-00 00:00:00. I'd prefer the latter. It doesn't waste storage and is clear in its meaning.

Related

Error database import from localhost to server

I want to upload database from localhost to server and I get 1067 error.
Localhost Mysql version - 5.7
Server - MariaDB, v5.5.68
Code part
CREATE TABLE `wp_momopay_payments` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`order_id` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT '',
`amount` int(10) unsigned DEFAULT '0',
`phone_number` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT '',
`created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`payment_id` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT '',
`failed_reason` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT '',
`salt` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT '',
`status` varchar(36) COLLATE utf8mb4_unicode_ci DEFAULT 'pending',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Error message
ERROR 1067 (42000) at line 1721: Invalid default value for 'created'
program 'mysql' finished with non-zero exit code: 1
Mariadb 5.5 only allows TIMESTAMP, not DATETIME, columns to have the CURRENT_TIMESTAMP default value. So your table definition uses a feature it lacks.
Mariadb 5.5 hit end-of-life on 11-April-2020, well over two years ago.

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.

Why is sql not letting me create this table?

I'm trying to migrate a db
from: MySQL Distrib 5.5.60-MariaDB, for Linux (x86_64)
to: MySQL 5.5.4, UNIX
I tried importing the db as a zip package and it started throwing errors so now I'm trying to re-create each table one at a time on phpMyAdmin.
The query below is throwing a #1064 Syntax error, and I'm having trouble figuring out the issue.
MySQL Said:
#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 '(2) NOT NULL DEFAULT CURRENT_TIMESTAMP(2) ON UPDATE CURRENT_TIMESTAMP(2),
`st' at line 6
I'm looking at line 6, trying to find any reserved words, missing data, typos, and or obsolete commands but no luck.
CREATE TABLE `tblmoto_auth_policies` (
`policy_id` int(11) NOT NULL AUTO_INCREMENT,
`policy_name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`policy_desc` text COLLATE utf8_unicode_ci NOT NULL,
`policy_url` text COLLATE utf8_unicode_ci NOT NULL,
`date_added` timestamp(2) NOT NULL DEFAULT CURRENT_TIMESTAMP(2) ON UPDATE CURRENT_TIMESTAMP,
`status` smallint(2) NOT NULL DEFAULT '1',
PRIMARY KEY (`policy_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
I could use some help.
Thanks in advance.
CURRENT_TIMESTAMP is not good
Try this:
CREATE TABLE `tblmoto_auth_policies` (
`policy_id` int(11) NOT NULL AUTO_INCREMENT,
`policy_name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`policy_desc` text COLLATE utf8_unicode_ci NOT NULL,
`policy_url` text COLLATE utf8_unicode_ci NOT NULL,
`date_added` timestamp(2) NOT NULL DEFAULT CURRENT_TIMESTAMP(2) ON UPDATE CURRENT_TIMESTAMP(2),
`status` smallint(2) NOT NULL DEFAULT '1',
PRIMARY KEY (`policy_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
This works in SQL Fiddle:
CREATE TABLE `tblmoto_auth_policies` (
`policy_id` int(11) NOT NULL AUTO_INCREMENT,
`policy_name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`policy_desc` text COLLATE utf8_unicode_ci NOT NULL,
`policy_url` text COLLATE utf8_unicode_ci NOT NULL,
`date_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`status` smallint(2) NOT NULL DEFAULT '1',
PRIMARY KEY (`policy_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
i.e., remove the precision (the (2)) from the definition of the date_added column.
TIMESTAMP(2) is valid syntax, but not in combination with the DEFAULT CURRENT_TIMESTAMP nor ON UPDATE CURRENT_TIMESTAMP auto-initializers.

Error when importing to server

SQL query:
CREATE TABLE `wp_layerslider` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`author` int(10) NOT NULL DEFAULT '0',
`name` varchar(100) COLLATE utf8mb4_unicode_520_ci DEFAULT '',
`slug` varchar(100) COLLATE utf8mb4_unicode_520_ci DEFAULT '',
`data` mediumtext COLLATE utf8mb4_unicode_520_ci NOT NULL,
`date_c` int(10) NOT NULL,
`date_m` int(10) NOT NULL,
`schedule_start` int(10) NOT NULL DEFAULT '0',
`schedule_end` int(10) NOT NULL DEFAULT '0',
`flag_hidden` tinyint(1) NOT NULL DEFAULT '0',
`flag_deleted` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
MySQL said: Documentation
1273 - Unknown collation: 'utf8mb4_unicode_520_ci'
When attempting to import a database. I'm very new to all of this, hoping someone can assist in simple terms for me
could be you are using an oldeversion of mysql db .. and the collate you need is not avialable .. you could use try alter the table or change the collate
ALTER TABLE `wp_layerslider` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci

What could be wrong here

DROP TABLE IF EXISTS 'ci_sessions';
CREATE TABLE 'ci_sessions' (
'session_id' varchar(40) COLLATE utf8_bin NOT NULL DEFAULT '0',
'ip_address' varchar(16) COLLATE utf8_bin NOT NULL DEFAULT '0',
'user_agent' varchar(120) COLLATE utf8_bin DEFAULT NULL,
'last_activity' int(10) unsigned NOT NULL DEFAULT '0',
'user_data' text COLLATE utf8_bin NOT NULL, PRIMARY KEY ('session_id'),
KEY 'last_activity_idx' ('last_activity') )
ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
I've tried to run this code on phpMyAdmin and it says
#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 ''ci_sessions'' at line 1
It's the same error in all your table and column names. You use ' while you should use backticks:
`
So this should work:
DROP TABLE IF EXISTS `ci_sessions`;
CREATE TABLE `ci_sessions` (
`session_id` varchar(40) COLLATE utf8_bin NOT NULL DEFAULT '0',
`ip_address` varchar(16) COLLATE utf8_bin NOT NULL DEFAULT '0',
`user_agent` varchar(120) COLLATE utf8_bin DEFAULT NULL,
`last_activity` int(10) unsigned NOT NULL DEFAULT '0',
`user_data` text COLLATE utf8_bin NOT NULL, PRIMARY KEY (`session_id`),
KEY `last_activity_idx` (`last_activity`) )
ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
As a sidenote - you use NOT NULL and then set the default value to the string 0. quite often. That indicates bad practice, or at least more work for later queries. A column named last_activity for example indicates that null values should be allowed...
Second sidenote - last_activity would typically be a DATETIME column, not an int(10)