1

I have 2 tables that I don't understand why I need to join them using double quotes "". This is running in ORACLE 12c...

Can someone explain that to me?

CREATE TABLE "TB1"."PROPERTY" 
   (    "ItemID" CHAR(36 BYTE) NOT NULL ENABLE, 
    "Name" NVARCHAR2(255) NOT NULL ENABLE, 
    "Type" NVARCHAR2(50), 
    "Value" NCLOB, 
    "Size" NUMBER(*,0), 
     CONSTRAINT "COMPOUNDPK" PRIMARY KEY ("ItemID", "Name")



CREATE TABLE "TB2"."CATALOGITEM" 
   (    "ID" CHAR(36 BYTE) NOT NULL ENABLE, 
    "Type" NUMBER(*,0), 
    "Shortcut" CHAR(1 BYTE), 
    "Name" NVARCHAR2(255), 
     PRIMARY KEY ("ID")


SELECT *
FROM CATALOGITEM c, PROPERTY p
WHERE p."ItemID" = c."ID"

I get the following error:

ORA-00904: "P"."ITEMID": invalid identifier 00904. 00000 - "%s: invalid identifier" *Cause:
*Action: Error at Line: 8 Column: 8

Adam M
  • 109
  • 1
  • 7

1 Answers1

2

A common mistake people - whose origin isn't Oracle - do: use mixed case and enclose table & column names into double quotes. The fact that you can do that doesn't mean that you should do it. No benefit, many drawbacks.

Any time you reference such a table or a column, you must enclose its name into double quotes and type its name correctly, i.e. not mistype it. For example, "FirstName" would be different from "Firstname" or "FIRSTNAME".

Shortly - get rid of double quotes. Type those names anyway you want - you don't have to pay attention to it. Oracle will store those names in UPPERCASE into the data dictionary, but you'll be able to reference it anyway you want - without using double quotes (firstname, FirstName, FIRSTNAME - all the same).

As of the cause of an error - those tables belong to different users, so you'll have to acquire grant (at least SELECT) from one user to another in order to make it work.

(Not that it matters here, but - those CREATE TABLE statements are invalid, both miss the final close bracket).

A demonstration:

SQL> show user
USER is "SCOTT"
SQL> CREATE TABLE scott."PROPERTY"
  2     (    "ItemID" CHAR(36 BYTE) NOT NULL ENABLE,
  3      "Name" NVARCHAR2(255) NOT NULL ENABLE,
  4      "Type" NVARCHAR2(50),
  5      "Value" NCLOB,
  6      "Size" NUMBER(*,0),
  7       CONSTRAINT "COMPOUNDPK" PRIMARY KEY ("ItemID", "Name"));

Table created.

Connect as user MIKE and create another table:

SQL> connect mike/lion@xe
Connected.

Session altered.

SQL> CREATE TABLE mike."CATALOGITEM"
  2     (    "ID" CHAR(36 BYTE) NOT NULL ENABLE,
  3      "Type" NUMBER(*,0),
  4      "Shortcut" CHAR(1 BYTE),
  5      "Name" NVARCHAR2(255),
  6       PRIMARY KEY ("ID"));

Table created.

SQL> select *
  2  from catalogitem c, property p
  3  where p."ItemID" = c."ID";
from catalogitem c, property p
                    *
ERROR at line 2:
ORA-00942: table or view does not exist

As expected, that doesn't work. Now, back to SCOTT and grant some privileges to MIKE:

SQL> connect scott/tiger@xe
Connected.

Session altered.

SQL> grant select on property to mike;

Grant succeeded.

Back to MIKE:

SQL> connect mike/lion@xe
Connected.

Session altered.

SQL> select *
  2  from catalogitem c, scott.property p    --> note SCOTT here!
  3  where p."ItemID" = c."ID";

no rows selected

SQL>
Littlefoot
  • 131,892
  • 15
  • 35
  • 57
  • OK, so I think I understand. The the tables were created with double quotes "" and hence the need to reference them in that manner. This is a 3rd party app and I don't have control over the design itself. I'm a little confused on your provisioning that you're doing?? giving a user different rights will allow references to the tables without double quotes? – Adam M Mar 30 '18 at 18:58
  • You created two tables. One of them belongs to user TB1, another one to user TB2. If you want to make a query run, one of those users has to grant (at least) SELECT privilege to another. That "another" user then has to precede that table name with its owner name. Otherwise, that won't work (apart from granting privileges to PUBLIC, but that's most probably not a good idea). Grant has nothing to do with double quotes. – Littlefoot Mar 30 '18 at 19:12