Search code examples
mysqlspring-bootmysql-connector

MySQL Error : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use'


Actually, I am using Spring boot and there is an error in creating the table in the database. I am using MySQL as my database

the error are

Error Showing in Spring Boot

java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'like bigint not null, time_created time, primary key (blog_id)) engine=InnoDB' at line 1

Error when I try to manually create the table

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'like bigint not null, time_created time, primary key (blog_id)) engine=InnoDB' at line 1

My command

create table blogs (blog_id integer not null, date_created date, discription varchar(999), dislike bigint not null, like bigint not null, time_created time, primary key (blog_id)) engine=InnoDB;

And my Entity

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int blogId;

@Column(length = 999)
private String discription;

@CreatedDate
private LocalDate dateCreated;

private LocalTime timeCreated;
private long like;
private long dislike;


public blogs(int blogId, String discription, LocalDate dateCreated, LocalTime timeCreated, long like,
        long dislike) {
    this.blogId = blogId;
    this.discription = discription;
    this.dateCreated = dateCreated;
    this.timeCreated = timeCreated;
    this.like = like;
    this.dislike = dislike;
}

Solution

  • The Error lies in your SQL Statement. LIKE is a keyword/reserved word and cannot be used as a name of an attribute.

    Change your SQL Statement of the blogs table creation to the following one:

    CREATE TABLE blogs (
        blog_id        integer        not null, 
        date_created   date, 
        discription    varchar(999), 
        dislike        bigint         not null, 
        liked          bigint         not null, 
        time_created   time, 
        primary key (blog_id)
    ) engine=InnoDB;