Solved

can i sert default date/rime in mysql

Posted on 2007-12-02
6
851 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
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

This article describes some very basic things about SQL Server filegroups.
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…

776 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