Re: DEFAULT now() ?

From: Margarita Barvinok <brita(at)umich(dot)edu>
To: ty <ty(at)koi(dot)tsukebe(dot)net>
Cc: pgsql-admin(at)postgresql(dot)org
Subject: Re: DEFAULT now() ?
Date: 2000-04-18 15:55:06
Message-ID: Pine.SOL.4.10.10004181147410.27628-100000@joust.gpcc.itd.umich.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)umich(dot)edu
---------------------------------------------------------

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Andrew Perrin - Demography 2000-04-18 16:18:35 Re: sequence
Previous Message Raul Carvalho 2000-04-18 10:58:11 RE: Maintenance