Question

I have created a table in Oracle 10g using the following CREATE statement.

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

The basic table structure looks like as follows.

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

It should precisely be noted that the column names in this CREATE statement are enclosed within double quotes just for having a fun :)

After issuing this DDL statement, I issued three DML statements to add this many rows as follows.

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

And finally, the following SELECT statement was executed to verify, if those rows were inserted.

SELECT * FROM test;

Oracle indeed displays three rows exactly as inserted on executing this query.


But when I issue the following SELECT query,

SELECT id, description FROM test;

Oracle complains,

ORA-00904: "DESCRIPTION": invalid identifier

The following (same) query also,

SELECT id FROM test;

fails with the error,

ORA-00904: "ID": invalid identifier

The same is true for the query,

SELECT description FROM test;

The only SELECT query with the meta character * works. Listing fields in the SELECT clause doesn't work. Capitalizing the column names in the SELECT clause also doesn't work.

What is the reason behind it?

Please don't just say, Don't do this. I'm interested in knowing the reason behind it.

Was it helpful?

Solution

OK, I won't say it, I'll just think it loudly.

The documentation clearly says that if you have quoted identifiers, you have to quote them everywhere (my italics for emphasis):

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.

So you always have to do:

SELECT "id", "description" FROM test;

Which is a pain. But obviously I'm just thinking that too, not really saying it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top