Re: simple problem - Mailing list pgsql-sql

From Masaru Sugawara
Subject Re: simple problem
Date
Msg-id 20020316141353.69D4.RK73@sea.plala.or.jp
Whole thread Raw
In response to simple problem  ("Daniel Ordobas Bortolas" <bortolas@inf.UFSM.br>)
List pgsql-sql
On Wed, 06 Mar 2002 15:08:45 GMT
"Daniel Ordobas Bortolas" <bortolas@inf.UFSM.br> wrote:

> Hello!
> I am having problems creating a table and i'd like some help.
> 
> -->create table loco( id_loco int4 not null, primary key (id_loco), check(
> (select count(*) from loco ) < 3 ) );
> 
> I guess the statment is right, but when:
> ->insert into loco values(1);
> 
> the result is: ERROR:  ExecEvalExpr: unknown expression type 108
> 


By taking advantage of the fact that COUNT() aggregation can be used 
in RULEs(at least in v7.2) and that NULL can't be inserted into columns
defined with NOT NULL , you'll be able to set the limit on  the number of
the rows.  you, however, probably need to take care not to misunderstand the
meaning in the returned error message --  whether it shows the original
meaning or the limit: COUNT() < n.  But I would think that  using TRIGGERs
seems common at the moment. 


create table loco( id_loco int4 not null, primary key (id_loco));
create view loco_view as select id_loco from loco;

create rule rule_loco_view as     on insert to loco_view      do instead insert into loco(id_loco)
selectcase when  (select count(*) from loco) < 3                           then new.id_loco
elsenull                        end ;
 


renew=> insert into loco_view values(1);
INSERT 1712397 1
renew=> insert into loco_view values(1);
ERROR:  Cannot insert a duplicate key into unique index loco_pkey
renew=> insert into loco_view values(2);
INSERT 1712399 1
renew=> insert into loco_view values(null);
ERROR:  ExecAppend: Fail to add null value in not null attribute id_loco
renew=> insert into loco_view values(3);
INSERT 1712400 1
renew=> insert into loco_view values(3);
ERROR:  ExecAppend: Fail to add null value in not null attribute id_loco
renew=>
renew=> select * from loco;id_loco
---------      1      2      3
(3 rows)


Regards,
Masaru Sugawara




pgsql-sql by date:

Previous
From: Dima Tkach
Date:
Subject: Re: Debugging C functions...
Next
From: george young
Date:
Subject: hashtext function disappears in 7.2?