Oracle SQL Syntax: Quoted identifier

☆樱花仙子☆ 提交于 2019-11-26 21:55:11

问题


I encountered SQL queries that looked like

select "hello"
from "foo"."bar"

I found that we can have quoted and unquoted identifiers in Oracle: Database Object Names and Qualifiers

... A quoted identifier begins and ends with double quotation marks ("). If you name a schema object using a quoted identifier, then you must use the double quotation marks whenever you refer to that object...

I asked the DBAs and they told me that there is a table with name bar but not "bar"

Why is that?


回答1:


The table is named bar and not BAR or "bar" but because it is in lowercase you can only reference it using double quotes:

select * from bar; -- will fail

select * from "bar"; -- will succeed

The moral is: never create tables like this!




回答2:


It helps to know that in the data_dictionary, object names are all stored capitalized; unless you use the quoted identifier to specifically tell oracle "hey, we wan't this objects name to be case sensitive" or in other words, "create this table with this literal name"

Below - create a table named bar, you can select using 'from bar', 'from BaR', 'from BAR'. 'from "bar"' wouldn't work because you are saying to oracle "give me the results from the table literally named lowercase "bar".

create a table named "bar", you can only select from it using 'from "bar"'. This is because 'from bar' translates to 'from BAR'.

SQL> create table bar (x varchar2(1));

Table created.

SQL> 
SQL> select * from bar;

no rows selected

SQL> select * from BAR;

no rows selected

SQL> select * from "bar";
select * from "bar"
              *
ERROR at line 1:
ORA-00942: table or view does not exist 


SQL> select * from "BAR";

no rows selected

SQL> 
SQL> drop table bar;

Table dropped.

SQL> 
SQL> create table "bar" (x varchar2(1));

Table created.

SQL> 
SQL> select * from bar;
select * from bar
              *
ERROR at line 1:
ORA-00942: table or view does not exist 


SQL> select * from BAR;
select * from BAR
              *
ERROR at line 1:
ORA-00942: table or view does not exist 


SQL> select * from "bar";

no rows selected

SQL> select * from "BAR";
select * from "BAR"
              *
ERROR at line 1:
ORA-00942: table or view does not exist 


SQL> 
SQL> drop table "bar";

Table dropped.

SQL> spool off



回答3:


By quoting something it forces a non-collated match on a database entity. So I think Tony's answer is nearly right:

select sysdate from dual; -- works

select sysdate from DUAL; -- works

select sysdate from "DUAL"; -- works

select sysdate from "dual"; - FAILS

And as you quoted, if the table was created using a quoted string, then you will probably have to use a quoted string to reference it.

I've not experimented with Oracle, but on other DBMSs, using quoted identifiers allows you to use reserved words as identifiers, e.g. in MySQL (which uses a backquote rather than rabbit ears) the following would be valid:

SELECT `SUM` FROM `WHERE`;

HTH




回答4:


On another, but similar note, the double quotes in Oracle indicate an identifier, not a string constant as the text you quoted indicates. This means that your example will give you an error even if "foo"."bar" exists. Consider this simple example:

SQL> SELECT "hello" FROM "DUAL";

SELECT "hello" FROM "DUAL"

ORA-00904: "hello": invalid identifier

versus this:

SQL> SELECT 'hello' FROM "DUAL";

'HELLO'
-------
hello

"DUAL" is an identifier: in this case it represents a table. It can represent a user, a column, etc., but it won't be interpreted as a string constant.



来源:https://stackoverflow.com/questions/6468337/oracle-sql-syntax-quoted-identifier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!