为什么Oracle正在运行这个(错误的)查询

时间:2015-04-08 09:18:02

标签: sql oracle

为什么Oracle会运行这个(错误的)查询?

SELECT * FROM CUSTOMERS WHERE CUSTOMER_TYPE_ID = 1ORDER BY ID;

1ORDER

之间没有空格

2 个答案:

答案 0 :(得分:4)

在Oracle中,变量名称或标识符以下划线(" _")或字母开头。因此,对于1order,解释器知道没有标识符,它必须是一个数字,因此它会尝试获取数字并将其余部分分开并成功。

答案 1 :(得分:1)

查看解释计划,您可以看到Oracle可以解析过滤谓词,并且该查询被认为是有效的。

SQL> EXPLAIN PLAN FOR
  2  SELECT * FROM OE.CUSTOMERS WHERE CUSTOMER_ID = 232ORDER BY CUSTOMER_ID;

Explained.

SQL>
SQL> SELECT * FROM TABLE(dbms_xplan.display);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------
Plan hash value: 4238351645

--------------------------------------------------------------------------------------------
| Id  | Operation                   | Name         | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |              |     1 |   177 |     1   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| CUSTOMERS    |     1 |   177 |     1   (0)| 00:00:01 |
|*  2 |   INDEX UNIQUE SCAN         | CUSTOMERS_PK |     1 |       |     0   (0)| 00:00:01 |
--------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("CUSTOMER_ID"=232)

14 rows selected.

SQL>

因此,优化程序可将其识别为访问权限(“CUSTOMER_ID”= 232)