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 ...
Hibernate 出现这样的错误是因为表中的字段名使用了sql的保留字,不要使用保留字
一、mysql的保留字主要有:
ADD | ALL | ALTER |
ANALYZE | AND | AS |
ASC | ASENSITIVE | BEFORE |
BETWEEN | BIGINT | BINARY |
BLOB | BOTH | BY |
CALL | CASCADE | CASE |
CHANGE | CHAR | CHARACTER |
CHECK | COLLATE | COLUMN |
CONDITION | CONNECTION | CONSTRAINT |
CONTINUE | CONVERT | CREATE |
CROSS | CURRENT_DATE | CURRENT_TIME |
CURRENT_TIMESTAMP | CURRENT_USER | CURSOR |
DATABASE | DATABASES | DAY_HOUR |
DAY_MICROSECOND | DAY_MINUTE | DAY_SECOND |
DEC | DECIMAL | DECLARE |
DEFAULT | DELAYED | DELETE |
DESC | DESCRIBE | DETERMINISTIC |
DISTINCT | DISTINCTROW | DIV |
DOUBLE | DROP | DUAL |
EACH | ELSE | ELSEIF |
ENCLOSED | ESCAPED | EXISTS |
EXIT | EXPLAIN | FALSE |
FETCH | FLOAT | FLOAT4 |
FLOAT8 | FOR | FORCE |
FOREIGN | FROM | FULLTEXT |
GOTO | GRANT | GROUP |
HAVING | HIGH_PRIORITY | HOUR_MICROSECOND |
HOUR_MINUTE | HOUR_SECOND | IF |
IGNORE | IN | INDEX |
INFILE | INNER | INOUT |
INSENSITIVE | INSERT | INT |
INT1 | INT2 | INT3 |
INT4 | INT8 | INTEGER |
INTERVAL | INTO | IS |
ITERATE | JOIN | KEY |
KEYS | KILL | LABEL |
LEADING | LEAVE | LEFT |
LIKE | LIMIT | LINEAR |
LINES | LOAD | LOCALTIME |
LOCALTIMESTAMP | LOCK | LONG |
LONGBLOB | LONGTEXT | LOOP |
LOW_PRIORITY | MATCH | MEDIUMBLOB |
MEDIUMINT | MEDIUMTEXT | MIDDLEINT |
MINUTE_MICROSECOND | MINUTE_SECOND | MOD |
MODIFIES | NATURAL | NOT |
NO_WRITE_TO_BINLOG | NULL | NUMERIC |
ON | OPTIMIZE | OPTION |
OPTIONALLY | OR | ORDER |
OUT | OUTER | OUTFILE |
PRECISION | PRIMARY | PROCEDURE |
PURGE | RAID0 | RANGE |
READ | READS | REAL |
REFERENCES | REGEXP | RELEASE |
RENAME | REPEAT | REPLACE |
REQUIRE | RESTRICT | RETURN |
REVOKE | RIGHT | RLIKE |
SCHEMA | SCHEMAS | SECOND_MICROSECOND |
SELECT | SENSITIVE | SEPARATOR |
SET | SHOW | SMALLINT |
SPATIAL | SPECIFIC | SQL |
SQLEXCEPTION | SQLSTATE | SQLWARNING |
SQL_BIG_RESULT | SQL_CALC_FOUND_ROWS | SQL_SMALL_RESULT |
SSL | STARTING | STRAIGHT_JOIN |
TABLE | TERMINATED | THEN |
TINYBLOB | TINYINT | TINYTEXT |
TO | TRAILING | TRIGGER |
TRUE | UNDO | UNION |
UNIQUE | UNLOCK | UNSIGNED |
UPDATE | USAGE | USE |
USING | UTC_DATE | UTC_TIME |
UTC_TIMESTAMP | VALUES | VARBINARY |
VARCHAR | VARCHARACTER | VARYING |
WHEN | WHERE | WHILE |
WITH | WRITE | X509 |
XOR | YEAR_MONTH | ZEROFILL |
二、MySQL中字段名和保留字冲突的解决
只需要用撇号(`)把字段名括起来,这样在select、insert、update、delete等语句中都不会出现语法错误
代码如下:
select col from table1;
这句sql没有问题,当把col换成range
代码如下:
select range from table1;
这句sql有问题
当字段名与MySQL保留字冲突时,可以用撇号字符“`”(TAB键上面那个,也就是数字1键前面那个键上的)将字段名括起来:
代码如下:
select `range` from table;
三、SQLserver中字段名和保留字冲突的解决
只需要用[]把字段名括起来,这样在select、insert、update、delete等语句中都不会出现语法错误
四、Oracle中字段名和保留字冲突的解决
(1)官方文档说明:
Reserved words and keywordsare identifiers that have special meaning in PL/SQL.
You cannot use reserved words as ordinary user-defined identifiers. You can use them as quoted user-defined identifiers, but it is not recommended. For more information, see"QuotedUser-Defined Identifiers".
You can use keywords as ordinary user-defined identifiers,but it is not recommended.
译文:
保留字和keywordsare标识符,在PL / SQL的特殊意义。
不能使用保留字作为普通用户定义标识符。您可以使用它们作为引用的用户定义标识符,但不推荐使用。更多信息,见“quoteduser定义标识符”。
您可以使用关键字作为普通用户定义的标识符,但不推荐使用。
(2)不能直接使用保留字作为用户定义的变量,但加上双引号就使用。
如:
create tale int(
id varchar(10),
name varchar(10),
primary key id);
出现错误,int是关键字
create table "int"(
id varchar(10),
name varchar(10),
primary key id);
这样就正确
(3)保留字和关键字
保留字:
Begins with: | Reserved Words |
---|---|
A | ALL, ALTER, AND, ANY, AS, ASC, AT |
B | BEGIN, BETWEEN, BY |
C | CASE, CHECK, CLUSTERS, CLUSTER, COLAUTH, COLUMNS, COMPRESS, CONNECT, CRASH, CREATE, CURSOR |
D | DECLARE, DEFAULT, DESC, DISTINCT, DROP |
E | ELSE, END, EXCEPTION, EXCLUSIVE |
F | FETCH, FOR, FROM, FUNCTION |
G | GOTO, GRANT, GROUP |
H | HAVING |
I | IDENTIFIED, IF, IN, INDEX, INDEXES, INSERT, INTERSECT, INTO, IS |
L | LIKE, LOCK |
M | MINUS, MODE |
N | NOCOMPRESS, NOT, NOWAIT, NULL |
O | OF, ON, OPTION, OR, ORDER, OVERLAPS |
P | PROCEDURE, PUBLIC |
R | RESOURCE, REVOKE |
S | SELECT, SHARE, SIZE, SQL, START, SUBTYPE |
T | TABAUTH, TABLE, THEN, TO, TYPE |
U | UNION, UNIQUE, UPDATE |
V | VALUES, VIEW, VIEWS |
W | WHEN, WHERE, WITH |
关键字:
Begins with: | Keywords |
---|---|
A | A, ADD, AGENT, AGGREGATE, ARRAY, ATTRIBUTE, AUTHID, AVG |
B | BFILE_BASE, BINARY, BLOB_BASE, BLOCK, BODY, BOTH, BOUND, BULK, BYTE |
C | C, CALL, CALLING, CASCADE, CHAR, CHAR_BASE, CHARACTER, CHARSET, CHARSETFORM, CHARSETID, CLOB_BASE, CLOSE, COLLECT, COMMENT, COMMIT, COMMITTED, COMPILED, CONSTANT, CONSTRUCTOR, CONTEXT, CONTINUE, CONVERT, COUNT, CURRENT, CUSTOMDATUM |
D | DANGLING, DATA, DATE, DATE_BASE, DAY, DEFINE, DELETE, DETERMINISTIC, DOUBLE, DURATION |
E | ELEMENT, ELSIF, EMPTY, ESCAPE, EXCEPT, EXCEPTIONS, EXECUTE, EXISTS, EXIT, EXTERNAL |
F | FINAL, FIXED, FLOAT, FORALL, FORCE |
G | GENERAL |
H | HASH, HEAP, HIDDEN, HOUR |
I | IMMEDIATE, INCLUDING, INDICATOR, INDICES, INFINITE, INSTANTIABLE, INT, INTERFACE, INTERVAL, INVALIDATE, ISOLATION |
J | |
L | LANGUAGE, LARGE, LEADING, LENGTH, LEVEL, LIBRARY, LIKE2, LIKE4, LIKEC, LIMIT, LIMITED, LOCAL, LONG, LOOP |
M | MAP, MAX, MAXLEN, MEMBER, MERGE, MIN, MINUTE, MOD, MODIFY, MONTH, MULTISET |
N | NAME, NAN, NATIONAL, NATIVE, NCHAR, NEW, NOCOPY, NUMBER_BASE |
O | OBJECT, OCICOLL, OCIDATE, OCIDATETIME, OCIDURATION, OCIINTERVAL, OCILOBLOCATOR, OCINUMBER, OCIRAW, OCIREF, OCIREFCURSOR, OCIROWID, OCISTRING, OCITYPE, OLD, ONLY, OPAQUE, OPEN, OPERATOR, Oracle, ORADATA, ORGANIZATION, ORLANY, ORLVARY, OTHERS, OUT, OVERRIDING |
P | PACKAGE, PARALLEL_ENABLE, PARAMETER, PARAMETERS, PARENT, PARTITION, PASCAL, PIPE, PIPELINED, PRAGMA, PRECISION, PRIOR, PRIVATE |
R | RAISE, RANGE, RAW, READ, RECORD, REF, REFERENCE, RELIES_ON, REM, REMAINDER, RENAME, RESULT, RESULT_CACHE, RETURN, RETURNING, REVERSE, ROLLBACK, ROW |
S | SAMPLE, SAVE, SAVEPOINT, SB1, SB2, SB4, SECOND, SEGMENT, SELF, SEPARATE, SEQUENCE, SERIALIZABLE, SET, SHORT, SIZE_T, SOME, SPARSE, SQLCODE, SQLDATA, SQLNAME, SQLSTATE, STANDARD, STATIC, STDDEV, STORED, STRING, STRUCT, STYLE, SUBMULTISET, SUBPARTITION, SUBSTITUTABLE, SUM, SYNONYM |
T | TDO, THE, TIME, TIMESTAMP, TIMEZONE_ABBR, TIMEZONE_HOUR, TIMEZONE_MINUTE, TIMEZONE_REGION, TRAILING, TRANSACTION, TRANSACTIONAL, TRUSTED |
U | UB1, UB2, UB4, UNDER, UNSIGNED, UNTRUSTED, USE, USING |
V | VALIST, VALUE, VARIABLE, VARIANCE, VARRAY, VARYING, VOID |
W | WHILE, WORK, WRAPPED, WRITE |
Y | YEAR |
Z | ZONE |