dbTalk Databases Forums  

Data type for when a record was last modified

comp.databases.mysql comp.databases.mysql


Discuss Data type for when a record was last modified in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
ThomasH
 
Posts: n/a

Default Data type for when a record was last modified - 09-20-2011 , 01:01 PM






Hi guys,

Total noob here, trying to figure out how to build a database

Problem
I want two fields in my tablet:
1) showing when a record was created &
2) showing when the record was last updated

#1 I can figure out. #2 is a bugger. I've tried TIMESTAMP, and a few
other types, but whenever I update the record, nothing happens :/

Suggestions?
Thanks

Reply With Quote
  #2  
Old   
The Natural Philosopher
 
Posts: n/a

Default Re: Data type for when a record was last modified - 09-20-2011 , 02:19 PM






ThomasH wrote:
Quote:
Hi guys,

Total noob here, trying to figure out how to build a database

Problem
I want two fields in my tablet:
1) showing when a record was created &
2) showing when the record was last updated

#1 I can figure out. #2 is a bugger. I've tried TIMESTAMP, and a few
other types, but whenever I update the record, nothing happens :/

Suggestions?
Thanks

I explicitly set it in (in this case ) PHP using the function
$time=date("Y-m-d H:i:s");

the data type is DATETIME

I suspect there is a Mysql way to do the same like
update..mytable set last_access=NOW();

Reply With Quote
  #3  
Old   
ThomasH
 
Posts: n/a

Default Re: Data type for when a record was last modified - 09-20-2011 , 02:42 PM



Quote:
I explicitly set it in (in this case ) PHP using the function
$time=date("Y-m-d H:i:s");
Cool, I get that But what do you do if your input doesn't come from
PHP? Lets say it's being manually typed into the database?

In SQL I might make a trigger, that notes the date of last modification,
but I'd sure love for the database to keep track of it for me, no matter
when or how an entry is updated.

Thanks though

Reply With Quote
  #4  
Old   
The Natural Philosopher
 
Posts: n/a

Default Re: Data type for when a record was last modified - 09-20-2011 , 03:32 PM



ThomasH wrote:
Quote:
I explicitly set it in (in this case ) PHP using the function
$time=date("Y-m-d H:i:s");

Cool, I get that But what do you do if your input doesn't come from
PHP? Lets say it's being manually typed into the database?

In SQL I might make a trigger, that notes the date of last modification,
but I'd sure love for the database to keep track of it for me, no matter
when or how an entry is updated.

Thanks though
see the next part of my post. ...set last_access=NOW()

I think it may be even simpler.

Reply With Quote
  #5  
Old   
Peter H. Coffin
 
Posts: n/a

Default Re: Data type for when a record was last modified - 09-20-2011 , 04:32 PM



On Tue, 20 Sep 2011 20:01:21 +0200, ThomasH wrote:
Quote:
Hi guys,

Total noob here, trying to figure out how to build a database

Problem
I want two fields in my tablet:
1) showing when a record was created &
2) showing when the record was last updated

#1 I can figure out. #2 is a bugger. I've tried TIMESTAMP, and a few
other types, but whenever I update the record, nothing happens :/
Section 10.3.1.1 of the manual. The abbreviated answer is that there are
two controls for automatically handling TIMESTAMP columns. 1) you've
figured out ( my_ts_col TIMESTAMP DEFAULT CURRENT_TIMESTAMP ). 2) auto
updating whenever the record changes uses the following format:

my_ts_col TIMESTAMP DEFAULT 0 ON UPDATE CURRENT_TIMESTAMP

turns off auto initialization, but turns on updates on UPDATE. (If
you're using traditional sql, this will also get you an error because a
zero timestamp is an error, and there's another couple of settings that
can cause problems with this as well, but they're rare alone.)

There's some additional quirks that these are *automatically* both turned
on for the first timestamp column in a record layout. If you let the
first column do that, you'll get the behavior of having both turned
on. If you explicitly turn on the DEFAULT version, that turns off the
autoupdate for that column. If you set the column to nullable you'll
ruin the default automatic settings and I think the ON UPDATE as well.

--
I wish there was a knob on the TV to turn up the intelligence. There's a
knob called "brightness", but it doesn't work.
-- Gallagher

Reply With Quote
  #6  
Old   
ThomasH
 
Posts: n/a

Default Re: Data type for when a record was last modified - 09-21-2011 , 01:41 AM



On 20-09-2011 23:32, Peter H. Coffin wrote:
Quote:
On Tue, 20 Sep 2011 20:01:21 +0200, ThomasH wrote:
Hi guys,

Total noob here, trying to figure out how to build a database

Problem
I want two fields in my tablet:
1) showing when a record was created&
2) showing when the record was last updated

#1 I can figure out. #2 is a bugger. I've tried TIMESTAMP, and a few
other types, but whenever I update the record, nothing happens :/

Section 10.3.1.1 of the manual. The abbreviated answer is that there are
two controls for automatically handling TIMESTAMP columns. 1) you've
figured out ( my_ts_col TIMESTAMP DEFAULT CURRENT_TIMESTAMP ). 2) auto
updating whenever the record changes uses the following format:

