Oracle
 sql >> Datenbank >  >> RDS >> Oracle

Von Oracle SQL abgeleitete Tabelle - optionales Aliasing

Sie müssen nur dann einen Alias ​​verwenden, wenn Sie auf eine Spalte verweisen, die nicht eindeutig definiert ist. Das bedeutet, dass die Spalte in mehr als einer Tabelle/abgeleiteten Tabelle vorhanden ist. Eine Referenz könnte sich in der Select-Anweisung oder in einem Join befinden. Wenn alle Spalten eindeutig sind, benötigen Sie keinen Alias.

Ich ziehe es aus Gründen der Übersichtlichkeit immer vor, Alias ​​zu verwenden, und weil es bei Intellisense in PL/SQL hilft.

--ALIAS needed, because the 'a' column referenced is not unique
--this will throw an error
select a, a, b, c
  from (select 'A1' as a, 'B1' as b, 'C1' as c from dual),
       (select 'A2' as a from dual);
--this will not throw an error
select t1.a, t2.a, b,c
  from (select 'A1' as a, 'B1' as b, 'C1' as c from dual) t1,
       (select 'A2' as a from dual) t2;
;

--ALIAS not needed for join, because all referenced columns are unique
select a, b, c, d, e, f
  from (select 'A' as a, 'B' as b, 'C' as c from dual)
  join (select 'D' as d, 'E' as e, 'F' as f from dual)
    on a = d;

--ALIAS needed for join, because the 'x' column referenced is not unique
--this will throw an error
select a
  from (select 'A' as a, 'B' as b, 'C' as c, 'X' as x from dual)
  join (select 'D' as d, 'E' as e, 'F' as f, 'X' as x from dual)
    on x = x;
--this will not throw an error
select a
  from (select 'A' as a, 'B' as b, 'C' as c, 'X' as x from dual) t1
  join (select 'D' as d, 'E' as e, 'F' as f, 'X' as x from dual) t2
    on t1.x = t2.x;