Thread: enumerated type..

enumerated type..

From
"raptor@tvskat.net"
Date:
hi,

Is it possible in Postgre to have enum type,  so that later table fileld get values from 1-6 (saving space)

1 - statex
2 - stateY
3 - stateZ

something like that and then constrain on this enum type..

tia

Re: enumerated type..

From
Franco Bruno Borghesi
Date:
CREATE DOMAIN is the answer:

CREATE DOMAIN mytype AS INT2 CHECK(VALUE>=1 AND VALUE<=6);

On Mon, 2004-07-19 at 10:43, raptor@tvskat.net wrote:
hi,

Is it possible in Postgre to have enum type,  so that later table fileld get values from 1-6 (saving space)

1 - statex
2 - stateY
3 - stateZ

something like that and then constrain on this enum type..

tia

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command   (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
Attachment

Re: enumerated type..

From
Michael Kleiser
Date:
I don`t know, if there is a special feature in postgresql for this.
The standard SQL-way is to create a lookup-table and define a foreign-key to it.

create table states( state_id int primary key not null
                    , state_text varchar(30) not null
                    , constraint uk_states_state_text unique( state_text ) );


create table states( state_id int primary key not null , state_text varchar(30) not null );
alter table states add constraint uk_states_state_text unique( state_text );

create table your_table (
...
   state_id int not null
, constraint fk_yt_state_id foreign key (state_id) references  states
);




raptor@tvskat.net schrieb:
> hi,
>
> Is it possible in Postgre to have enum type,  so that later table fileld get values from 1-6 (saving space)
>
> 1 - statex
> 2 - stateY
> 3 - stateZ
>
> something like that and then constrain on this enum type..
>
> tia
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
>     (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)


Re: enumerated type..

From
Bruno Wolff III
Date:
On Mon, Jul 19, 2004 at 16:46:08 +0200,
  Michael Kleiser <mkl@webde-ag.de> wrote:
> I don`t know, if there is a special feature in postgresql for this.
> The standard SQL-way is to create a lookup-table and define a foreign-key
> to it.

You could also use a domain which constrains one of its columns to a
few values. I don't think this would be appropiate for a large list,
like the list of use states. But you might do this when there are fewer
values and you don't expect the list to change.