PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > mysql.general > timestamp for update and insert
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
timestamp for update and insert

Réponse
 
LinkBack Outils de la discussion
Vieux 04/09/2007, 20h01   #1
Hiep Nguyen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut timestamp for update and insert

Hi list,

i tried to create a table with inserted & lastupdated timestamp fields:

create table temp (
id int not null primary ke auto_increment,
data varchar(100),
inserted timestamp default now(),
lastupdated timestamp(8));


how do i get mysql to put in the current timestamp for inserted &
lastupdated fields when i insert a record and only lastupdated when i
update the record?

thanks,
T. Hiep

  Réponse avec citation
Vieux 04/09/2007, 20h05   #2
Olaf Stein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: timestamp for update and insert

I would use a trigger (at least for the update)....

The first insert should work with now() and you can leave lastupdateted
empty


Olaf


On 9/4/07 3:01 PM, "Hiep Nguyen" <hiep@ee.ucr.edu> wrote:

> Hi list,
>
> i tried to create a table with inserted & lastupdated timestamp fields:
>
> create table temp (
> id int not null primary ke auto_increment,
> data varchar(100),
> inserted timestamp default now(),
> lastupdated timestamp(8));
>
>
> how do i get mysql to put in the current timestamp for inserted &
> lastupdated fields when i insert a record and only lastupdated when i
> update the record?
>
> thanks,
> T. Hiep
>


  Réponse avec citation
Vieux 04/09/2007, 20h15   #3
Michael Dykman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: timestamp for update and insert

Triggers are a fine idea, but I would use a trigger for both cases..
no point putting that level of housekeeping on the application when
you can set rules in the database and more or less forget about it.

- michael


On 9/4/07, Olaf Stein <steino@ccri.net> wrote:
> I would use a trigger (at least for the update)....
>
> The first insert should work with now() and you can leave lastupdateted
> empty
>
>
> Olaf
>
>
> On 9/4/07 3:01 PM, "Hiep Nguyen" <hiep@ee.ucr.edu> wrote:
>
> > Hi list,
> >
> > i tried to create a table with inserted & lastupdated timestamp fields:
> >
> > create table temp (
> > id int not null primary ke auto_increment,
> > data varchar(100),
> > inserted timestamp default now(),
> > lastupdated timestamp(8));
> >
> >
> > how do i get mysql to put in the current timestamp for inserted &
> > lastupdated fields when i insert a record and only lastupdated when i
> > update the record?
> >
> > thanks,
> > T. Hiep
> >

>
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe: http://lists.mysql.com/mysql?unsub=mdykman@gmail.com
>
>



--
- michael dykman
- mdykman@gmail.com

- All models are wrong. Some models are useful.
  Réponse avec citation
Vieux 04/09/2007, 20h18   #4
Olaf Stein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: timestamp for update and insert

Agreed...
Also for consistency's sake


On 9/4/07 3:15 PM, "Michael Dykman" <mdykman@gmail.com> wrote:

> Triggers are a fine idea, but I would use a trigger for both cases..
> no point putting that level of housekeeping on the application when
> you can set rules in the database and more or less forget about it.
>
> - michael
>
>
> On 9/4/07, Olaf Stein <steino@ccri.net> wrote:
>> I would use a trigger (at least for the update)....
>>
>> The first insert should work with now() and you can leave lastupdateted
>> empty
>>
>>
>> Olaf
>>
>>
>> On 9/4/07 3:01 PM, "Hiep Nguyen" <hiep@ee.ucr.edu> wrote:
>>
>>> Hi list,
>>>
>>> i tried to create a table with inserted & lastupdated timestamp fields:
>>>
>>> create table temp (
>>> id int not null primary ke auto_increment,
>>> data varchar(100),
>>> inserted timestamp default now(),
>>> lastupdated timestamp(8));
>>>
>>>
>>> how do i get mysql to put in the current timestamp for inserted &
>>> lastupdated fields when i insert a record and only lastupdated when i
>>> update the record?
>>>
>>> thanks,
>>> T. Hiep
>>>

