Question

SQL> -- CASE 1

SQL>select nest_test.id.num from nest_test;
select nest_test.id.num from nest_test
       *
ERROR at line 1:
ORA-00904: "NEST_TEST"."ID"."NUM": invalid identifier

SQL> -- CASE 2

SQL>select n.id.num from nest_test n;

    ID.NUM
----------
        12

As, AFAIK, aliasing any table is just giving simple name to the table or column. Then, why I'm getting error in Case 1, when I'm trying to retrieve user defined object from table ? What actually happened, when I aliased my table.

Was it helpful?

Solution

The Oracle documentation states that table aliases are required to reference methods or attributes of objects to avoid the potential for "inner capture" (a table column name clashing with an object attribute name). Find out more.

The documentation states:

"You must qualify a reference to an object attribute or method with a table alias rather than a table name even if the table name is itself qualified by a schema name."

It doesn't say why Oracle enforce this rule. But there is obviously some complexity in the namespace implementation, caused by retrofitting objects into the relational kernel.


"then, why this query works select emp.sal from emp"

Because that's a regular column on a table. Oracle have never enforced aliases on normal tables. Starting now would break an enormous amount of code which has accrued over the decades.

Having to use aliases is just one more penalty for using object types to define table columns. It's far from being teh most onerous aspect of the clunky syntax which goes with Oracle's ORDBMS implementation.

Besides, there's almost cases in which using objects rather than realtional tables to persist data is the rigyht solution, so it doesn't really matter.

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