pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
DEFAULT now() ?
2+ messages / 2 participants
[nested] [flat]

* DEFAULT now() ?
@ 2000-04-18 10:36  ty <ty@koi.tsukebe.net>
  0 siblings, 1 reply; 2+ messages in thread

From: ty @ 2000-04-18 10:36 UTC (permalink / raw)
  To: pgsql-admin

May someone please show me the necessary code snippets that would allow the value of a column to take the current time ( ala now() ) by default on INSERT?

Indicative example:

CREATE TABLE foo (
    a INTEGER NOT NULL DEFAULT 0,
    b DATETIME NOT NULL DEFAULT now()
);

INSERT INTO foo (a) VALUES (123);

What happens here? Well, a row is inserted into table ``foo'' where ``a'' has the value 123 and b holds the date & time of when the create DDL statement was executed. I'm wondering how to get b to have the value of the time whenever any arbitrary insert statement is executed...

Ty

^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: DEFAULT now() ?
@ 2000-04-18 15:55  Margarita Barvinok <brita@umich.edu>
  parent: ty <ty@koi.tsukebe.net>
  0 siblings, 0 replies; 2+ messages in thread

From: Margarita Barvinok @ 2000-04-18 15:55 UTC (permalink / raw)
  To: ty <ty@koi.tsukebe.net>; +Cc: pgsql-admin

On Tue, 18 Apr 2000, ty wrote:

> May someone please show me the necessary code snippets that would allow the value of a column to take the current time ( ala now() ) by default on INSERT?
> 
> Indicative example:
> 
> CREATE TABLE foo (
>     a INTEGER NOT NULL DEFAULT 0,
>     b DATETIME NOT NULL DEFAULT now()
> );
> 
> INSERT INTO foo (a) VALUES (123);
> 
> What happens here? Well, a row is inserted into table ``foo'' 
where ``a'' has the value 123 and b holds 
the date & time of when the create DDL statement was executed. 
I'm wondering how to get b to have the value of the time whenever any
arbitrary insert statement is executed...
> 
> Ty
> 
> 
I think it works OK.

This is my test:
create table table1
(f1 int2 default 0 primary key,
f2 timestamp default now(),
f3 datetime default now()
);

biology=> \d table1
Table    = table1
+---------------------------+----------------------------------+-------+
|              Field        |              Type                |Length |
+---------------------------+----------------------------------+-------+
| f1                        | int2 not null default 0          |2      |
| f2                        | timestamp default now ( )        |4      |
| f3                        | datetime default now ( )         |8      |
+---------------------------+----------------------------------+-------+
Index:    table1_pkey

biology=>insert into table1 (f1) values ('1');
biology=>insert into table1 (f1) values ('2');
biology=>insert into table1 (f1) values ('3');
and so on...

biology=> select * from table1;
f1|f2                    |f3                          
--+----------------------+----------------------------
 1|2000-04-18 11:30:01-04|Tue Apr 18 11:30:01 2000 EDT
 2|2000-04-18 11:30:18-04|Tue Apr 18 11:30:18 2000 EDT
 3|2000-04-18 11:30:24-04|Tue Apr 18 11:30:24 2000 EDT
 4|2000-04-18 11:30:28-04|Tue Apr 18 11:30:28 2000 EDT
 7|2000-04-18 11:41:52-04|Tue Apr 18 11:41:52 2000 EDT
 8|2000-04-18 11:41:59-04|Tue Apr 18 11:41:59 2000 EDT
 9|2000-04-18 11:42:05-04|Tue Apr 18 11:42:05 2000 EDT
(7 rows)


--------------------------------------------------------
Margarita Barvinok                University of Michigan
System Administrator II           Department of Biology
brita@umich.edu
---------------------------------------------------------




^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2000-04-18 15:55 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2000-04-18 10:36 DEFAULT now() ? ty <ty@koi.tsukebe.net>
2000-04-18 15:55 ` Margarita Barvinok <brita@umich.edu>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox