Re: Creating exponential sequences - Mailing list pgsql-novice

From Rod Kreisler
Subject Re: Creating exponential sequences
Date
Msg-id JNEGKNDJGBKLBDGPOPFOIEJMDCAA.rod@23net.net
Whole thread Raw
In response to Creating exponential sequences  (Rod Kreisler <rod@23net.net>)
List pgsql-novice
OK, figured out a workaround:

create sequence "my_seq" start 0 minvalue 0 increment 1 maxvalue 63

CREATE TABLE "example" (
   "ID" int4 DEFAULT (2 ^ (nextval('"my_seq"'))) NOT NULL,
   "description" varchar(32),
   CONSTRAINT "example_pkey" PRIMARY KEY ("ID")
);
CREATE  UNIQUE INDEX "example_description" ON "example" ("description");

Obviously this will only work for small sets (i.e. <=64), but that's exactly
what I'm looking to replace.

Now just got to figure out how to write the constraints...

> -----Original Message-----
> From: pgsql-novice-owner@postgresql.org
> [mailto:pgsql-novice-owner@postgresql.org]On Behalf Of Rod Kreisler
> Sent: Monday, October 07, 2002 10:52 PM
> To: pgsql-novice@postgresql.org
> Subject: [NOVICE] Creating exponential sequences
>
>
> Is there any way to create an exponential sequence rather than
> incremental?
>
> What I would like is a sequence that would start at 1 and grow
> exponentially
> by 2.  (i.e. 1,2,4,8,16,32....)
>
> The advantages are, IMHO, obvious:
>
> With a sequence "my_seq" declared as above create a table as follows:
>
> create table example
> (
>     "ID" int4 DEFAULT nextval('"my_seq"'::text) NOT NULL,
>     "description" varchar(32),
>      CONSTRAINT "example_pkey" PRIMARY KEY ("ID")
> );
> CREATE  UNIQUE INDEX "example_description" ON "example" ("description");
>
> When referenced by another table with a 1:many relationship, instead of
> using a third table, the values can be stored in a single field
> using a sum
> of the "ID"s and reference can be queried using a logical AND.
> Assuming the
> referring table "example2" contains a field "example" which references the
> above table with the pk on "example2" being "e2id":
>
> select "description" from "example", "example2" where ("ID" &
> "example")!=0
> and "e2id"=555;
>
> Am I nuts? This seems so obvious but I've never seen it applied anywhere.
> Of course, I'm by no means a db guru.
>
> Of course, if I can't do it with a sequence, I could write a function....
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/users-lounge/docs/faq.html
>
>


pgsql-novice by date:

Previous
From: Tom Lane
Date:
Subject: Re: Creating exponential sequences
Next
From: Rod Kreisler
Date:
Subject: Re: Creating exponential sequences