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
  #11  
Old   
ThomasH
 
Posts: n/a

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






Thanks guys

Being more comfortable with SQL, I think a trigger is more up my alley.

Will get back to you with sample code for others to use, when (if?) I
have a solution.

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

Default Re: Data type for when a record was last modified - 09-22-2011 , 12:23 PM






So, a trigger's the way go.

Found a pretty nice article:
http://joegornick.com/2009/12/30/mys...d-date-fields/

Still no luck Should be as easy as:

-- 1) create the columns
ALTER TABLE articles add `date_created` TIMESTAMP NULL DEFAULT NULL;
ALTER TABLE articles add `date_modified` TIMESTAMP NOT NULL DEFAULT
CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;


-- 1) create the trigger

DROP FUNCTION IF EXISTS temp_before_insert_created_date ;
DELIMITER |

CREATE TRIGGER temp_before_insert_created_date BEFORE INSERT ON `articles`
FOR EACH ROW
BEGIN SET NEW.date_created= CURRENT_TIMESTAMP;

END
Quote:
DELIMITER ;


New rows get the same date_created and date_modified.

Edited rows remain the same.

Perhaps seeing when an article was last edited isn't all that important :/

Reply With Quote
  #13  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

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



ThomasH wrote:

Quote:
So, a trigger's the way go.

Found a pretty nice article:
http://joegornick.com/2009/12/30/mys...d-date-fields/

Still no luck
"Does not work" is not an error description. Try to track the problem down
before you post, like reviewing the log. And get a real name.

Quote:
Should be as easy as:

-- 1) create the columns
ALTER TABLE articles add `date_created` TIMESTAMP NULL DEFAULT NULL;
ALTER TABLE articles add `date_modified` TIMESTAMP NOT NULL DEFAULT
CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
You can add two (or more) columns in one ALTER statement, you know.

Quote:
-- 1) create the trigger

DROP FUNCTION IF EXISTS temp_before_insert_created_date ;
DELIMITER |

CREATE TRIGGER temp_before_insert_created_date BEFORE INSERT ON `articles`
^^^^^^^^^^^^^
Wrong event for your purpose, yes?

RTFM: <http://dev.mysql.com/doc/refman/5.5/en/create-trigger.html>

Quote:
FOR EACH ROW
BEGIN SET NEW.date_created= CURRENT_TIMESTAMP;
END
|
DELIMITER ;
HTH

--
PointedEars

Please do not Cc: me. / Bitte keine Kopien per E-Mail.

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

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



On Thu, 22 Sep 2011 19:23:38 +0200, ThomasH wrote:
Quote:
So, a trigger's the way go.

Found a pretty nice article:
http://joegornick.com/2009/12/30/mys...d-date-fields/

Still no luck Should be as easy as:

-- 1) create the columns
ALTER TABLE articles add `date_created` TIMESTAMP NULL DEFAULT NULL;
ALTER TABLE articles add `date_modified` TIMESTAMP NOT NULL DEFAULT
CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;


-- 1) create the trigger

DROP FUNCTION IF EXISTS temp_before_insert_created_date ;
DELIMITER |

CREATE TRIGGER temp_before_insert_created_date BEFORE INSERT ON `articles`
FOR EACH ROW
BEGIN SET NEW.date_created= CURRENT_TIMESTAMP;

END
|
DELIMITER ;


New rows get the same date_created and date_modified.

Edited rows remain the same.
Swap 'em. Put the one you want to auto-update as the FIRST timestamp in
the table. See if that works better.

--
96. My door mechanisms will be designed so that blasting the control
panel on the outside seals the door and blasting the control panel
on the inside opens the door, not vice versa.
--Peter Anspach's list of things to do as an Evil Overlord

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.