Re: simple problem

From: Masaru Sugawara <rk73(at)sea(dot)plala(dot)or(dot)jp>
To: "Daniel Ordobas Bortolas" <bortolas(at)inf(dot)UFSM(dot)br>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: simple problem
Date: 2002-03-16 09:23:33
Message-ID: 20020316141353.69D4.RK73@sea.plala.or.jp
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Wed, 06 Mar 2002 15:08:45 GMT
"Daniel Ordobas Bortolas" <bortolas(at)inf(dot)UFSM(dot)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)
select case when (select count(*) from loco) < 3
then new.id_loco
else null
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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message george young 2002-03-16 18:28:00 hashtext function disappears in 7.2?
Previous Message Dima Tkach 2002-03-16 05:26:52 Re: Debugging C functions...