H2 Schema initialization. Syntax error in SQL statement
Asked Answered
S

10

18

I have a spring boot application and I trying to initialize some data on application startup.

This is my application properties:

#Database connection
spring.datasource.url=jdbc:h2:mem:test_db
spring.datasource.username=...
spring.datasource.password=...
spring.datasource.driverClassName=org.h2.Driver

spring.datasource.initialize=true
spring.datasource.schema=schema.sql
spring.datasource.data=schema.sql


#Hibernate configuration
#spring.jpa.hibernate.ddl-auto = none

This is schema.sql:

CREATE TABLE IF NOT EXISTS `Person` (
  `id`         INTEGER  PRIMARY KEY AUTO_INCREMENT,
  `first_name` VARCHAR(50) NOT NULL,
  `age`        INTEGER  NOT NULL,
  PRIMARY KEY(`id`)
);

and data.sql

INSERT INTO `Person` (
  `id`,
  `first_name`,
  `age`
) VALUES (
  1,
  'John',
  20
);

But I got 'Syntax error in SQL statement' on application startup:

19:08:45.642 6474 [main] INFO  o.h.tool.hbm2ddl.SchemaExport - HHH000476: Executing import script '/import.sql'
19:08:45.643 6475 [main] ERROR o.h.tool.hbm2ddl.SchemaExport - HHH000388: Unsuccessful: CREATE TABLE Person (
19:08:45.643 6475 [main] ERROR o.h.tool.hbm2ddl.SchemaExport - Syntax error in SQL statement "CREATE TABLE PERSON ( [*]"; expected "identifier"
Syntax error in SQL statement "CREATE TABLE PERSON ( [*]"; expected "identifier"; SQL statement:

I can't understand, what's wrong with this SQL.

Serrulate answered 30/5, 2017 at 16:16 Comment(0)
D
16

Try this code. Remove PRIMARY KEY(id) and execute it.

CREATE TABLE IF NOT EXISTS `Person` (
    `id`         INTEGER  PRIMARY KEY AUTO_INCREMENT,
     `first_name` VARCHAR(50) NOT NULL,
     `age`        INTEGER  NOT NULL
);
Deeprooted answered 30/5, 2017 at 16:22 Comment(0)
M
10

This error results from the structure of the CREATE TABLE declaration.

It will be the result when you have an extra comma in the end of your SQL declaration--no column declaration following the comma. For example:

CREATE TABLE IF NOT EXISTS `Person` (
  `id`         INTEGER  PRIMARY KEY AUTO_INCREMENT,
  `first_name` VARCHAR(50) NOT NULL,
  `age`        INTEGER  NOT NULL,     --note this line has a comma in the end
);

That's because CREATE TABLE expects a list of the columns that will be created along with the table, and the first parameter of the column is the identifier. As you check here, the column declaration follows the structure:

identifier datatype <constraints> <autoincrement> <functions>

Thus, in your case, as @budthapa and @Vishwanath Mataphati have mentioned, you could simply remove the PRIMARY KEY(id) line from the CREATE TABLE declaration. Moreover, you have already stated that id is a primary key on the first line of the column definitions.

In case you do not have a statement as the PRIMARY KEY declaration, be sure to check for the extra comma following your last column declaration.

Muntjac answered 28/8, 2020 at 13:9 Comment(0)
T
9

I was add below in to application.properties and it work for me

spring.jpa.properties.hibernate.globally_quoted_identifiers=true spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true

Tazza answered 31/10, 2022 at 8:56 Comment(1)
This worked for me. I'm not sure what it does, but it's related to H2 memory database, if you are using one.Dissatisfied
I
3

Try this, as you have used Table_name

CREATE TABLE IF NOT EXISTS Person (
    id        INTEGER  PRIMARY KEY AUTO_INCREMENT,
     first_name VARCHAR(50) NOT NULL,
     age        INTEGER  NOT NULL
);
Iodide answered 12/8, 2020 at 9:21 Comment(0)
E
2

In my case I was missing the semicolon at the end of create table statement.

Eaton answered 8/11, 2023 at 14:40 Comment(0)
C
1

What helped in my case was removing single quotes from the table name in my insert query

I had to change this:

INSERT INTO 'translator' (name, email) VALUES ('John Smith', '[email protected]');

to this:

INSERT INTO translator (name, email) VALUES ('John Smith', '[email protected]');
Camm answered 17/2, 2021 at 10:59 Comment(0)
U
1

You set auto increment id, so you can't insert new record with id.

Try INSERT INTO `Person` (
  `first_name`,
  `age`
) VALUES (
  'John',
  20
);
Ursala answered 8/11, 2021 at 7:23 Comment(0)
F
1

I ran into same issue. I fixed that with these application.properties:

spring.jpa.properties.hibernate.connection.charSet=UTF-8
spring.jpa.properties.hibernate.hbm2ddl.import_files_sql_extractor=org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor

Some issue with multi-line and default encoding.

Fascista answered 7/10, 2022 at 20:32 Comment(0)
C
0

I had the same problem when one of my fields in the JPA Entity had the same name as SQL reserved keyword (FROM).

Before:

@Entity
public class Event {

    ...

    @NotNull
    private LocalDateTime from;

}

Fix:

@Entity
public class Event {

    ...

    @NotNull
    private LocalDateTime fromDate;

}
Carousal answered 6/3, 2023 at 8:53 Comment(0)
S
0

In my case the problem was that I was using an integer field called "year"

Schreck answered 25/5, 2023 at 22:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.