Datetime current_timestamp in MariaDB - mysql

I've finished the first version of my site, and I'm now ready to move it from my local development environment (MySQL) to my x10hosting server (they use MariaDB).
I'm able to move all my files over fine, but the issues started happening when I moved the database over.
I exported my database as a .sql file from phpmyadmin on my local machine, and opened phpmyadmin on x10 to import the file, but it throws this error:
#1067 - Invalid default value for 'created'
I traced the issue to it being that 'created' is a datetime type, and apparently MariaDB doesn't let you set current_timestamp as a default for datetime.
How can I use the current timestamp as the datetime default in maria?
The full create statement is:
CREATE TABLE IF NOT EXISTS `comments` (
`id` BIGINT( 10 ) NOT NULL AUTO_INCREMENT ,
`post` BIGINT( 10 ) NOT NULL ,
`author` VARCHAR( 50 ) NOT NULL ,
`comment` TEXT NOT NULL ,
`mod_status` INT( 2 ) NOT NULL DEFAULT '0' COMMENT '0 is needing moderation, 1 is approved, and 2 is rejected',
`created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ,
PRIMARY KEY ( `id` )
) ENGINE = INNODB DEFAULT CHARSET = latin1 AUTO_INCREMENT =4;

In MariaDB you must refer to DEFAULT_TIMESTAMP as a function () :
`created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP(),
^^
Then it will work. Have noticed the same peculiarity when firing INSERT statements using CURRENT_TIMESTAMP or NOW. In MySQL you can do
insert into table (created, id) values ('CURRENT_TIMESTAMP', 42)
in MariaDB it must be
insert into table (created, id) values (CURRENT_TIMESTAMP(), 42)

I guess your column is of type datetime and not timestamp, so you should use
current_date
Have a look at https://mariadb.com/kb/en/mariadb/current_date/

Related

MySql 5.7 Timestamp works in windows but not on linux

I am trying to create a table using the script down below and getting the error
Error Code: 1067. Invalid default value for 'LAST_MODIFIED_TS.
In my understanding after 5.6 you could create more than one timestamp column also it is not necessary to provide default values . Another thing is it is not barfing at Created_TS which is just one line before it.
Also the same script works on windows but not on linux ubuntu , the version of mysql running on both of them is 5.7
CREATE TABLE testdb.test (
ID BIGINT NOT NULL AUTO_INCREMENT,
DESCRIPTION VARCHAR(300) NOT NULL,
CREATED_TS TIMESTAMP NOT NULL,
LAST_MODIFIED_TS TIMESTAMP NOT NULL,
PROPERTY_TYPE VARCHAR(1) DEFAULT 'S',
last_modified timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
CONSTRAINT test_pk PRIMARY KEY
(ID) ) ENGINE=InnoDB ;
Make sure you don't have NO_ZERO_DATE sql_mode variable set (http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_no_zero_date) by running the following query:
show variables like 'sql_mode';

invalid default value for variable last_updated in the below code

why do i get invalid default value error for the variable "last_updated"?
note i am getting this error while i run the following code in MySQL console in phpmyadmin
CREATE TABLE IF NOT EXISTS `articles` (
`article_id` int(10) NOT NULL AUTO_INCREMENT,
`content_id` int(10) NOT NULL,
`article_body` text NOT NULL,
`last_updated` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`article_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=532 ;
You need to change the last_updated column's data type to timestamp rather than datetime. This will allow the use of CURRENT_TIMESTAMP as a default value.
As it happens, these two data types are represented in the same format YYYY-MM-DD HH:MM:SS. So if/when you use the data, you shouldn't run into any troubles.
Check your MySQL server version, CURRENT_TIMESTAMP is allowed since version 5.6.5 as DEFAULT for DATETIME type, otherwise you should use either TIMESTAMP type or maintain it outside.

MySQL Error "There can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT clause" even though I'm doing nothing wrong

CREATE TABLE AlarmHistory
(
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
value DOUBLE NOT NULL,
startedStamp TIMESTAMP NOT NULL,
finishedStamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
);
When trying to create the above table I get the following error: "SQL Error (1293): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause".
My question is this a bug? Because sure, I have two TIMESTAMP columns, but only ONE of them have a default definition. When I remove startedStamp I have no errors.
Per the MySQL manual, version 5.5, Automatic Initialization and Updating for TIMESTAMP
With neither DEFAULT CURRENT_TIMESTAMP nor ON UPDATE CURRENT_TIMESTAMP, it is the same as specifying both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP.
CREATE TABLE t1 (
ts TIMESTAMP
);
However,
With a constant, the default is the given value. In this case, the column has no automatic properties at all.
CREATE TABLE t1 (
ts TIMESTAMP DEFAULT 0
);
So, this should work:
CREATE TABLE AlarmHistory
(
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
value DOUBLE NOT NULL,
startedStamp TIMESTAMP DEFAULT 0 NOT NULL,
finishedStamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
);
fiddle
This is the limitation in MYSQL 5.5 version. You need to update the version to 5.6.
I was getting this error in adding a table in MYSQL
Incorrect table definition; there can be only one TIMESTAMP column
with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause My new MYSQL
table looks something like this.
create table table_name (col1 int(5) auto_increment primary key, col2
varchar(300), col3 varchar(500), col4 int(3), col5 tinyint(2),
col6 timestamp default current_timestamp, col7 timestamp default
current_timestamp on update current_timestamp, col8 tinyint(1)
default 0, col9 tinyint(1) default 1);
After some time of reading about changes in different MYSQL versions and some of the googling. I found out that there was some changes that were made in MYSQL version 5.6 over version 5.5.
This article will help you to resolve the issue.
http://www.oyewiki.com/MYSQL/Incorrect-table-definition-there-can-be-only-one-timestamp-column

timestamp to use specific timezone?

The below is my table definition that automatically inserts timestamp value into each record. All i want to do is let the timestamp use a specific timezone for example in my case i want it to use the current time of British Columbia How do i do that ?
Because when i insert data in the table i do not really pass a timestame value from my php script but it takes the default value.
CREATE TABLE `cfv_postbusupdate` (
`BusNumber` int(11) NOT NULL,
`Direction` varchar(100) DEFAULT 'Not Provided',
`StopNames` varchar(300) DEFAULT 'Not Provided',
`Status` varchar(45) DEFAULT 'Not Provided',
`comments` varchar(150) DEFAULT 'None',
`username` varchar(45) DEFAULT 'anonymous_user',
`dayofweek` varchar(45) DEFAULT NULL,
`time` varchar(20) DEFAULT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`DatePosted` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
Timestamps are stored as UTC values ad converted on retrieval to the time zone in use by the application. If you want British Columbia time set the time zone with SET time_zone = 'america/vancouver';
The manual reference is here
use this one: refer this link:https://stackoverflow.com/a/6158432/3242978
-- Make sure we're all working off of the same local time zone
test=> SET timezone = 'America/Los_Angeles';
SET
test=> SELECT NOW();
now
-------------------------------
2011-05-27 15:47:58.138995-07
(1 row)
test=> SELECT NOW() AT TIME ZONE 'UTC';
timezone
----------------------------
2011-05-27 22:48:02.235541
(1 row)
update: 1
According to MySQL docs, the error #1193 occurs when you use wrong code for SQLSTATE.
Message: Unknown system variable %s
And, as you can see on the same page, the SQLSTATE 99003 is not defined.

MySQL syntax error in the CREATE TABLE statement

SQL query:
CREATE TABLE `comment_threads` (
`comment_id` INT( 11 ) UNSIGNED NOT NULL DEFAULT '0',
`updated` TIMESTAMP NOT NULL ,
`timestamp` TIMESTAMP NOT NULL ,
) ENGINE = MYISAM ;
This is an old file that I'm trying to run on HostGator through phpMyAdmin. I get an error that says:
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 ') ENGINE=MyISAM' at line 5
UPDATE:
If I change the statement to this, I still get an error:
CREATE TABLE comment_threads (
comment_id INT( 11 ) UNSIGNED NOT NULL DEFAULT '0',
updated TIMESTAMP( 14 ) NOT NULL ,
timestamp TIMESTAMP NOT NULL
PRIMARY KEY ( comment_id ) )
ENGINE = MYISAM ;
I get the error:
ERROR 1064 (42000): 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 ,
timestamp TIMESTAMP NOT NULL
PRIMARY KEY ( comment_id ) )
ENGI' at line 3
Your MySQL query is incorrect. Correcting it to this works.
CREATE TABLE `comment_threads` (
`comment_id` INT(11) UNSIGNED NOT NULL DEFAULT '0',
`updated` TIMESTAMP NOT NULL ,
`timestamp` TIMESTAMP NOT NULL
) ENGINE=MyISAM;
To run this in phpMyAdmin, you can use the in-built table creator or enter the corrected SQL statement in the SQL query window.
Note that I removed the comma after the last TIMESTAMP NOT NULL line (immediately before the ending ).
UPDATE:
The second statement you posted corrects to this:
CREATE TABLE `comment_threads` (
`comment_id` INT(11) UNSIGNED NOT NULL DEFAULT '0',
`updated` TIMESTAMP NOT NULL ,
`timestamp` TIMESTAMP NOT NULL,
PRIMARY KEY(`comment_id`)
) ENGINE=MyISAM;
Here are the problems you introduced in your second CREATE TABLE statement:
TIMESTAMP( 14 ) should just be TIMESTAMP (per the documentation)
You need a comma after the line TIMESTAMP NOT NULL line. The comma is necessary now because unlike in the first example, you're separated two parts of the statement: the TIMESTAMP NOT NULL line and the PRIMARY KEY declaration.
If you want more information on simple methods for debugging SQL statements, I strongly suggest you look at my answer to this question (see the section titled A bit more information on how to methodically fix errors like this).
Make sure that when you change a CREATE TABLE statement, or any piece of code, that you make changes in small enough increments that you're only "breaking at most one thing at a time." In the case of your second CREATE TABLE statement, there was no reason to change the first TIMESTAMP declaration, and doing so broke the code. That line was working; no need to change it.