enumerated type..

Started by raptor@tvskat.netover 21 years ago4 messagesgeneral
Jump to latest
#1raptor@tvskat.net
raptor@tvskat.net

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

#2Franco Bruno Borghesi
franco@akyasociados.com.ar
In reply to: raptor@tvskat.net (#1)
Re: enumerated type..

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:

Show quoted text

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)

#3Michael Kleiser
mkl@webde-ag.de
In reply to: raptor@tvskat.net (#1)
Re: enumerated type..

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:

Show quoted text

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)

#4Bruno Wolff III
bruno@wolff.to
In reply to: Michael Kleiser (#3)
Re: enumerated type..

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.