[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 866
  • Last Modified:

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
0
teera
Asked:
teera
6 Solutions
 
steelseth12Commented:
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
 
steelseth12Commented:
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 KamdarCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
_agx_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
 
ursangelCommented:
create table date (id int, dateaval datetime default getdate())
0
 
_agx_Commented:
> default getdate())

Yes, that will work for MS SQL Server.  I think they are using MySQL though ...
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now