Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining a Foreign key constraint in H2 Databases

I am new in coding so I made a tables in SQL server and it worked, so i used the same command in H2 and it said I have a syntax problems with the second table, someone can help?

CREATE TABLE TOURISTINFO( TOURISTINFO_ID INT PRIMARY KEY, NAME VARCHAR(25) NOT NULL, NATIONALITY VARCHAR(15) NOT NULL )  CREATE TABLE PLANETICKETS( DESTINATION VARCHAR(10) NOT NULL, TICKETPRICE NUMERIC(8,2) NOT NULL, TOURISTINFO_ID INT FOREIGN KEY REFERENCES TOURISTINFO ) 

The error is

Syntax error in SQL statement "CREATE TABLE PLANETICKETS(  DESTINATION VARCHAR(10) NOT NULL,  TICKETPRICE NUMERIC(8,2) NOT NULL,  TOURISTINFO_ID INT FOREIGN[*] KEY REFERENCES TOURISTINFO  )"; expected "(, FOR, UNSIGNED, NOT, NULL, AS, DEFAULT, GENERATED, NOT, NULL, AUTO_INCREMENT, BIGSERIAL, SERIAL, IDENTITY, NULL_TO_DEFAULT, SEQUENCE, SELECTIVITY, COMMENT, CONSTRAINT, PRIMARY, UNIQUE, NOT, NULL, CHECK, REFERENCES, ,, )"; SQL statement: CREATE TABLE PLANETICKETS(  DESTINATION VARCHAR(10) NOT NULL,  TICKETPRICE NUMERIC(8,2) NOT NULL,  TOURISTINFO_ID INT FOREIGN KEY REFERENCES TOURISTINFO  ) [42001-173] 42001/42001 
like image 507
l3kov Avatar asked Jan 06 '17 22:01

l3kov


People also ask

Does H2 support foreign key?

H2 Alter Table Add Foreign Key. The RazorSQL alter table tool includes an Add Foreign Key option for adding foreign keys to H2 database tables. The add foreign key function lists all of the columns of the table and allows the user to choose one or more columns to add to the foreign key for the table.

When you define a foreign key constraint?

A foreign key joins a table to another table by referencing its primary key. A foreign key constraint specifies that the key can only contain values that are in the referenced primary key, and thus ensures the referential integrity of data that is joined on the two keys.

How do I create a schema in H2 database?

You could run a script, or just a statement or two: String url = "jdbc:h2:mem:test;" + "INIT=CREATE SCHEMA IF NOT EXISTS TEST" String url = "jdbc:h2:mem:test;" + "INIT=CREATE SCHEMA IF NOT EXISTS TEST\\;" + "SET SCHEMA TEST"; String url = "jdbc:h2:mem;" + "INIT=RUNSCRIPT FROM '~/create.

What is the FOREIGN KEY constraint?

The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables. A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY in another table. The table with the foreign key is called the child table, and the table with the primary key is called the referenced or parent table.

What is a foreign key?

A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY in another table. The table with the foreign key is called the child table, and the table with the primary key is called the referenced or parent table. Look at the following two tables:

When defining a foreign key H2 can omit the touristinfo_ID column?

When you define the FOREIGN KEY in this case you can omit to reference explicitly the TOURISTINFO_ID column because H2 knows what column is the primary key in PLANETICKETS. Thanks for contributing an answer to Stack Overflow!

What are primary key constraints?

This topic contains the following sections. Primary Key Constraints. A table typically has a column or combination of columns that contain values that uniquely identify each row in the table. This column, or columns, is called the primary key (PK) of the table and enforces the entity integrity of the table.


2 Answers

Two-step process

  1. Create the table without a foreign key
CREATE TABLE PLANETICKETS(     DESTINATION VARCHAR(10) NOT NULL,     TICKETPRICE NUMERIC(8,2) NOT NULL,     TOURISTINFO_ID INT  ) 
  1. Add the foreign key constraint
 ALTER TABLE PLANETICKETS     ADD FOREIGN KEY (TOURISTINFO_ID)      REFERENCES TOURISTINFO(TOURISTINFO_ID) 

One-step process

CREATE TABLE PLANETICKETS(   DESTINATION VARCHAR(10) NOT NULL,   TICKETPRICE NUMERIC(8,2) NOT NULL,   TOURISTINFO_ID INT,   foreign key (TOURISTINFO_ID) references touristinfo(TOURISTINFO_ID) ) 
like image 101
David Brossard Avatar answered Oct 15 '22 21:10

David Brossard


I would add one option to @david-brossard's answer:

CREATE TABLE PLANETICKETS(   DESTINATION VARCHAR(10) NOT NULL,   TICKETPRICE NUMERIC(8,2) NOT NULL,   TOURISTINFO_ID INT,    CONSTRAINT FK_PLANETICKET_TOURIST FOREIGN KEY (TOURISTINFO_ID) REFERENCES TOURISTINFO(TOURISTINFO_ID) ) 

By using a Constaint Name Definition the foreign key is named explicitly, otherwise H2 assigns it a name based on it's own naming scheme e.g. CONSTRAINT_74.

I feel this makes it safer to manage the constraint later by avoiding ambiguity on use of the name and referencing the name directly defined previously e.g.

ALTER TABLE PLANETICKETS DROP CONSTRAINT FK_PLANETICKET_TOURIST; ALTER TABLE PLANETICKETS ADD CONSTRAINT FK_PLANETICKET_TOURIST FOREIGN KEY (TOURISTINFO_ID) REFERENCES TOURISTINFO(TOURISTINFO_ID)  ON DELETE CASCADE; 

I have started doing this as standard, based on my use of Flyway for an installable software product.

In theory the sequence of Flyway migrations should result in constraints (including Foreign Keys) being applied in the same order and therefore H2 should assign the same name in each copy of database. However, the worry point is removed if a direct name is assigned - one which is referenced in previous migration scripts - rather than one deduced from checking the assigned name in a single database instance.

like image 27
Dazed Avatar answered Oct 15 '22 22:10

Dazed