gpt4 book ai didi

sql - Oracle 中带引号字段的 CREATE 语句

转载 作者:行者123 更新时间:2023-12-02 21:30:59 31 4
gpt4 key购买 nike

我使用以下 CREATE 语句在 Oracle 10g 中创建了一个表。

CREATE TABLE test ("id" NUMBER(35, 0) primary key, "description" VARCHAR2(250) not null);

基本表结构如下所示。

--------------------------------------------------------------------------------
Column Name Data Type Nullable Default Primary Key
--------------------------------------------------------------------------------
id NUMBER(35, 0) No - 1
description VARCHAR2(250) No - -

需要特别注意的是,这个 CREATE 语句中的列名称用双引号引起来只是为了好玩:)

发出此 DDL 语句后,我发出了三个 DML 语句来添加这么多行,如下所示。

INSERT INTO test VALUES (1, 'aaa');
INSERT INTO test VALUES (2, 'bbb');
INSERT INTO test VALUES (3, 'ccc');

最后,执行以下 SELECT 语句来验证是否插入了这些行。

SELECT * FROM test;

Oracle 确实显示了与执行此查询时插入的三行完全相同的行。


但是当我发出以下 SELECT 查询时,

SELECT id, description FROM test;

甲骨文提示,

ORA-00904: "DESCRIPTION": invalid identifier

还有以下(相同)查询,

SELECT id FROM test;

因错误而失败,

ORA-00904: "ID": invalid identifier

查询也是如此,

SELECT description FROM test;

唯一可以使用元字符 *SELECT 查询。在 SELECT 子句中列出字段不起作用。 SELECT 子句中的列名称大写也不起作用。

背后的原因是什么?

请不要只是说,不要这样做。我有兴趣知道其背后的原因。

最佳答案

好吧,我不说,我只是大声地想。

documentation明确指出,如果您引用了标识符,则必须在各处引用它们(我用斜体字表示强调):

Every database object has a name. In a SQL statement, you represent the name of an object with a quoted identifier or a nonquoted identifier.

  • 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.

  • A nonquoted identifier is not surrounded by any punctuation.

所以你总是必须做:

SELECT "id", "description" FROM test;

这是一种痛苦。但显然我也只是这么想,并没有真正说出来。

关于sql - Oracle 中带引号字段的 CREATE 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22286283/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com