automatic incrementation

From: Nuchanard Chiannilkulchai <nuch(at)valigene(dot)com>
To: pgsql-sql(at)hub(dot)org
Subject: automatic incrementation
Date: 1999-06-09 08:47:25
Message-ID: 375E2A1D.FC614366@valigene.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

hello,

I have a table with the field 'id' which i would like to increment at
each insert automatically.
The idea is to create a function which determines the max(id) and then a
rule to insert instead
the real insertion instruction, with the next id value.

-- table
create table toto (
id int4 primary key,
project char(8) not null,
comments text
);
insert into toto values (1,'PTest','ndfjSHJG');
insert into toto values (2,'PTest','ndfjSHJG');
insert into toto values (3,'PTest','ndfjSHJG');

-- function
create function nextid(toto) returns int4
as ' select max($1.id)+1 as id ; '
language 'sql';

**** ERROR: type id lookup of 12 failed
This is my first problem

-- rule
create rule r_id as on insert to toto
do instead
insert into toto (id, project, comments) values (nextid(),
new.project, new.comments);

**** The next problem may be the rule writing !!!
I would like to have , even some other suggestions to make it works.
I can't use a sequence table because I've already one in the database.

Thanks,
Nuch

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Szcząchor Tomasz 1999-06-09 13:03:20 Cursors and waiting users
Previous Message Michal Samek 1999-06-09 08:41:02 Select like when searching for whole word and optimizing it