>>
>>
>> --
>> MySQL General Mailing List
>> For list archives: http://lists.mysql.com/mysql
>> To unsubscribe: http://lists.mysql.com/mysql?unsub=mdykman@gmail.com
>>
>>

>



  Réponse avec citation
Vieux 04/09/2007, 20h29   #5
Hiep Nguyen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: timestamp for update and insert

is it possible to do without trigger?

i google and found this link:
http://sql-info.de/mysql/examples/CR...-examples.html

but when i tried to combine two examples into one CREATE statement and it
didn't work.

any idea?

is there a way to create this table that accomplishes these two goals?

thanks,
T. Hiep

On Tue, 4 Sep 2007, Michael Dykman wrote:

> Triggers are a fine idea, but I would use a trigger for both cases..
> no point putting that level of housekeeping on the application when
> you can set rules in the database and more or less forget about it.
>
> - michael
>
>
> On 9/4/07, Olaf Stein <steino@ccri.net> wrote:
>> I would use a trigger (at least for the update)....
>>
>> The first insert should work with now() and you can leave lastupdateted
>> empty
>>
>>
>> Olaf
>>
>>
>> On 9/4/07 3:01 PM, "Hiep Nguyen" <hiep@ee.ucr.edu> wrote:
>>
>>> Hi list,
>>>
>>> i tried to create a table with inserted & lastupdated timestamp fields:
>>>
>>> create table temp (
>>> id int not null primary ke auto_increment,
>>> data varchar(100),
>>> inserted timestamp default now(),
>>> lastupdated timestamp(8));
>>>
>>>
>>> how do i get mysql to put in the current timestamp for inserted &
>>> lastupdated fields when i insert a record and only lastupdated when i
>>> update the record?
>>>
>>> thanks,
>>> T. Hiep
>>>

>>
>>
>> --
>> MySQL General Mailing List
>> For list archives: http://lists.mysql.com/mysql
>> To unsubscribe: http://lists.mysql.com/mysql?unsub=mdykman@gmail.com
>>
>>

>
>
> --
> - michael dykman
> - mdykman@gmail.com
>
> - All models are wrong. Some models are useful.
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe: http://lists.mysql.com/mysql?unsub=hiep@ee.ucr.edu
>
>

  Réponse avec citation
Vieux 04/09/2007, 20h47   #6
Michael Dykman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: timestamp for update and insert

There is nothing terribly wrong with the approach documented in
'http://sql-info.de/mysql/examples/CREATE-TABLE-examples.html' but, as
you no doubt have read, it does mean that you have to make sure that
every insert statement is specifically designed to set the *second*
timestamp field to now() and then count on the built-in properties to
see the first one updated on every UPDATE.

The only other caveats are:
your application behaviour is now dependent on the ordering of
columns; ok in the short-term, increasingly annoying over time as
maintainence phases grow the app in complexity.
importing data from your system to another system might prove
hairy as you figure out how to temporarily avoid this bevahiour to
keep your data intact.

The trigger method is universal in that this solution will port to any
half-way reasonable database engine....

but, as in all things IT, do whatever best meets your circumstances.

- michael

