Solved

can i sert default date/rime in mysql

Posted on 2007-12-02
6
859 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 200 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 200 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 100 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
Upcoming Webinar: Securing your MySQL/MariaDB data

Join Percona’s Chief Evangelist, Colin Charles as he presents Securing your MySQL®/MariaDB® data on Tuesday, July 11, 2017 at 7:00 am PDT / 10:00 am EDT (UTC-7).

 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 150 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 50 total points
ID: 20393775
create table date (id int, dateaval datetime default getdate())
0
 
LVL 52

Assisted Solution

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

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

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 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