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
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.
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.
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.
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.
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 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!
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.
CREATE TABLE PLANETICKETS( DESTINATION VARCHAR(10) NOT NULL, TICKETPRICE NUMERIC(8,2) NOT NULL, TOURISTINFO_ID INT )
ALTER TABLE PLANETICKETS ADD FOREIGN KEY (TOURISTINFO_ID) REFERENCES TOURISTINFO(TOURISTINFO_ID)
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) )
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With