If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > getting record to automatically put in default date

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-14-04, 13:23
leeroy leeroy is offline
Registered User
 
Join Date: Jun 2002
Posts: 173
getting record to automatically put in default date

is there something similar to "timestamp" column type or a way of having a "date" field automatically insert today's date when the record is created. I want it to do something similar to "timestamp" but i don't want it to change everytime the record is edited/updated. It would just be something like a record creation date. any help would be appreciated. thanks.
Reply With Quote
  #2 (permalink)  
Old 04-15-04, 07:39
edwin edwin is offline
Registered User
 
Join Date: Apr 2002
Posts: 56
Add a date column to the table and assign a default value to it.
Then you don't mention it in the INSERT statement, but it gets filled with that default.

default value: SQL Server getdate()
Oracle sysdate
MySQL ??

I'm more into SQL Server and Oracle, so I don't know het 'now'-function
in MySQL.
Reply With Quote
  #3 (permalink)  
Old 04-15-04, 07:50
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
unfortunately, in mysql the DEFAULT for a column cannot be a function

however, the first TIMESTAMP column in a table will be updated automatically

you can prevent it from being updated on every UPDATE by following the suggestions in the manual
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 04-15-04, 11:16
leeroy leeroy is offline
Registered User
 
Join Date: Jun 2002
Posts: 173
From what i've read in the mysql manual "timestamp" behaves differently whether or not mysql is in maxdb mode or not. if in maxdb mode the timestamp will not update to current date/time when the record is updated.

i need to have two columns in my tables. one field that will have a record creation date/time that is defaulted to current date when record is created only. then the second date field will update to current date/time whenever the record is changed. is this possible? i need a solution that will work in maxdb mode or not. why would mysql change the way the 'timestamp' field type behaves depending on maxdb mode or not?
Reply With Quote
  #5 (permalink)  
Old 04-15-04, 11:23
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
make the second one the first one with a TIMESTAMP datatype

it will get updated every row update

make the first one the second one with DATETIME datatype

assign the value explicitly when inserting

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 04-15-04, 13:20
leeroy leeroy is offline
Registered User
 
Join Date: Jun 2002
Posts: 173
thanks for your suggestions.

did i understand the mysql manual correctly that the 'timestamp' field updates or does not update on a record change depending on whether maxdb is enabled?
Reply With Quote
  #7 (permalink)  
Old 04-15-04, 13:37
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
12.5.1 The DATETIME, DATE, and TIMESTAMP Types

?

12.5.1.1 TIMESTAMP Properties Prior to MySQL 4.1

?

12.5.1.2 TIMESTAMP Properties as of MySQL 4.1

aha

"In addition, if the MySQL server is running in MAXDB mode, TIMESTAMP is identical with DATETIME. That is, if the server is running in MAXDB mode at the time that a table is created, any TIMESTAMP columns are created as DATETIME columns. As a result, such columns use DATETIME display format, have the same range of values, and no automatic updating occurs. "

so if you want automatic timestamp updating, don't run in maxdb mode, is the conclusion i would draw from that

what's maxdb?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 04-15-04, 15:41
leeroy leeroy is offline
Registered User
 
Join Date: Jun 2002
Posts: 173
i don't know anything about maxdb mode but i'm searching. if you find anything out let me know. thanks.
Reply With Quote
  #9 (permalink)  
Old 05-01-04, 17:29
hesuresh hesuresh is offline
Registered User
 
Join Date: May 2004
Posts: 1
Ading Current Date with mysql automatically

Use the function CURDATE()

here is an example of using the above function. Remember to set the date field (`today` field in the following example) to type DATE

INSERT INTO `table_name` ( `id` , `entry` , `today`)
VALUES ('', 'dummy entry', CURDATE( ) );
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On