问题:
今天在用hibernate建一张订单表的时候遇到了一个问题,订单的信息表始终无法创建,莫名其妙,最后发现和sql语句的关键字冲突了。现在分享一下。
报错信息如下:(指的sql语句语法错误)
01:20:45,317 ERROR SchemaUpdate:212 - Unsuccessful: create table 'order' (id integer not null auto_increment, name varchar(255), customer_id integer, primary key (id))
01:20:45,317 ERROR SchemaUpdate:213 - 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 ''order' (
id integer not null auto_increment,
name varchar(255),' at line 1
01:20:45,463 ERROR SchemaUpdate:212 - Unsuccessful: alter table 'order' add index FKD2F26980230E719A (customer_id), add constraint FKD2F26980230E719A foreign key (customer_id) references customer (id)
01:20:45,463 ERROR SchemaUpdate:213 - 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 ''order'
add index FKD2F26980230E719A (customer_id),
add constr' at line 1
原因:
建订单表的时候,表的名字用的是order,由于order是mysql的关键字,因为hibernate生成表的时候依赖的是mysql,所以解析的时候就会报错。但是用可视化工具建表时,表名是order是可以的。
在网上查找说可以用分号、小括号或者中括号,但是试了好像都不可以。
1.在建表的时候尽量避免关键字,表名尽量使用x_xxx这样就不会和关键字冲突了,表名一般小写。
2.不要使用mysql的保留字。