MySQL - employ created_date and updated_date? - mysql

In MySQL, is there an easy way for the following:
On creation, both created_date and updated_date are set to the same TIMESTAMP
On subsequent edits, updated_date is changed to the CURRENT_TIMESTAMP
Why is only one field allowed to use the CURRENT_TIMESTAMP as its default value? Why can't I have one default to the CURRENT_TIMESTAMP and the other use it only on update?
If I use now() for created_date and on update CURRENT_TIMESTAMP for updated_date, will they be the same on the creation of a row?

That's just the way it is :)
But seriously, it's usually easier (less surprises) when you set those dates explicitly with 'now()' when you create/update the row.

Related

Database timestamps in sql

Creating a database table in MySQL. I have created two fields to grab timestamps.
created_at timestamp default '0000-00-00 00:00:00',
updated_at timestamp default now() on update now(),
When I update the database, both fields are updating to the current timestamp. Any thoughts on how to prevent this from happening? I am not providing the 'created_at' field when I update -- I am also providing 'null' for the updated_at field to auto update.
Depending on the version of MySQL you should be able to use the following for your default value on the updated_at field:
CURRENT_TIMESTAMP on UPDATE CURRENT_TIMESTAMP
Your created_at field should just have CURRENT_TIMESTAMP as the default.
I would also set both to the datatype datetime instead of timestamp.
Timestamp initialisation can be an ugly beast in mysql!
Based on the commment below you have mysql v5.6.4 or earlier:
I can't have two current_timestamps declared without getting an error -- and although it's default is 0 -- it is producing a current timestamp upon creation(without me feeding in the timestamp) – Spencer Rohan
You can have a single timestamp field that are initialised and/or updated to the current timestamp when you insert / update your record. In earlier versions this had to be the 1st timestamp field in the table.
I would simply set the created_at to be a nullable column with a default value of null because according to mysql documentation on timestamp initialization:
In other words, a TIMESTAMP column defined to permit NULL values auto-initializes only if its definition includes DEFAULT CURRENT_TIMESTAMP
By changing the default value to null you go around any problems you may have with zero dates and various sql modes.
created_at timestamp null default null,

How to create 2 auto-set timestamp columns in a MySQL table?

I want a MySQL table of mine to contain 2 timestamp columns, both set automatically without the client side help: one to be initialized once on insert:
`added` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
and another initialized the same on insert and updated on every update:
`updated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
But this doesn't work this way:
[Err] 1293 - Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
Is there a known workaround?
There is not just a workaround, there is a solution: Upgrade to MySQL 5.6.5 or higher and this is supported.
See: MySQL 5.6.6 TIMESTAMP columns and DEFAULT values
{edit} Since upgrading is not an option, you can make the first column a normal timestamp column and create a trigger that sets one timestamp when you insert the record. Then you can create the other colum with the DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, so it gets a timestamp on insertion and on update.
CREATE TRIGGER task_creation_timestamp BEFORE INSERT ON tasks
FOR EACH ROW
SET NEW.created = NOW();
I've stolen this trigger from this answer.
There is no "solution" as the error suggest, you can ONLY HAVE ONE TIMESTAMP per table (On previous versions of 5.6.6 as GolezTrol Suggested)
To workaround this i suggest you make of the "timestamps" a datetime and set the default to NOW() or CURRENT_TIMESTAMP() or any other synonym for NOW()

on insert current_timestamp in mysql

I need to alter a column with timestamp datatype. The thing is when record is inserted the current time stamp should be inserted in that column. I know there is ON UPDATE CURRENT_TIMESTAMP but for insertion i can't find a way.
What you're looking for is the DEFAULT keyword.
ALTER TABLE yourTable MODIFY COLUMN yourColumn timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
What Rock 'Em wanted was to be told to use CURRENT_TIMESTAMP as the default for the column. Not on update. I use this all of the time. That way when a record gets inserted you know exactly when it happened. It is good practice in many tables to do this for audit tracking, etc.
Simply use DEFAULT clause, ignoring ON UPDATE clause, that's it should work for you!
ALTER TABLE yourTable MODIFY COLUMN yourColumn timestamp DEFAULT CURRENT_TIMESTAMP

Is "ON UPDATE" specific for TIMESTAMP cols?

Is ON UPDATE specific for TIMESTAMP cols?
When I use it on cols with other types it causes a syntax error.
Whilst I'm no Mysql expert, yes it looks like that particular syntax is TIMESTAMP specific.
From the documentation:
To specify automatic properties, use the DEFAULT CURRENT_TIMESTAMP and
ON UPDATE CURRENT_TIMESTAMP clauses.
Then a little later:
Use of DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP is
specific to TIMESTAMP.

MySQL timestamp only on create

I use timestamp on MySQL 5.x (with PHP) to remember event times. During development I had to update the table with a query that changes something in all columns. The timestamp was then reset to current time.
How can I make timestamp change only on inserts and not on updates or replace?
Here's all you need to know. In short, though, I think this should do it:
ALTER TABLE `mytable`
CHANGE `mydatefield` `mydatefield`
TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP
Very good documentation here for time-stamp.
You can use a default value for that field and not include it in the insert or update query.