問題:
今天在用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的保留字。