转载地址:http://blog.itechol.com/space.php?uid=33&do=blog&id=6681
问题描述:
工作中搭建gitlab服务器,在使用mysql数据库测试数据插入时出现错误,根据错误提示需查看gitlabhq_production数据库中keys表的相关信息,可执行查询语句中总是报错,如下示:
mysql> select * from
keys;
ERROR 1064 (42000): 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 'keys' at line 1
mysql> desc
keys;
ERROR 1064 (42000): 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 'keys' at line 1
加上反引号(1左边的字符)后再次执行成功
mysql> select * from `keys`\G;
mysql> select * from `keys`\G;
*************************** 1. row ***************************
id: 6
user_id: NULL
created_at: 2013-07-08 10:28:39
updated_at: 2013-07-08 10:28:39
key: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDC/hNktBYwu07csO1oy1o5JPTPQKLEWj/iOaKB4DYQdN/5OpCSSwXdxFkuDA/8Zt/R0XXy61QQ3arzaqZXqqnWUcf4AJ8cbnYqrf/F2wX2ThYBvSud86wQqCC1bQHkqc7ocnwUkK8rEEqejnJhPeQkeW5LIFJEpVLfZCibqNPSFpCCdHtoj8OyOMqr0RyVtny60kOXDXU47EbHpCFVgsmnQlF48AQRuTt5/VxIF2fkCuUofBA97yu6+t2ZTscJbcJhpTtxALgBluZ1NfXWWuad7yXTLeK/xK7/rc9MF8BbBH9+POAvICN9AGThw0lcANCxt3mvW8LmtmRwvQgKdbxV lidong@eswine.com
title: lidong@eswine.com
identifier: NULL
type: DeployKey
*************************** 2. row ***************************
以下略…………。
mysql> desc
`keys`;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | YES | MUL | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
| key | text | YES | | NULL | |
| title | varchar(255) | YES | | NULL | |
| identifier | varchar(255) | YES | MUL | NULL | |
| type | varchar(255) | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)
问题分析:
为何必须加上反引号?
它是为了区分MYSQL的保留字与普通字符而引入的符号。
举个例子:SELECT `select` FROM `test` WHERE select='字段值'
在test表中,有个select字段,如果不用反引号,MYSQL将把select视为保留字而导致出错,所以,有MYSQL保留字作为字段的,必须加上反引号来区分。
引号一般用在字段的值,如果字段值是字符或字符串,则要加引号,如:select='字段值'
不加反引号建的表不能包含MYSQL保留字,否则出错
反引号`,数字1左边的符号。
保留字不能用于表名,比如desc,此时需要加入反引号来区别,但使用表名时可忽略反引号。
create table desc报错
create table `desc`成功
create table `test`成功
drop table test成功
保留字不能用于字段名,比如desc,此时也需要加入反引号,并且insert等使用时也要加上反引号。
create table `test`(`desc` varchar(255))成功
insert into test(desc) values('fxf')失败
insert into test(`desc`) values('fxf')成功
mysql常见的保留字
=======================================================
使用mysql时一定要注意,不要使用它的保留字作为表名或者列名,否则会出现莫名其妙的错误。
注:MySQL允许部分关键字用做未引起来的识别符,因为许多人以前曾使用过它们。如:ACTION、BIT、DATE、ENUM、NO、TEXT、TIME、TIMESTAMP
昨天建了一个表,其中一列名为interval(时间间隔),结果数据死都插不进去,最后才发现原来interval是mysql的保留字。
以后出现类似的错误要先想想是不是因为表名或列名冲突造成的啊。
从网上找了一个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 |