Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove duplicate columns when selecting * in natural joins [CORE2096] #2530

Closed
firebird-automations opened this issue Sep 30, 2008 · 7 comments

Comments

@firebird-automations
Copy link
Collaborator

Submitted by: Philip Williams (unordained)

Is related to CORE1235

Example:
create table a
(
id integer not null primary key,
x varchar(250)
);

create table b
(
a_id integer not null,
constraint b_fk_a foreign key (a_id) references a (id),
blah timestamp
);

create view c as
select http://a.id id, count(*) b_times from a left join b on b.a_id = http://a.id group by http://a.id;

create view d as
select a.*, c.*
from a natural join c;

That last select statement, by itself, works just fine; but it will have two columns named 'id', of same datatype and value, and the attempt to create the view will fail:

unsuccessful metadata update
STORE RDB$RELATION_FIELDS failed
attempt to store duplicate value (visible to active transactions) in unique index "RDB$INDEX_15"

The Joneses:
I'm not absolutely sure, but I vaguely remember that Oracle automatically drops these duplicate columns when using either USING or NATURAL JOIN syntaxes. Regardless, it would be beneficial for generating unambiguous select * results, particularly for view creation. (I want to later be able to add columns to "a" or "c", and regenerate my view from a script, without having to name every column I added.)

Considerations:
I don't know if "picking" a column to keep would affect automation down-stream; the view resulting from the join probably wouldn't be updatable by default anyway, but there are some (unused?) mechanisms in JDBC for identifying the source table of a given column, and I could see that getting confusing. Also, if NATURAL JOIN and USING support joins across columns of similar but different types (different varchar lengths?), I could see that being weird too.

@firebird-automations
Copy link
Collaborator Author

Commented by: Philip Williams (unordained)

Related to CORE1235

@firebird-automations
Copy link
Collaborator Author

Modified by: @dyemanov

Link: This issue is related to CORE1235 [ CORE1235 ]

@firebird-automations
Copy link
Collaborator Author

Modified by: @dyemanov

assignee: Adriano dos Santos Fernandes [ asfernandes ]

@firebird-automations
Copy link
Collaborator Author

Commented by: @asfernandes

First, you're asking for columns of both table, so both columns should be returned. NATURAL and USING joins could also be outer joins, so this is valuable information to have. Also, if you ask for "select t.*, t.* from t" the columns will be returned two times.

What you want is:
select * from t1 natural join t2

It will not duplicate the columns used for the join. For outer joins, a coalesce is automatically built (coalesce(t1.x, t2.x)). AFAIK, this is what the standard requires and it should be documented in our release notes.

@firebird-automations
Copy link
Collaborator Author

Commented by: Philip Williams (unordained)

Ah! Quite right, outer joins wouldn't work with that. And I hadn't thought about select * vs. select a.* being able to expand differently. Thanks! (Ticket cancellable.)

@firebird-automations
Copy link
Collaborator Author

Modified by: @asfernandes

status: Open [ 1 ] => Resolved [ 5 ]

resolution: Won't Fix [ 2 ]

@firebird-automations
Copy link
Collaborator Author

Modified by: @pcisar

status: Resolved [ 5 ] => Closed [ 6 ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants