Обсуждение: DEFAULT now() ?

Поиск
Список
Период
Сортировка

DEFAULT now() ?

От
"ty"
Дата:
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
 

Re: DEFAULT now() ?

От
Margarita Barvinok
Дата:
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
---------------------------------------------------------