Thread: DEFAULT TEXT 'now'
Hi I´m have a table like bellow: CREATE TABLE conex ( id int4 PRIMARY KEY DEFAULT NEXTVAL('seq_conex'), inicio datetime DEFAULT TEXT 'now', ultimo datetime DEFAULT TEXT 'now', . . . ) insert into conex (ip) values(´200.200.200.200´) Will result every time the same data and time for ´inicio´ and ´ultimo´! I want to insert the current date and time. Is that code correct and the result a bug??? Is there an way to make the DEFAULT work??? I know I can insert like: insert into conex (ip, inicio, ultimo) values(´200.200.200.200´, ´now´, ´now´) but I would like to use DEFAULT. PostgreSQL 6.4 - Solaris 2.5 sparc Thank you Roberto
On Tue, 23 Mar 1999, Roberto Joao Lopes Garcia wrote: > Hi > > I�m have a table like bellow: > > CREATE TABLE conex ( > id int4 PRIMARY KEY DEFAULT NEXTVAL('seq_conex'), > > inicio datetime DEFAULT TEXT 'now', > ultimo datetime DEFAULT TEXT > 'now', > . > . > . > ) > > insert into conex (ip) values(�200.200.200.200�) > > Will result every time the same data and time for �inicio� and �ultimo�! > > I want to insert the current date and time. Is that code correct and the > result a bug??? > Is there an way to make the DEFAULT work??? try: ... inicido DATETIME DEFAULT NOW(), ultimo DATETIME DEFAULT NOW(), ... --- Howie <caffeine@toodarkpark.org> URL: http://www.toodarkpark.org "You can change the world with a bullet in the right place"
>>>>> "RJLG" == Roberto Joao Lopes Garcia <roberto@mha.com.br> writes: RJLG> Hi RJLG> I╢m have a table like bellow: RJLG> CREATE TABLE conex ( RJLG> id int4 PRIMARY KEY DEFAULT NEXTVAL('seq_conex'), RJLG> inicio datetime DEFAULT TEXT 'now', RJLG> ultimo datetime DEFAULT TEXT RJLG> 'now', RJLG> . RJLG> . RJLG> . RJLG> ) RJLG> insert into conex (ip) values(╢200.200.200.200╢) RJLG> Will result every time the same data and time for ╢inicio╢ and ╢ultimo╢! You must type inicio datetime DEFAULT TEXT now(), ^^^^^ This is the function which calls every time you insert new record. In your case 'now' is the constant, which calculates once, when you create the table. -- Anatoly K. Lasareff Email: tolik@icomm.ru Senior programmer
Anatoly K. Lasareff wrote: > > >>>>> "RJLG" == Roberto Joao Lopes Garcia <roberto@mha.com.br> writes: > >> <snipped problem with using default 'now'> > <snipped description of using now() instead> What version of postgresql are you all using? I tried RJLG's example code with 6.4.2, and it worked - I got different times at each insert, so the default 'now' was being calculated at insertion time, not table creation time. Here's what it looks like: test2=> \d conex Table = conex +----------------------------------+----------------------------------+-------+ | Field | Type | Length| +----------------------------------+----------------------------------+-------+ | id | int4 not null default nextval ( | 4 | | inicio | datetime default text 'now' | 8 | | ultimo | datetime default text 'now' | 8 | | ip | inet | var | +----------------------------------+----------------------------------+-------+ Index: conex_pkey test2=> select * from conex; id|inicio |ultimo | ip --+----------------------------+----------------------------+--------------- 1|Tue Mar 23 13:40:24 1999 CST|Tue Mar 23 13:40:24 1999 CST|200.200.200.200 2|Tue Mar 23 13:40:28 1999 CST|Tue Mar 23 13:40:28 1999 CST|200.200.200.202 3|Tue Mar 23 13:40:30 1999 CST|Tue Mar 23 13:40:30 1999 CST|200.200.200.203 4|Tue Mar 23 13:41:06 1999 CST|Tue Mar 23 13:41:06 1999 CST|200.200.200.203 5|Tue Mar 23 13:41:21 1999 CST|Tue Mar 23 13:41:21 1999 CST|200.200.200.205 (5 rows) test2=> ??? Ross -- Ross J. Reedstrom, Ph.D., <reedstrm@rice.edu> NSBRI Research Scientist/Programmer Computer and Information Technology Institute Rice University, 6100 S. Main St., Houston, TX 77005