ERROR 1067 (42000): Invalid default value for 'date_time' in MySQL - 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.

Related

MySQL - SQLite - Converting is spitting out SET error

I am trying to convert the following sql into sqlite via sqlite3.exe but it keeps giving me this error:
Error: near line 1: near "SET": syntax error
I'm not entirely sure what this means or why. Here is my sql script:
SET SQL_MODE = 'NO_AUTO_VALUE_ON_ZERO',
time_zone = '+00:00';
CREATE TABLE `px` (
`x` int(11) NOT NULL,
`y` int(11) NOT NULL,
`colour` varchar(30) NOT NULL,
`time` varchar(30) NOT NULL,
`id` int(10) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `px`
ADD PRIMARY KEY (`id`);
ALTER TABLE `px`
MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;
I'm sure i'm overlooking a simple syntax error. Any help would be much appreciated!
You can 't just run a MySQL script in SQLite. These are two different databases, whose syntax differ.
In SQLite, a relatively close syntax to the MySQL script would be:
CREATE TABLE `px` (
`x` integer NOT NULL,
`y` integer NOT NULL,
`colour` varchar(30) NOT NULL,
`time` varchar(30) NOT NULL,
`id` integer NOT NULL PRIMARY KEY AUTOINCREMENT
);

Export from localhost to remote on the same LAN

I want to export one of my database to my Raspberry. I tried through PHPMyadmin, but on import I got this errormessage:
SQL query:
--
-- Database: `leltar`
--
-- --------------------------------------------------------
--
-- Table structure for table `eventlog`
--
CREATE TABLE `eventlog` (
`ID` int(50) NOT NULL,
`event` varchar(50) COLLATE utf8_hungarian_ci NOT NULL,
`productbc` varchar(50) COLLATE utf8_hungarian_ci DEFAULT NULL,
`uname` varchar(50) COLLATE utf8_hungarian_ci NOT NULL,
`datetime` datetime(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_hungarian_ci;
MySQL said: Documentation
#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 '(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_hungarian_ci' at line 16
What's the matter?
Your datetime field has fractional seconds specified: datetime(1).
This feature is probably not supported by the target mysql version (< v5.7), hence the error message.
You either need to upgrade your target mysql version to support fractional seconds or you need to remove the fractional seconds from the export file, both from the data definition and the data itself.
CREATE TABLE `eventlog` (
`ID` int(50) NOT NULL,
`event` varchar(50) COLLATE utf8_hungarian_ci NOT NULL,
`productbc` varchar(50) COLLATE utf8_hungarian_ci DEFAULT NULL,
`uname` varchar(50) COLLATE utf8_hungarian_ci NOT NULL,
`datetime` DATETIME NOT NULL
) ;
Remove the "(1)" after the word "datetime" because it needs no length (sorry, it's not "length" it is a fractional seconds specification). The following should work:
CREATE TABLE `eventlog` (
`ID` int(50) NOT NULL,
`event` varchar(50) COLLATE utf8_hungarian_ci NOT NULL,
`productbc` varchar(50) COLLATE utf8_hungarian_ci DEFAULT NULL,
`uname` varchar(50) COLLATE utf8_hungarian_ci NOT NULL,
`datetime` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_hungarian_ci;

Mysql i get Error when i try to edit after insert data

this is my one of database table structure and after insert some fields as row into that i can't update columns and i get error:
CREATE TABLE `channels` (
`id` int(11) NOT NULL,
`channelName` varchar(30) COLLATE utf8_persian_ci NOT NULL,
`channelLink` varchar(50) COLLATE utf8_persian_ci NOT NULL,
`channelDescription` varchar(100) COLLATE utf8_persian_ci NOT NULL,
`channelImageFileName` varchar(100) COLLATE utf8_persian_ci NOT NULL,
`channelAvatarFileName` varchar(100) COLLATE utf8_persian_ci NOT NULL,
`channelState` tinyint(1) NOT NULL,
`channelType` enum('channels','userCreatedChannels') COLLATE utf8_persian_ci NOT NULL,
`channelOwnerUserId` int(11) NOT NULL,
`fileServerUrlId` int(11) NOT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `channels`
--
ALTER TABLE `channels`
ADD PRIMARY KEY (`id`);
Error and notice:
This table does not contain a unique column. Features related to the grid edit, checkbox, Edit, Copy and Delete links may not work after saving.
Error
UPDATE `channels` SET `channelState` = '1' WHERE LIMIT 1
MySQL said: Documentation
#1064 - 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 'LIMIT 1' at line 1
id of this table is unique and A_I, how can i resolve this problem?

How to create mysql table with column timestamp default current_date?

I need to create mysql table with default value on column CURRENT_DATE()
I try
DROP TABLE IF EXISTS `visitors`;
CREATE TABLE `visitors` (
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`ip` VARCHAR(32) NOT NULL,
`browser` VARCHAR(500) NOT NULL,
`version` VARCHAR(500) NOT NULL,
`platform` ENUM('w','l','m') NOT NULL,
`date` TIMESTAMP NOT NULL DEFAULT CURRENT_DATE(),
PRIMARY KEY (`id`),
UNIQUE KEY `person` (`ip`,`date`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
but it is writting an error
Query: CREATE TABLE `visitors` ( `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, `ip` VARCHAR(32) NOT NULL, `browser` VARCHAR(500) NO...
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 'CURRENT_DATE() NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `person` (`ip`,`date`)
) ENGINE=INNODB AUTO_INCREMENT=1' at line 7
Execution Time : 0 sec
Transfer Time : 0 sec
Total Time : 0.063 sec
what is the problem?
I need to uniques only date
not with full time info...
can you help me?
Use CURRENT_TIMESTAMP function instead of CURRENT_DATE() function
Try this:
DROP TABLE IF EXISTS `visitors`;
CREATE TABLE `visitors` (
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`ip` VARCHAR(32) NOT NULL,
`browser` VARCHAR(500) NOT NULL,
`version` VARCHAR(500) NOT NULL,
`platform` ENUM('w','l','m') NOT NULL,
`date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `person` (`ip`,`date`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
you just have to replace CURRENT_DATE() by NOW() in your query.
I tried it and it's look ok.
This may be a little late but I think it might be helpful to someone else.
My approach has been to use getdate() like this:
[TimeStamp] [datetime] NOT NULL CONSTRAINT [DF_myTable_TimeStamp] DEFAULT (getdate()).
Where [TimeStamp] is the column in question.
The result is for example: 2017-11-02 11:58:34.203
To trim this, I use the following
declare #mydate datetime
set #mydate = '2017-11-02 11:58:34.203'
SELECT try_convert(nvarchar(20), #mydate, 120)
This final result is: 2017-11-02 11:58:34
You can actually set this in MSSQL Management Studio

Got the ERROR 1046* CREATE TABLE IF NOT EXISTS `activity` (

Table structure for table activity
CREATE TABLE IF NOT EXISTS `activity` (
`fnum` int(10) unsigned NOT NULL auto_increment,
`fid` int(25) default NULL,
`fdate` datetime default NULL,
`ftask` varchar(50) default NULL,
`username` varchar(255) NOT NULL default '',
PRIMARY KEY (`fnum`),
KEY `username` (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1204 ;
MySQL said:
1046 - No database selected
As the error says "1046 - No database selected". You should start your script with selecting a database. E.g.:
use my_database;