On 9/4/07, Hiep Nguyen <hiep@ee.ucr.edu> wrote:
> is it possible to do without trigger?
>
> i google and found this link:
> http://sql-info.de/mysql/examples/CR...-examples.html
>
> but when i tried to combine two examples into one CREATE statement and it
> didn't work.
>
> any idea?
>
> is there a way to create this table that accomplishes these two goals?
>
> thanks,
> T. Hiep
>
> On Tue, 4 Sep 2007, Michael Dykman wrote:
>
> > Triggers are a fine idea, but I would use a trigger for both cases..
> > no point putting that level of housekeeping on the application when
> > you can set rules in the database and more or less forget about it.
> >
> > - michael
> >
> >
> > On 9/4/07, Olaf Stein <steino@ccri.net> wrote:
> >> I would use a trigger (at least for the update)....
> >>
> >> The first insert should work with now() and you can leave lastupdateted
> >> empty
> >>
> >>
> >> Olaf
> >>
> >>
> >> On 9/4/07 3:01 PM, "Hiep Nguyen" <hiep@ee.ucr.edu> wrote:
> >>
> >>> Hi list,
> >>>
> >>> i tried to create a table with inserted & lastupdated timestamp fields:
> >>>
> >>> create table temp (
> >>> id int not null primary ke auto_increment,
> >>> data varchar(100),
> >>> inserted timestamp default now(),
> >>> lastupdated timestamp(8));
> >>>
> >>>
> >>> how do i get mysql to put in the current timestamp for inserted &
> >>> lastupdated fields when i insert a record and only lastupdated when i
> >>> update the record?
> >>>
> >>> thanks,
> >>> T. Hiep
> >>>
> >>
> >>
> >> --
> >> MySQL General Mailing List
> >> For list archives: http://lists.mysql.com/mysql
> >> To unsubscribe: http://lists.mysql.com/mysql?unsub=mdykman@gmail.com
> >>
> >>

> >
> >
> > --
> > - michael dykman
> > - mdykman@gmail.com
> >
> > - All models are wrong. Some models are useful.
> >
> > --
> > MySQL General Mailing List
> > For list archives: http://lists.mysql.com/mysql
> > To unsubscribe: http://lists.mysql.com/mysql?unsub=hiep@ee.ucr.edu
> >
> >

>



--
- michael dykman
- mdykman@gmail.com

- All models are wrong. Some models are useful.
  Réponse avec citation
Vieux 04/09/2007, 20h55   #7
Daevid Vincent
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: timestamp for update and insert

Just do this...

