sql >> Databasteknik >  >> RDS >> Oracle

Oracle sql härledd tabell - valfri aliasing

Du behöver bara alias när du refererar till en kolumn som inte är unikt definierad. Det betyder att kolumnen finns i mer än en tabell/härledd tabell. En referens kan finnas i select-satsen eller en sammanfogning. Om alla kolumner är unika behöver du inget alias.

Jag föredrar att alias hela tiden för tydlighetens skull och för att det hjälper med Intellisense i PL/SQL.

--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;


  1. Hur man tar reda på antalet siffror i ett orakelnummer

  2. Sortering i SQL Server

  3. Att använda en utlösare i Oracle för att logga ändringar till en tabell

  4. MySQL helvete... kan inte ansluta till databasen /tmp/mysql.sock