Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table creation with h2 database

I am new to h2.I just using h2 in spring embedded mode with hibernate.I am trying to execute the following scripts using h2.

CREATE TABLE acct_authority (
  id bigint(20) NOT NULL auto_increment,
  name varchar(255) NOT NULL default '',
  value varchar(255) NOT NULL,
  PRIMARY KEY  (id),
  UNIQUE KEY name (name)
);

The table acct_authority is created without any error.But if i create another table with the following script.

CREATE TABLE acct_role (
  id bigint(20) NOT NULL auto_increment,
  name varchar(255) NOT NULL default '',
  PRIMARY KEY  (id),
  UNIQUE KEY name (name)
);

It shows error as constraint name allready exists.What mistake i did.

like image 755
Kamalam Avatar asked Aug 09 '12 13:08

Kamalam


People also ask

Can we create table in H2 database?

The H2 Create Table Tool allows users to visually create tables. After entering in the table name and the number of columns, the tool allows the user to enter the following information for each column of the table. Column Type (for example, Integer, Char, Varchar, etc.)

How do I access my H2 database table?

Start the spring boot application and access the console in the browser with this URL : http://localhost:8080/h2 . We can see the console like this. Now enter the configured username and password. We can verify the table structure and default data inserted through SQL files.

Is H2 DB good for production?

It can be embedded in Java applications or run in the client-server mode. Mainly, H2 database can be configured to run as inmemory database, which means that data will not persist on the disk. Because of embedded database it is not used for production development, but mostly used for development and testing.


1 Answers

You tried to create two constraints with same name. As you see, both CREATE TABLE statements contain following:

UNIQUE KEY name (name)

Result is that first one creates constraint named name, and second one fails because constraint name already exists. Problem can be solved by using unique names. Also in general it makes sense have little bit more descriptive names for database objects. Maybe you can use for example something like following:

  UNIQUE KEY acct_authority_name_UNIQUE (name)  
  ...  
  UNIQUE KEY acct_role_name_UNIQUE (name)
like image 153
Mikko Maunu Avatar answered Oct 21 '22 15:10

Mikko Maunu