Is it possible to create an Algebraic data type in Postgres and then use it as a column type?
For example:
CREATE TYPE hoofed AS ENUM('horse', 'goat'); CREATE TYPE monkey AS ENUM('chimp','macaque'); CREATE TYPE ANIMAL AS ENUM(hoofed, monkey);
This fails:
syntax error at or near "hoofed" LINE 1: CREATE TYPE ANIMAL AS ENUM(hoofed, monkey);
Is it possible to do something like this?
Ultimately, what I would like to do is something like this:
CREATE TABLE zoo ( a ANIMAL, name text ); INSERT INTO zoo(a, name) VALUES('horse', 'bob'); INSERT INTO zoo(a, name) VALUES('macaque', 'jimmy');
And so that both entries are independently valid.
EDIT: @ Abihabi87 the answer below allows me to create, in essence, a product type, but it still does not allow me to create a union type as desired.
enums types sql postgresql union-types
Abraham p
source share