sql - Ambiguity in Left joins (oracle only?) -
my boss found bug in query created, , don't understand reasoning behind bug, although query results prove he's correct. here's query (simplified version) before fix:
select ptno,ptnm,catcd parts left join categories on (categories.catcd=parts.catcd);
and here after fix:
select ptno,ptnm,parts.catcd parts left join categories on (categories.catcd=parts.catcd);
the bug was, null values being shown column catcd, i.e. query results included results table categories instead of parts. here's don't understand: if there ambiguity in original query, why didn't oracle throw error? far understood, in case of left joins, "main" table in query (parts) has precedence in ambiguity. wrong, or not thinking problem correctly?
update:
here's revised example, ambiguity error not thrown:
create table parts (ptno number, catcd number, seccd number); create table categories(catcd number); create table sections(seccd number, catcd number); select ptno,catcd parts left join categories on (categories.catcd=parts.catcd) left join sections on (sections.seccd=parts.seccd) ;
anybody have clue?
i'm afraid can't tell why you're not getting exception, can postulate why chose categories' version of column on parts' version.
as far understood, in case of left joins, "main" table in query (parts) has precedence in ambiguity
it's not clear whether "main" mean left table in left join, or "driving" table, see query conceptually... in either case, see "main" table in query you've written not "main" table in actual execution of query.
my guess oracle using column first table hits in executing query. , since individual operations in sql not require 1 table hit before other, dbms decide @ parse time efficient 1 scan first. try getting execution plan query. suspect may reveal it's hitting categories first , parts.
Comments
Post a Comment