问题描述:今天使用mysql新建了一个表。在插入数据时,报了语法错误。由于使用的是 mabits 代码自动生成工具,并且自己没有改写其中的sql语句。所以确信,sql语句部分应该没有可能写错。因此马上意识到可能表里面的字段出了问题。于是查看表里面的字段,发现有一个desc 字段和mysql中降序的关键字冲突了。然后顺手把这个字段改写成了describe。到这里,以为问题就应该解决了。谁料想,又冲突了。describe也是mysql的关键字。于是上网搜了一下mysql 关键字表。
如下是mysql关键字表
MySQL 5.5 保留关键字
ACCESSIBLE | 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 | 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 |
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 | LEADING | LEAVE |
LEFT | LIKE | LIMIT |
LINEAR | LINES | LOAD |
LOCALTIME | LOCALTIMESTAMP | LOCK |
LONG | LONGBLOB | LONGTEXT |
LOOP | LOW_PRIORITY | MASTER_SSL_VERIFY_SERVER_CERT |
MATCH | MAXVALUE | 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 | RANGE | READ |
READS | READ_WRITE | REAL |
REFERENCES | REGEXP | RELEASE |
RENAME | REPEAT | REPLACE |
REQUIRE | RESIGNAL | RESTRICT |
RETURN | REVOKE | RIGHT |
RLIKE | SCHEMA | SCHEMAS |
SECOND_MICROSECOND | SELECT | SENSITIVE |
SEPARATOR | SET | SHOW |
SIGNAL | 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 | XOR | YEAR_MONTH |
ZEROFILL |
MySQL 允许一些大家常用到的关键字使用英文 ` 号引起来使用,如:
ACTION
BIT
DATE
ENUM
NO
TEXT
TIME
TIMESTAMP
上述表中的关键字,在设计数据库时,应当尽量避免这些不必要的冲突。
问题解决:把关键字替换成了非关键字。
经验总结:通过程序操作数据库时,mysql会解析你代码里的sql语句,如果操作的字段中含有关键字,你写的sql语句就可能被错误解释了,sql语句也就出错了。数据库种类还是比较多的。多多少少都有点关键字,每个版本还略有不同。在设计数据库表的时候,应当设计的字段名特殊一点,尽量长一点,避免和关键字冲突。