[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

can i sert default date/rime in mysql

Posted on 2007-12-02
6
Medium Priority
?
862 Views
Last Modified: 2008-02-01
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
Comment
Question by:teera
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 20

Accepted Solution

by:
steelseth12 earned 800 total points
ID: 20391592
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
 
LVL 20

Assisted Solution

by:steelseth12
steelseth12 earned 800 total points
ID: 20391599
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
 
LVL 18

Assisted Solution

by:Jinesh Kamdar
Jinesh Kamdar earned 400 total points
ID: 20391938
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.

 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 600 total points
ID: 20392349
> 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
 
LVL 5

Assisted Solution

by:ursangel
ursangel earned 200 total points
ID: 20393775
create table date (id int, dateaval datetime default getdate())
0
 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 600 total points
ID: 20393814
> default getdate())

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

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question