can i sert default date/rime in mysql

I have a columename  date   have a type datetime how can i se default value to current date time so the pro gram no need to add date time but my sql put it automatic when record is generate
teeraAsked:
Who is Participating?
 
steelseth12Connect With a Mentor Commented:
you can make the field timestamp which would automatically insert the date ... or you can use mysql NOW() function .. e.g

INSERT INTO tablename(datefield) VALUES(NOW());
0
 
steelseth12Connect With a Mentor Commented:
For more information on the timestamp datatype have a look at

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-type-overview.html

For information on the NOW() function have a look at

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_now
0
 
Jinesh KamdarConnect With a Mentor Commented:
Set a default value for the column in the table-definition if it is allowed in MySQL. E.g. Oracle allows to define a date-column with a default value returned by the SYSDATE function.

CREATE TABLE test
(
 i NUMBER,
 d DATE DEFAULT SYSDATE
);
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
_agx_Connect With a Mentor Commented:
> d DATE DEFAULT SYSDATE

This is a good idea.  But afaik MySql does not allow functions (like now()) in the DEFAULT clause.  For datetime columns I think you'll have to use a trigger.  There is a good example here

http://forums.mysql.com/read.php?99,156497,156572#msg-156572

Short answer:  

CREATE Trigger YourTableName_AddDefaultDate BEFORE INSERT ON YourTableName
    FOR EACH ROW
      SET new.`date` = NOW();
0
 
ursangelConnect With a Mentor Commented:
create table date (id int, dateaval datetime default getdate())
0
 
_agx_Connect With a Mentor Commented:
> default getdate())

Yes, that will work for MS SQL Server.  I think they are using MySQL though ...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.