MySQL标识符问题

时间:2009-08-27 04:11:45

标签: mysql

来自mysql认证指南的示例问题......

问题

Will the following SQL statement succeed or result in an error? Assume that the table to be created doesn't already exist.

CREATE TABLE MD5 (id INT);

答案

The statement results in an error if the IGNORE_SPACE SQL mode is enabled. In that case, all function names become reserved words and the statement would return an error because MD5 is a function name.

If the IGNORE_SPACE SQL mode is not enabled, the statement succeeds:

mysql> CREATE TABLE MD5 (id INT);
Query OK, 0 rows affected


Note that the IGNORE_SPACE SQL mode is part of the ANSI SQL mode. 

为什么他们谈论空间?有人有任何想法吗?什么是正确的答案?它失败是因为函数是保留字?引用时会成功吗,例如。用反叛......对吧?

2 个答案:

答案 0 :(得分:2)

IGNORE_SPACE模式允许函数名和下面的括号之间的空格。如果启用,则所有函数名称都成为保留字,因为MySQL无法确定您是在调用函数还是声明(使用)标识符。

因此,如果它已启用,您的CREATE TABLE将失败,并且会出现以下情况:

SELECT MD5 (some_column) FROM some_table

会奏效。如果IGNORE_SPACE被禁用,则会发生相反的情况(CREATE TABLE将起作用,而SELECT则不会,因为函数名和括号之间有空格。)

答案 1 :(得分:0)

IGNORE_SPACE将忽略潜在函数名称与(

之间的空格

Mysql's manual page on function resolution explains it all,但这是简短版本:

SELECT md5(colum1) FROM table;

将md5函数应用于colum1。

 SELECT md5 (colum1) FROM table;

如果启用了IGNORE_SPACE,则将md5函数应用于colum1。

如果IGNORE_SPACE关闭,“md5(”不会被视为函数调用,因为那里有空格。

使用反引号使得Mysql不是这个词,而是一个函数名,而是一些数据库,表,列等标识符。