Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace enum type in H2 database?

Tags:

java

mysql

h2

MySQL dialect:

CREATE TABLE My_Table ( my_column enum ('first', 'second', ... 'last'));

H2 dialect:

CREATE TABLE My_Table ( my_column ? ('first', 'second', ... 'last'));

What type is equivalent in H2 too the enum type from MySQL?

like image 431
Igor Kostenko Avatar asked Feb 20 '12 12:02

Igor Kostenko


1 Answers

I'm not sure if this is what you are looking for, but would you could do is use a check constraint:

CREATE TABLE My_Table(my_column varchar(255) 
    check (my_column in ('first', 'second', 'last')));

-- fails:
insert into My_Table values('x');

-- ok:
insert into My_Table values('first');

This will work in H2, Apache Derby, PostgreSQL, HSQLDB, and even SQLite. I didn't test other databases.

like image 89
Thomas Mueller Avatar answered Oct 28 '22 16:10

Thomas Mueller