change Mysql current_timestamp default value - php

I have a field in a MySQL database with default value as current_timestamp
and i wish to insert value as follows:
e.g. 2014-01-25 03:53:14 to 2014-01-25 00:00:00
How can i do this?
Any suggestion or idea would be great help!!!

what you should do is change the type of your field to DATE type. Then create a trigger to SET NEW.field = CURDATE().
Here's an example in sqlFiddle
CREATE TRIGGER default_my_timestamp BEFORE INSERT ON yourTable
FOR EACH ROW
SET NEW.timestamp = CURDATE();

Related

Invalid default value for 'Date and time' CURRENT_TIMESTAMP

I am using:
'time' type(datetime) defult value(CURRENT_TIMESTAMP)
but it doesn't work, it's showing the following error:
Invalid default value for 'Date'
Please can anybody help me?
If you are running MySQL version 5.6.5 or later
In your CREATE TABLE you can declare a column like this:
`mydtcol` DATETIME DEFAULT CURRENT_TIMESTAMP
Before 5.6, it's not possible to use CURRENT_TIMESTAMP for a DATETIME column. It is possible with the first TIMESTAMP column in a table.
`mytscol` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
If you require the column to be DATETIME datatype, and you need the value of the column initialized when you insert a row, you can either provide a value for the column in the INSERT statement, e.g.
INSERT INTO mytable (...,mycol,...) VALUES (...,'2016-04-21 23:55:55',...)
Or, you could use a BEFORE INSERT trigger to assign a value to the column.
DELIMITER $$
CREATE TRIGGER mytable_bi
BEFORE INSERT ON mytable
FOR EACH ROW
BEGIN
IF new.mydtcol IS NULL THEN
SET new.mydtcol = NOW();
END IF;
END$$
DELIMITER ;

Updating date by CURRENT_TIMESTAMP .. Why all Zeros?

I am trying to update date whenever any updates occur on the record, the field type is datetime and I set the default value to CURRENT_TIMESTAMP. when update sql is excuted the result is all zeros!
0000-00-00 00:00:00
$sql = 'INSERT INTO product_shop_offers (id,product_id,shop_id,price,currency_id,added_by,last_update)'
. ' VALUES (?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE id=?,shop_id=?,price=?,currency_id=?,added_by=?,last_update=?';
$this->db->query($sql, array(
$storeInfoArray['recordId'],
$productId,
$storeInfoArray['storeNameNo'],
$storeInfoArray['price'],
$storeInfoArray['currency'],
$addedBy,
'CURRENT_TIMESTAMP()',
$storeInfoArray['recordId'],
$storeInfoArray['storeNameNo'],
$storeInfoArray['price'],
$storeInfoArray['currency'],
$addedBy,
'CURRENT_TIMESTAMP()'
))
Note: I checked Similar problem question but I didn't understand the problem!!can somebody help please.
Is it because you are binding in the string 'CURRENT_TIMESTAMP()'.. I'm not sure you can do that and it is being converted to zeros.
If you have the default set in the database, why bother binding it in at all? Just remove the field from the statement.. oh and you'll need to add the attribute ON UPDATE CURRENT_TIMESTAMP to the column:
ALTER TABLE product_shop_offers
MODIFY COLUMN last_update DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
If you need it otherwise, you may have to write it into the query directly.
I think it should be helpful for you
MySQL CURRENT_TIMESTAMP field updates on every update
Change CURRENT_TIMESTAMP() to NOW()
Another possible way is to set the field type from datetime to timestamp

MYSQL ON UPDATE CURRENT_TIMESTAMP not updating when using INSERT OR REPLACE?

I unable to update current time(NOW) in last_updated cloumn.
Because i have read this query from text file.
INSERT OR REPLACE INTO table_name (kb_corporate_guid,kb_user_guid,
name,last_updated)
VALUES ('100','121','FAQ','2013-02-07 07:06:05');
I want to ignore last_updated cloumn value '2013-02-07 07:06:05' even if i specified in query and replace with NOW() value into last_updated cloumn.
I tried this one but it won't work for me.
ALTER TABLE table_name
CHANGE `last_updated` `last_updated` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP;
Option 1
You may create trigger on insert/update which will update last_updated field with MySql NOW() function, it will overwrite the field value, but it might slow down the process when you do bulk insert/update.
Option 2
Find and Replace the text for last_updated field and timestamp values from text file.
Option 3
Create temporary table with same schema and import into temporary table then use INSERT INTO main_table SELECT corp_id, user_id, name, NOW() FROM temp_table table to insert into main table.
Default is used when the value is not sent in the query, use now in the query instead of timestamp..

TIMESTAMP to DATETIME transition?

How can I convert a column with TIMESTAMP to DATETIME and retain the dates? I'm using PHPMyAdmin.
Also timestamp had the option to autofill, is there a way to do so in DATETIME? Or should I insert it each time with PHP?
thanks
If this query
ALTER TABLE table CHANGE `time` `time` datetime
will lose dates you can create new column then assign old values then delete old column and rename new one
ALTER TABLE table ADD `datetime` datetime AFTER `time`;
UPDATE table set datetime=time;
ALTER TABLE table DROP datetime;
ALTER TABLE CHANGE `datetime` `time` datetime
Read this:
This means, for example, that you cannot set the default for a date column to be the value of a function such as NOW() or CURRENT_DATE.
ALTER TABLE tablename MODIFY COLUMN columnname DATETIME;
(test on a test table first...)
No way to set the default to the current time save for triggers:
DELIMITER $
CREATE TRIGGER tablename_before_insert BEFORE INSERT ON tablename
FOR EACH ROW
BEGIN
IF NEW.columnname IS NULL THEN
SET NEW.columnname = NOW();
END IF;
IF NEW.datum = '0000-00-00 00:00:00' THEN
SET NEW.columnname = NOW();
END IF;
END$
DELIMITER ;
Use FROM_UNIXTIME() to convert from a unix timestamp into a regular datetime value.
A unix timestamp has no native type in mysql - it's simply an integer. timestamp fields will auto-populate, but only the first one in any given table. More details on that here.

Updated Timestamp field in MySQL through PHP

I have a database table in mysql with a field that is of "TIMESTAMP" type. I need help writing the SQL query to update the field with the current timestamp.
UPDATE tb_Test set dt_modified = ?????
Use:
UPDATE tb_Test
SET dt_modified = CURRENT_TIMESTAMP
WHERE ? -- if you don't specify, ALL dt_modified values will be updated
You can use NOW() instead of CURRENT_TIMESTAMP, but CURRENT_TIMESTAMP is ANSI standard so the query can be ported to other databases.
ALTER TABLE tb_Test MODIFY COLUMN dt_modified TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP
Now whenever any field is changed the dt_modified will be updated by the special trigger.

Categories