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.
Related
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 ;
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();
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..
I am working with a database that has a table called date, which contains a separate field for day, month, year. Clearly this is not ideal when I am trying to run comparisons, etc. I am wondering is it possible for me to add a DateTime field to each row of this table and insert a concatenated string into the new field from the existing day, month, year fields.
I am quite sure its possible, I'm just wondering if anyone might be able to point me in the right direction on how to achieve this?
Below is the current date table: (i know)
CREATE TABLE IF NOT EXISTS `date` (
`deposition_id` varchar(11) NOT NULL default '',
`day` int(2) default NULL,
`month` int(2) default NULL,
`year` int(4) default NULL,
PRIMARY KEY (`deposition_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
first use alter table query:
alter table date add column datetimefield datetime NOT Null
then
use update query with self join on date and update datetimefield with concat on date,month, year column values.
Try this (untested) -
UPDATE date d SET d.datetime = (SELECT CONCAT('-','year','month','day') from date d1 where d1.id = d.id);
What is the problem, I don't understand? Alter the table, add new DATE column and then populate it with a string "yyyy-mm-dd" using CONCAT mysql function or whatever.
This seems like a really simple one but I'm struggling to figure it out. I want a column in my database that lists when a record was first created and another column that says when it was updated. It's my understanding I should be able to do all this just using MySQL. All help is appreciated :)
This stinks still no answer, reasons I'm already starting to miss Ruby on Rails...
You will probably need to use a combination of the Datetime datatype and the Timestamp data type. I would set my created column as a DateTime with a DEFAULT NOW(), and my updated column as a Timestamp with DEFAULT CURRENT_TIMESTAMP and an ON UPDATE CURRENT_TIMESTAMP attribute.
Here are the docs for the Timestamp dt:
http://dev.mysql.com/doc/refman/5.0/en/timestamp.html
In a CREATE TABLE statement, the first TIMESTAMP column can be declared in any of the following ways:
With both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP clauses, the column has the current timestamp for its default value, and is automatically updated.
With neither DEFAULT nor ON UPDATE clauses, it is the same as DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP.
With a DEFAULT CURRENT_TIMESTAMP clause and no ON UPDATE clause, the column has the current timestamp for its default value but is not automatically updated.
With no DEFAULT clause and with an ON UPDATE CURRENT_TIMESTAMP clause, the column has a default of 0 and is automatically updated.
With a constant DEFAULT value, the column has the given default and is not automatically initialized to the current timestamp. If the column also has an ON UPDATE CURRENT_TIMESTAMP clause, it is automatically updated; otherwise, it has a constant default and is not automatically updated.
To fulfill your question and for others viewing this question, here is the answer. Note this was written for MySQL 5.x.
DROP TABLE IF EXISTS `test1`;
CREATE TABLE `test1` (
`id` INT NULL AUTO_INCREMENT ,
`name` varchar(50) NOT NULL ,
`created` DATETIME ,
`updated` DATETIME ,
PRIMARY KEY (`id`),
INDEX (`name`)
);
DELIMITER $$
DROP TRIGGER IF EXISTS `test1_created`$$
CREATE TRIGGER `test1_created` BEFORE INSERT ON `test1`
FOR EACH ROW BEGIN
SET NEW.`created` = UTC_TIMESTAMP();
SET NEW.`updated` = UTC_TIMESTAMP();
END;
$$
DROP TRIGGER IF EXISTS `test1_updated`$$
CREATE TRIGGER `test1_updated` BEFORE UPDATE ON `test1`
FOR EACH ROW BEGIN
SET NEW.`updated` = UTC_TIMESTAMP();
END;
$$
DELIMITER ;
Note
You could use TIMESTAMP for the updated column which would have automatically updated the value thus not requiring the BEFORE UPDATE trigger, however TIMESTAMP has a range from 1970 to 2038 which is fast approaching and I like to think my applications will live forever :). Although TIMESTAMP is only 4bytes while DATETIME is 8bytes.
TIMESTAMP range '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC
DATETIME range '1000-01-01 00:00:00' to '9999-12-31 23:59:59'
From the MySQL 5.0 Certification Guide:
CREATE TABLE ts_test5 (
created TIMESTAMP DEFAULT 0,
updated TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
data CHAR(30)
);
To control the initialization and update behaviour of a TIMESTAMP column, you add either or both of the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP attributes to the column defintion when creating the table with CREATE TABLE...
and
...if you do not specify either of the DEFAULT CURRENT_TIMESTAMP or ON UPDATE CURRENT_TIMESTAMP attributes when creating a table, MySQL automatically assigns BOTH to the first TIMESTAMP column
Also
you cannot use DEFAULT CURRENT_TIMESTAMP with one column and ON UPDATE CURRENT_TIMESTAMP with another
If you can't use the timestamp fields with default attributes that Paul W has suggested, you can use AFTER INSERT and AFTER UPDATE triggers to populate the fields.
You will need two fields "Created" and
"Updated" with type datetime. When a
new entry is inserted then insert
"Created" with current time stamp.
When a update is happening insert
"Updated" with the current time stamp,
and let the "Created" field remain as
it is.
For current time stamp you can use
NOW() in your mysql query.