SQL 带空格的 Oracle 表列名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13798035/
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 12:32:50  来源:igfitidea点击:

Oracle table column name with space

sqloracle

提问by laksys

Is it possible to create a table with column name containing space? If so how can I create and use it?

是否可以创建列名包含空格的表?如果是这样,我该如何创建和使用它?

回答by Alex Poole

It is possible, but it is not advisable. You need to enclose the column name in double quotes.

这是可能的,但不建议这样做。您需要用双引号将列名括起来。

create table my_table ("MY COLUMN" number);

But note the warning in the documentation:

但请注意文档中的警告:

Note: Oracle does not recommend using quoted identifiers for database object names. These quoted identifiers are accepted by SQL*Plus, but they may not be valid when using other tools that manage database objects.

注意:Oracle 不建议对数据库对象名称使用带引号的标识符。SQL*Plus 接受这些带引号的标识符,但在使用其他管理数据库对象的工具时,它们可能无效。

The name will be case-sensitive, and you wil have to enclose the name in double quotes every time you reference it:

该名称区分大小写,每次引用时都必须用双引号将名称括起来:

select "MY COLUMN" from my_table;

So... don't, would be my advice...

所以......不要,这是我的建议......

回答by Aleksander Blomsk?ld

Yes, it's possible. You just have to be sure to quote the column name. For instance:

是的,这是可能的。您只需要确保引用列名。例如:

CREATE TABLE Fubar ("Foo Bar" INT);
INSERT INTO Fubar VALUES (1337);
SELECT "Foo Bar" FROM SpaceMonster

Even though it's possible, it doesn't make it a good idea. You'll probably save yourself from a lot of pain if just replace all you spaces with underscores.

尽管这是可能的,但这并不是一个好主意。如果只用下划线替换所有空格,您可能会免于很多痛苦。

回答by Brian Agnew

You can (see the Oracle doc) if you quote these appropriately. However I suspect this is not a good idea, since you'll have to quote everything. Generally db naming standards / conventions (e.g. hereor here) favour using underscores (which don't require quoting) over whitespace.

如果您适当地引用这些内容,您可以(请参阅Oracle 文档)。但是我怀疑这不是一个好主意,因为您必须引用所有内容。通常,数据库命名标准/约定(例如此处此处)倾向于使用下划线(不需要引用)而不是空格。

回答by mcha

it is possible by naming the column between two "example: "My columN" , the column name becomes case sensitive which means.

可以通过在两个"示例之间命名列: "My columN" ,列名称区分大小写,这意味着。

SELECT "my column" from table; --NOT OK
SELECT "My columN" from table; --OK

回答by Ajith Sasidharan

This is the columns rule defined for oracle

这是为 oracle 定义的列规则

Columns (for tables)

列(用于表)

  1. All columns are in form {alias}_{colname}. For example prs_id, prs_name, prs_adr_id, adr_street_name. This guarantees that column names are unique in a schema, except denormalized columns from another table, which are populated using triggers or application logic.
    1. All columns are in singular. If you think you need a column name in plural think twice whether it is the right design? Usually it means you are including multiple values in the same column and that should be avoided.
    2. All tables have surrogate primary key column in form {alias}_id, which is the first column in the table. For example, prs_id, mat_id, adr_id.
  1. 所有列都采用 {alias}_{colname} 形式。例如 prs_id、prs_name、prs_adr_id、adr_street_name。这保证了列名在模式中是唯一的,除了来自另一个表的非规范化列,这些列是使用触发器或应用程序逻辑填充的。
    1. 所有列都是单数。如果你认为你需要一个复数的列名,请三思它是否是正确的设计?通常这意味着您在同一列中包含多个值,应该避免这种情况。
    2. 所有表都有 {alias}_id 形式的代理主键列,它是表中的第一列。例如,prs_id、mat_id、adr_id。

you can always have alias for column name bu using ""

您始终可以使用“”为列名 bu 设置别名

回答by Mike Woodhouse

Did you Google for this? I did - the 6th link was this, in which I find the following:

你谷歌了吗?我做了 - 第 6 个链接是这个,我在其中找到了以下内容:

create table employee (
   "First Name" varchar2(20),
   "Last Name" varchar2(20),
   Address varchar2(60),
   Phone varchar2(15),
   Salary number,
   EmpID number,
   DeptID number
); 

... which works fine when I tried it in 10g.

...当我在 10g 中尝试时效果很好。