Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Laravel 4 Basics Laravel and Databases Migrations & Schema Builder

Curtis Black
Curtis Black
2,829 Points

Value NOW() in table puts in default '0000-00-00 00:00:00'. Why?

I am running windows 8.1 pro and am using mysql workbench. When I insert a new row and include the value for the timestamps of NOW(), it puts in the default zeros and not the current timestamp. I can't seem to figure out why this is.

1 Answer

John Mercer
John Mercer
31,479 Points

I've never heard of NOW() working in any CREATE TABLE syntax. As of MySQL 5.6.5, you can (1) have column default to the current DateTime and (2) have a column that does the former and also updates the timestamp to the current DateTime when the row is update.

CREATE TABLE t1 (
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
dt DATETIME DEFAULT CURRENT_TIMESTAMP
);

Also,

CREATE TABLE t1 (
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
dt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

If you do something like

ts1 TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,     -- default 0

it will default to zero.

Pretty cool! For more info, read the docs.