create table temp (
id int not null primary key auto_increment,
data varchar(100),
inserted timestamp default 0,
lastupdated default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

And just use

Insert into temp (inserted ) values (NOW());

You're only inserting once, so just do it in the code.

Then no need for triggers -- what a waste. Mysql will update the lastupdated
field.

Note the 'default 0' that is important... When you have multiple timestamp
columns and want one to be auto handled.

D.Vin

> -----Original Message-----
> From: Hiep Nguyen [mailto:hiep@ee.ucr.edu]
> Sent: Tuesday, September 04, 2007 12:02 PM
> To: mysql@lists.mysql.com
> Subject: timestamp for update and insert
>
> Hi list,
>
> i tried to create a table with inserted & lastupdated
> timestamp fields:
>
> create table temp (
> id int not null primary ke auto_increment,
> data varchar(100),
> inserted timestamp default now(),
> lastupdated timestamp(8));
>
>
> how do i get mysql to put in the current timestamp for inserted &
> lastupdated fields when i insert a record and only lastupdated when i
> update the record?
>
> thanks,
> T. Hiep
>
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe:
> http://lists.mysql.com/mysql?unsub=daevid@daevid.com
>
>


  Réponse avec citation
Vieux 04/09/2007, 20h59   #8
Hiep Nguyen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: timestamp for update and insert

so, if trigger is used then

create table temp (
id int not null primary key auto_increment,
data varchar(100),
inserted timestamp,
lastupdated timestamp)

is good enough, right? trigger will use now() function to set inserted &
lastupdated.

any thought on backup & restore tables & tringgers???

thank you for your s.
T. Hiep

On Tue, 4 Sep 2007, Michael Dykman wrote:

> There is nothing terribly wrong with the approach documented in
> 'http://sql-info.de/mysql/examples/CREATE-TABLE-examples.html' but, as
> you no doubt have read, it does mean that you have to make sure that
> every insert statement is specifically designed to set the *second*
> timestamp field to now() and then count on the built-in properties to
> see the first one updated on every UPDATE.
>
> The only other caveats are:
> your application behaviour is now dependent on the ordering of
> columns; ok in the short-term, increasingly annoying over time as
> maintainence phases grow the app in complexity.
> importing data from your system to another system might prove
> hairy as you figure out how to temporarily avoid this bevahiour to
> keep your data intact.
>
> The trigger method is universal in that this solution will port to any
> half-way reasonable database engine....
>
> but, as in all things IT, do whatever best meets your circumstances.
>
> - michael
>
> On 9/4/07, Hiep Nguyen <hiep@ee.ucr.edu> wrote:
>> is it possible to do without trigger?
>>
>> i google and found this link:
>> http://sql-info.de/mysql/examples/CR...-examples.html
>>
>> but when i tried to combine two examples into one CREATE statement and it
>> didn't work.
>>
>> any idea?
>>
>> is there a way to create this table that accomplishes these two goals?
>>
>> thanks,
>> T. Hiep
>>
>> On Tue, 4 Sep 2007, Michael Dykman wrote:
>>
>>> Triggers are a fine idea, but I would use a trigger for both cases..
>>> no point putting that level of housekeeping on the application when
>>> you can set rules in the database and more or less forget about it.
>>>
>>> - michael
>>>
>>>
>>> On 9/4/07, Olaf Stein <steino@ccri.net> wrote:
>>>> I would use a trigger (at least for the update)....
>>>>
>>>> The first insert should work with now() and you can leave lastupdateted
>>>> empty
>>>>
>>>>
>>>> Olaf
>>>>
>>>>
>>>> On 9/4/07 3:01 PM, "Hiep Nguyen" <hiep@ee.ucr.edu> wrote:
>>>>
>>>>> Hi list,
>>>>>
>>>>> i tried to create a table with inserted & lastupdated timestamp fields:
>>>>>
>>>>> create table temp (
>>>>> id int not null primary ke auto_increment,
>>>>> data varchar(100),
>>>>> inserted timestamp default now(),
>>>>> lastupdated timestamp(8));
>>>>>
>>>>>
>>>>> how do i get mysql to put in the current timestamp for inserted &
>>>>> lastupdated fields when i insert a record and only lastupdated when i
>>>>> update the record?
>>>>>
>>>>> thanks,
>>>>> T. Hiep
>>>>>
>>>>
>>>>
>>>> --
>>>> MySQL General Mailing List
>>>> For list archives: http://lists.mysql.com/mysql
>>>> To unsubscribe: http://lists.mysql.com/mysql?unsub=mdykman@gmail.com
>>>>
>>>>
>>>
>>>
>>> --
>>> - michael dykman
>>> - mdykman@gmail.com
>>>
>>> - All models are wrong. Some models are useful.
>>>
>>> --
>>> MySQL General Mailing List
>>> For list archives: http://lists.mysql.com/mysql
>>> To unsubscribe: http://lists.mysql.com/mysql?unsub=hiep@ee.ucr.edu
>>>
>>>

>>

>
>
> --
> - michael dykman
> - mdykman@gmail.com
>
> - All models are wrong. Some models are useful.
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe: http://lists.mysql.com/mysql?unsub=hiep@ee.ucr.edu
>
>

  Réponse avec citation
Vieux 04/09/2007, 21h04   #9
Olaf Stein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: timestamp for update and insert

If you decide to use the trigger here is the syntax

http://dev.mysql.com/doc/refman/5.0/en/triggers.html

And that table structure looks ok to me


As far as the backup goes just dump the mysql database, which you should be
doing anyway to backup users etc

Olaf



On 9/4/07 3:59 PM, "Hiep Nguyen" <hiep@ee.ucr.edu> wrote:

> so, if trigger is used then
>
> create table temp (
> id int not null primary key auto_increment,
> data varchar(100),
> inserted timestamp,
> lastupdated timestamp)
>
> is good enough, right? trigger will use now() function to set inserted &
> lastupdated.
>
> any thought on backup & restore tables & tringgers???
>
> thank you for your s.
> T. Hiep
>
> On Tue, 4 Sep 2007, Michael Dykman wrote:
>
>> There is nothing terribly wrong with the approach documented in
>> 'http://sql-info.de/mysql/examples/CREATE-TABLE-examples.html' but, as
>> you no doubt have read, it does mean that you have to make sure that
>> every insert statement is specifically designed to set the *second*
>> timestamp field to now() and then count on the built-in properties to
>> see the first one updated on every UPDATE.
>>
>> The only other caveats are:
>> your application behaviour is now dependent on the ordering of
>> columns; ok in the short-term, increasingly annoying over time as
>> maintainence phases grow the app in complexity.
>> importing data from your system to another system might prove
>> hairy as you figure out how to temporarily avoid this bevahiour to
>> keep your data intact.
>>
>> The trigger method is universal in that this solution will port to any
>> half-way reasonable database engine....
>>
>> but, as in all things IT, do whatever best meets your circumstances.
>>
>> - michael
>>
>> On 9/4/07, Hiep Nguyen <hiep@ee.ucr.edu> wrote:
>>> is it possible to do without trigger?
>>>
>>> i google and found this link:
>>> http://sql-info.de/mysql/examples/CR...-examples.html
>>>
>>> but when i tried to combine two examples into one CREATE statement and it
>>> didn't work.
>>>
>>> any idea?
>>>
>>> is there a way to create this table that accomplishes these two goals?
>>>
>>> thanks,
>>> T. Hiep
>>>
>>> On Tue, 4 Sep 2007, Michael Dykman wrote:
>>>
>>>> Triggers are a fine idea, but I would use a trigger for both cases..
>>>> no point putting that level of housekeeping on the application when
>>>> you can set rules in the database and more or less forget about it.
>>>>
>>>> - michael
>>>>
>>>>
>>>> On 9/4/07, Olaf Stein <steino@ccri.net> wrote:
>>>>> I would use a trigger (at least for the update)....
>>>>>
>>>>> The first insert should work with now() and you can leave lastupdateted
>>>>> empty
>>>>>
>>>>>
>>>>> Olaf
>>>>>
>>>>>
>>>>> On 9/4/07 3:01 PM, "Hiep Nguyen" <hiep@ee.ucr.edu> wrote:
>>>>>
>>>>>> Hi list,
>>>>>>
>>>>>> i tried to create a table with inserted & lastupdated timestamp fields:
>>>>>>
>>>>>> create table temp (
>>>>>> id int not null primary ke auto_increment,
>>>>>> data varchar(100),
>>>>>> inserted timestamp default now(),
>>>>>> lastupdated timestamp(8));
>>>>>>
>>>>>>
>>>>>> how do i get mysql to put in the current timestamp for inserted &
>>>>>> lastupdated fields when i insert a record and only lastupdated when i
>>>>>> update the record?
>>>>>>
>>>>>> thanks,
>>>>>> T. Hiep
>>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> MySQL General Mailing List
>>>>> For list archives: http://lists.mysql.com/mysql
>>>>> To unsubscribe: http://lists.mysql.com/mysql?unsub=mdykman@gmail.com
>>>>>
>>>>>
>>>>
>>>>
>>>> --
>>>> - michael dykman
>>>> - mdykman@gmail.com
>>>>
>>>> - All models are wrong. Some models are useful.
>>>>
>>>> --
>>>> MySQL General Mailing List
>>>> For list archives: http://lists.mysql.com/mysql
>>>> To unsubscribe: http://lists.mysql.com/mysql?unsub=hiep@ee.ucr.edu
>>>>
>>>>
>>>

>>
>>
>> --
>> - michael dykman
>> - mdykman@gmail.com
>>
>> - All models are wrong. Some models are useful.
>>
>> --
>> MySQL General Mailing List
>> For list archives: http://lists.mysql.com/mysql
>> To unsubscribe: http://lists.mysql.com/mysql?unsub=hiep@ee.ucr.edu
>>
>>


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h24.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,22491 seconds with 17 queries