SQL 表名周围的引号究竟有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/563090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 01:10:07  来源:igfitidea点击:

What exactly do quotation marks around the table name do?

sqloracle

提问by George Mauer

I thought that the quotation mark (") was simply a type of grouping marker but I'm debugging some NHibernate code and notice that while

我认为引号 (") 只是一种分组标记,但我正在调试一些 NHibernate 代码并注意到

SELECT * FROM site WHERE site_id = 3;

Works fine

工作正常

SELECT * FROM "site" WHERE site_id = 3;

fails with a table or view does not exist error.

因表或视图不存在错误而失败。

What gives?

是什么赋予了?

回答by Justin Cave

Putting double-quotes around an identifier in Oracle causes Oracle to treat the identifier as case sensitive rather than using the default of case-insensitivity. If you create a table (or a column) with double-quotes around the name, you must always refer to the identifier with double quotes and by correctly specifying the case (with the exception of all upper case identifiers, where double-quotes are meaningless).

在 Oracle 中将标识符放在双引号周围会导致 Oracle 将标识符视为区分大小写,而不是使用不区分大小写的默认值。如果您创建的表(或列)在名称周围加上双引号,则必须始终使用双引号引用标识符并正确指定大小写(所有大写标识符除外,其中双引号没有意义) )。

Under the covers, Oracle is always doing case-sensitive identifier matching. But it always casts identifiers that are not double-quoted to upper case before doing the matching. If you put double-quotes around an identifier, Oracle skips the casting to upper case.

在幕后,Oracle 总是进行区分大小写的标识符匹配。但它总是在进行匹配之前将没有双引号的标识符转换为大写。如果在标识符周围加上双引号,Oracle 将跳过转换为大写。

So if you do something like

所以如果你做类似的事情

CREATE TABLE my_table( 
  col1 number,
  col2 number
)

you can

你可以

SELECT * FROM my_table
SELECT * FROM MY_TABLE
SELECT * FROM My_Table
SELECT * FROM "MY_TABLE"

but something like

但像

SELECT * FROM "my_table" 

will fail.

将失败。

On the other hand, if you do something like

另一方面,如果你做类似的事情

CREATE TABLE "my_other_table"( 
  col1 number,
  col2 number
)

you cannot do

你不能做

SELECT * FROM my_other_table
SELECT * FROM MY_OTHER_TABLE
SELECT * FROM My_Other_Table
SELECT * FROM "MY_OTHER_TABLE"

but this

但是这个

SELECT * FROM "my_other_table" 

will work

将工作

回答by Erich Kitzmueller

It should be added that identifiers in quotation marks may contain special characters, e.g. "a-b c.d" is a valid identifier.

需要补充的是,引号中的标识符可以包含特殊字符,例如“ab cd”是有效标识符。