I am trying to create mysql table in PhpMyAdmin in Hostgator server with the following information but it is showing error
Error : #1067 - Invalid default value for 'CreatedDate'
Table
CREATE TABLE `tbl_sample` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Domain` varchar(100) DEFAULT NULL,
`ClickUrl` varchar(600) DEFAULT NULL,
`CreatedDate` datetime NULL DEFAULT now(),
`ModifyDate` timestamp NULL DEFAULT NULL on update now(),
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The purpose of table is I need to enter only 'Domain' and 'ClickUrl' data using insert command remaining Id(autoincrement),CreatedDate(current date when inserting row),ModifyDate(when update the row) will automatically insert.
The above table is executed successfully in mysql environment in my local system but it is not executing in the mysql environment in hostgator
Check this link]1
Problem with creating Two column with timestamps. have to use trigger to get it done.
Related
We use two servers (devsrv & qualifsrv), one for development and one for the qualification of our applications.
I have exactly the same Zend application on both servers (files and configuration of the application are the same).
I have a log table in which I insert the treatments carried out and the time required to achieve them.
When I insert a row into my table "Log" on the server devsrv, no problem.
When I execute exactly the same query in the table "Log" on the qualifsrv server, MySQL returns an error stating that in 1292 the value for the log_duration field is incorrect.
My log table :
CREATE TABLE `T_log` (
`log_id` int(11) NOT NULL AUTO_INCREMENT,
`log_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`log_priority` varchar(10) DEFAULT NULL,
`log_priority_name` varchar(20) DEFAULT NULL,
`log_event` varchar(255) DEFAULT NULL,
`log_commentaire` longtext,
`log_impacted_row` int(10) DEFAULT NULL,
`log_duration` time DEFAULT NULL,
`vag_id` int(11) DEFAULT NULL,
PRIMARY KEY (`log_id`),
KEY `idx_log_priority` (`log_priority`) USING BTREE,
KEY `idx_log_date` (`log_date`) USING BTREE,
KEY `idx_log_event` (`log_event`) USING BTREE,
KEY `idx_impacted_row` (`log_impacted_row`) USING BTREE,
KEY `fk_vag_id` (`vag_id`) USING BTREE,
CONSTRAINT `T_log_ibfk_1` FOREIGN KEY (`vag_id`) REFERENCES `T_vague` (`vag_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=4479 DEFAULT CHARSET=utf8;
The query :
INSERT INTO `T_log` (
`log_priority`,
`log_priority_name`,
`log_event`,
`log_commentaire`,
`log_impacted_row`,
`log_duration`,
`vag_id`
)
VALUES
(
6,
"INFO",
"TEST",
"UPDATE",
87552,
"20s",
15
)
When I insert on devsrv :
Affected rows: 1
(log_duration value is : 00:00:20 in Log table)
When I insert on qualifsrv :
[Err] 1292 - Incorrect time value: '20s' for column 'log_duration' at row 1
Why this difference in behavior between the two servers ?
I found the difference. It is because of the globale variable sql_mode that was configured to not allow other value than the one expected by the field.
More informations here : http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html
This is the query I have used for creating the table
CREATE TABLE IF NOT EXISTS `logging_api_request_js` (
`id` int(30) unsigned NOT NULL AUTO_INCREMENT,
`log_message` longtext,
`level` varchar(10) DEFAULT NULL,
`ip_address_merchant` varchar(45) DEFAULT NULL,
`ip_address_customer` varchar(45) DEFAULT NULL,
`creationTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updateTime` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11
It showed an error Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
When I googled I saw that in the mysql 5.6. version this issue / restriction has been took off.But i wont be able to upgrade mysql right now.
Is there any work around for this,whith out changing the table structure to dump this using mysql?Please help.Im having little knowledge about the db operations.Do help.Thanks
You could remove ON UPDATE CURRENT_TIMESTAMP and create trigger.
DROP TRIGGER IF EXISTS `update_logging_api_request_js`;
DELIMITER //
CREATE TRIGGER `update_logging_api_request_js` BEFORE UPDATE ON `logging_api_request_js`
FOR EACH ROW BEGIN
SET NEW.updateTime = NEW.creationTime;
END
//
DELIMITER ;
CREATE TABLE IF NOT EXISTS `$id` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`start` varchar(10) NOT NULL,
`end` varchar(10) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=45 ;
INSERT INTO `$id`(`start`, `end`) VALUES ('0','0')
I have been trying to figure out how I can bind these two Mysql(requests(?)) into one with no success. Basically I want it to work so when I create the table it should also add the values 0 and 0 to "start" and "end" rows. But I still want the "Create table if not exists" to be in effect for the INSERT INTO. So if the table exist don't INSERT either.
You could do that with following single statement:
CREATE TABLE IF NOT EXISTS `$id` (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`start` VARCHAR(10) NOT NULL,
`end` VARCHAR(10) NOT NULL,
`date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB
SELECT '0' AS `start`, '0' AS `end`;
You use the CREATE TABLE ... SELECT syntax with selecting not from a table, but constant values and get the autoincrement value and the current_timestamp by default.
as of MySQL 5.5.6 or newer, see following excerpt from the manual, CREATE TABLE ... SELECT:
As of MySQL 5.5.6, handling of CREATE TABLE IF NOT EXISTS ... SELECT
statements was changed for the case that the destination table already
exists. This change also involves a change in MySQL 5.1 beginning with
5.1.51.
Previously, for CREATE TABLE IF NOT EXISTS ... SELECT, MySQL produced
a warning that the table exists, but inserted the rows and wrote the
statement to the binary log anyway. By contrast, CREATE TABLE ...
SELECT (without IF NOT EXISTS) failed with an error, but MySQL
inserted no rows and did not write the statement to the binary log.
MySQL now handles both statements the same way when the destination
table exists, in that neither statement inserts rows or is written to
the binary log. The difference between them is that MySQL produces a
warning when IF NOT EXISTS is present and an error when it is not.
This change means that, for the preceding example, the CREATE TABLE IF
NOT EXISTS ... SELECT statement inserts nothing into the destination
table as of MySQL 5.5.6.
I am working on a function that compares the date created and date modified of images and return the status of each case with PHP + MySQL. However, I realized that the data i'm trying to compare both end up using the CURRENT_TIMESTAMP in MySQL so whenever they are updated they end up having the same dates.
Is there a way to just only save the first date the data is inserted into the database (date created) so it doesn't change based on date modified?
Any help is appreciated, thanks in advance!
UPDATE:
my timestamp columns are configured using "DEFAULT CURRENT_TIMESTAMP" not the "ON UPDATE CURRENT_TIMESTAMP" option. Any other work arounds?
UPDATE2:
Please see below for my table definition.
CREATE TABLE IF NOT EXISTS `images` (
`id` varchar(50) NOT NULL,
`patientid` varchar(8) NOT NULL,
`caseid` varchar(25) NOT NULL,
`image_name` varchar(256) NOT NULL,
`status` int(1) unsigned NOT NULL,
`comments` varchar(4000) DEFAULT NULL,
`mod_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Seems like your timestamp columns are configured with "ON UPDATE CURRENT_TIMESTAMP" option, which automatically updates them.
As there does not seem a way to change this on a column, you have to create a new column without that option.
See the TIMESTAMP manual for details visit timestamp-initialization
I'm trying to make a table that has two timestamps columns, one will be for when a row is created and the other for when the row is updated. Here's what I tried so far:
CREATE TABLE `tmp` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`data` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created` timestamp NOT NULL ,
`modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
But I'm getting this error:
Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
You can do this in MySQL 5.6 (available at the time of writing as release candidate but not yet production-ready).
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.... For any TIMESTAMP or DATETIME column in a table, you can assign the current timestamp as the default value, the auto-update value, or both
taken from https://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html
This is a MYSQL constraint, You can have only one column whose default value will be the systime.
This question can also be referred
How to add "ON update current timestamp" to existing table column
You could write a trigger to add the created timestamp on inserts seperately
delimiter |
CREATE TRIGGER add_created_ts BEFORE INSERT on `tmp`
FOR EACH ROW
BEGIN
SET NEW.created = current_timestamp;
END
|
delimiter ;