my_ts_col TIMESTAMP DEFAULT 0 ON UPDATE CURRENT_TIMESTAMP

turns off auto initialization, but turns on updates on UPDATE. (If
you're using traditional sql, this will also get you an error because a
zero timestamp is an error, and there's another couple of settings that
can cause problems with this as well, but they're rare alone.)

There's some additional quirks that these are *automatically* both turned
on for the first timestamp column in a record layout. If you let the
first column do that, you'll get the behavior of having both turned
on. If you explicitly turn on the DEFAULT version, that turns off the
autoupdate for that column. If you set the column to nullable you'll
ruin the default automatic settings and I think the ON UPDATE as well.

Wow, that's great - I think I have something to go on now... I predict
some studying in my future.

Thanks to both repliers

Reply With Quote
  #7  
Old   
The Natural Philosopher
 
Posts: n/a

Default Re: Data type for when a record was last modified - 09-21-2011 , 04:16 AM



ThomasH wrote:
Quote:
On 20-09-2011 23:32, Peter H. Coffin wrote:
On Tue, 20 Sep 2011 20:01:21 +0200, ThomasH wrote:
Hi guys,

Total noob here, trying to figure out how to build a database

Problem
I want two fields in my tablet:
1) showing when a record was created&
2) showing when the record was last updated

#1 I can figure out. #2 is a bugger. I've tried TIMESTAMP, and a few
other types, but whenever I update the record, nothing happens :/

Section 10.3.1.1 of the manual. The abbreviated answer is that there are
two controls for automatically handling TIMESTAMP columns. 1) you've
figured out ( my_ts_col TIMESTAMP DEFAULT CURRENT_TIMESTAMP ). 2) auto
updating whenever the record changes uses the following format:

my_ts_col TIMESTAMP DEFAULT 0 ON UPDATE CURRENT_TIMESTAMP

turns off auto initialization, but turns on updates on UPDATE. (If
you're using traditional sql, this will also get you an error because a
zero timestamp is an error, and there's another couple of settings that
can cause problems with this as well, but they're rare alone.)

There's some additional quirks that these are *automatically* both turned
on for the first timestamp column in a record layout. If you let the
first column do that, you'll get the behavior of having both turned
on. If you explicitly turn on the DEFAULT version, that turns off the
autoupdate for that column. If you set the column to nullable you'll
ruin the default automatic settings and I think the ON UPDATE as well.


Wow, that's great - I think I have something to go on now... I predict
some studying in my future.

Thanks to both repliers
I thought there was probably a better way...

Reply With Quote
  #8  
Old   
ThomasH
 
Posts: n/a

Default Re: Data type for when a record was last modified - 09-21-2011 , 01:18 PM



Hi again,

So I took a look at
http://dev.mysql.com/doc/refman/5.0/en/timestamp.html

and added a column to my db using:

ALTER TABLE MyDB
ADD lastmodified TIMESTAMP
DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP;

Still - if I edit the contents of another field in the row,
"lastmodified" remains unchanged.

Have tried updating with both the default 000-00-00 value and one set to
now, to get the ball rolling - no dice :/

Any further advice?
Thomas

Reply With Quote
  #9  
Old   
Norman Peelman
 
Posts: n/a

Default Re: Data type for when a record was last modified - 09-21-2011 , 08:37 PM



On 09/21/2011 02:18 PM, ThomasH wrote:
Quote:
Hi again,

So I took a look at
http://dev.mysql.com/doc/refman/5.0/en/timestamp.html

and added a column to my db using:

ALTER TABLE MyDB
ADD lastmodified TIMESTAMP
DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP;

Still - if I edit the contents of another field in the row,
"lastmodified" remains unchanged.

Have tried updating with both the default 000-00-00 value and one set to
now, to get the ball rolling - no dice :/

Any further advice?
Thomas
I think only the first TIMESTAMP field created can get auto
updated... not sure, but itÅ› just as easy to manually trigger it like so:


UPDATE table SET blah = something, blah2 = something2, lastmodified = null

--
Norman
Registered Linux user #461062
-Have you been to www.mysql.com yet?-

Reply With Quote
  #10  
Old   
Jerry Stuckle
 
Posts: n/a

Default Re: Data type for when a record was last modified - 09-21-2011 , 11:28 PM



On 9/21/2011 2:18 PM, ThomasH wrote:
Quote:
Hi again,

So I took a look at
http://dev.mysql.com/doc/refman/5.0/en/timestamp.html

and added a column to my db using:

ALTER TABLE MyDB
ADD lastmodified TIMESTAMP
DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP;

Still - if I edit the contents of another field in the row,
"lastmodified" remains unchanged.

Have tried updating with both the default 000-00-00 value and one set to
now, to get the ball rolling - no dice :/

Any further advice?
Thomas
As others have indicated, it is only the first TIMESTAMP which can
autoupdate like this. However, you can easily create an UPDATE trigger
to do what you want. Add an INSERT trigger and you have everything. It
can even be a separate TIMESTAMP so you can see when it was created and
when it was last updated.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex (AT) attglobal (DOT) net
==================

Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.