mysql字段key_Mysql使⽤“key”作为字段名会报错(key为保留
关键字)
环境
Spring boot Version : 2
Mysql Version : 5.7
Hibernate dialect : org.hibernate.dialect.MySQL5Dialect
报错信息
Caused by: ptions.jdbc4.MySQLSyntaxErrorException: 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 'varchar(255), userId_id bigint, primary key (id)) engine=MyISAM' at line 1
新建⼀个Entity,Hibernate在创建表时报的错误。
报错的Entity:import lombok.Data;
import javax.persistence.*;
import java.sql.Timestamp;
/**
* @author mylicharm
* @@outlook
* @date 2018/8/15 18:52
*/
@Data
@Entity
@Table(name = "com_system_parameter")
public class SystemParameter {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne(targetEntity = User.class)
private Long userId;
private String key;
private String value;
private String description;
@Column(insertable = true)
private Timestamp createdAt;
@Column(insertable = true, updatable = true)
private Timestamp updatedAt;
}
解决过程
错误信息中提⽰语法错误,在'varchar(255), userId_id bigint, primary key (id)) engine=MyISAM'附近,通过搜索,基本确定问题为entity中定义有问题,采⽤⼀⼀排除法确定问题,先将其他的字段注释掉,看能不能成功建表,再⼀个⼀个的取消注释。
最后发现,巨坑的地⽅!使⽤key作为字段名会报错!改为keyword就正常了。如果直接写SQL语句可以使⽤`符号把key包住(`key`),可以避免提⽰错误。
最后附上Mqsql保留关键字⼤全:
Table 2.1. Reserved Words in MySQL 5.0
ADDALLALTER
ANALYZEANDAS
ASCASENSITIVEBEFORE
BETWEENBIGINTBINARY
BLOBBOTHBY
CALLCASCADECASE
CHANGECHARCHARACTER
CHECKCOLLATECOLUMN
CONDITIONCONNECTION[a]CONSTRAINT
CONTINUECONVERTCREATE
CROSSCURRENT_DATECURRENT_TIME
CURRENT_TIMESTAMPCURRENT_USERCURSOR
DATABASEDATABASESDAY_HOUR
DAY_MICROSECONDDAY_MINUTEDAY_SECONDforeign key references用法
DECDECIMALDECLARE
DEFAULTDELAYEDDELETE
DESCDESCRIBEDETERMINISTIC
DISTINCTDISTINCTROWDIV
DOUBLEDROPDUAL
EACHELSEELSEIF
ENCLOSEDESCAPEDEXISTS
EXITEXPLAINFALSE
FETCHFLOATFLOAT4
FLOAT8FORFORCE
FOREIGNFROMFULLTEXT
GOTO[b]GRANTGROUP
HAVINGHIGH_PRIORITYHOUR_MICROSECOND HOUR_MINUTEHOUR_SECONDIF IGNOREININDEX
INFILEINNERINOUT
INSENSITIVEINSERTINT
INT1INT2INT3
INT4INT8INTEGER
INTERVALINTOIS
ITERATEJOINKEY
KEYSKILLLABEL[c]
LEADINGLEAVELEFT
LIKELIMITLINES LOADLOCALTIMELOCALTIMESTAMP LOCKLONGLONGBLOB
LONGTEXTLOOPLOW_PRIORITY MATCHMEDIUMBLOBMEDIUMINT MEDIUMTEXTMIDDLEINTMINUTE_MICROSECOND MINUTE_SECONDMODMODIFIES NATURALNOTNO_WRITE_TO_BINLOG NULLNUMERICON OPTIMIZEOPTIONOPTIONALLY
ORORDEROUT
OUTEROUTFILEPRECISION PRIMARYPROCEDUREPURGE
READREADSREAL REFERENCESREGEXPRELEASE RENAMEREPEATREPLACE REQUIRERESTRICTRETURN REVOKERIGHTRLIKE SCHEMASCHEMASSECOND_MICROSECOND SELECTSENSITIVESEPARATOR SETSHOWSMALLINT SONAMESPATIALSPECIFIC SQLSQLEXCEPTIONSQLSTATE
SQLWARNINGSQL_BIG_RESULTSQL_CALC_FOUND_ROWS SQL_SMALL_RESULTSSLSTARTING
STRAIGHT_JOINTABLETERMINATED THENTINYBLOBTINYINT
TINYTEXTTOTRAILING
TRIGGERTRUEUNDO
UNIONUNIQUEUNLOCK
UNSIGNEDUPDATEUPGRADE[d]
USAGEUSEUSING
UTC_DATEUTC_TIMEUTC_TIMESTAMP VALUESVARBINARYVARCHAR VARCHARACTERVARYINGWHEN
WHEREWHILEWITH
WRITEXORYEAR_MONTH
ZEROFILL

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。