Re: Foreign key joins revisited

2022-01-03 Thread Joel Jacobson
On Wed, Dec 29, 2021, at 10:46, Peter Eisentraut wrote: >In the 1990s, there were some SQL drafts that included syntax like Do you remember if it was in the beginning/middle/end of the 1990s? I will start the work of going through all drafts tomorrow. /Joel

Re: Foreign key joins revisited

2022-01-01 Thread Joel Jacobson
On Wed, Dec 29, 2021, at 16:28, Tom Lane wrote: >Peter Eisentraut writes: >> In the 1990s, there were some SQL drafts that included syntax like >> JOIN ... USING PRIMARY KEY | USING FOREIGN KEY | USING CONSTRAINT ... >> AFAICT, these ideas just faded away because of other priorities, so if >>

Re: Foreign key joins revisited

2021-12-30 Thread Joel Jacobson
On Wed, Dec 29, 2021, at 16:28, Tom Lane wrote: >Peter Eisentraut writes: >> In the 1990s, there were some SQL drafts that included syntax like >> JOIN ... USING PRIMARY KEY | USING FOREIGN KEY | USING CONSTRAINT ... >> AFAICT, these ideas just faded away because of other priorities, so if >>

Re: Foreign key joins revisited

2021-12-29 Thread Tom Lane
Peter Eisentraut writes: > In the 1990s, there were some SQL drafts that included syntax like > JOIN ... USING PRIMARY KEY | USING FOREIGN KEY | USING CONSTRAINT ... > AFAICT, these ideas just faded away because of other priorities, so if > someone wants to revive it, some work already exists.

Re: Foreign key joins revisited

2021-12-29 Thread Andrew Dunstan
On 12/28/21 15:10, Tom Lane wrote: > Vik Fearing writes: >> On 12/28/21 8:26 PM, Joel Jacobson wrote: >>> Can with think of some other suitable reserved keyword? >> I don't particularly like this whole idea anyway, but if we're going to >> have it, I would suggest >> JOIN ... USING KEY ...

Re: Foreign key joins revisited

2021-12-29 Thread Peter Eisentraut
On 28.12.21 20:45, Vik Fearing wrote: I don't particularly like this whole idea anyway, but if we're going to have it, I would suggest JOIN ... USING KEY ... since USING currently requires a parenthesized list, that shouldn't create any ambiguity. In the 1990s, there were some SQL

Re: Foreign key joins revisited

2021-12-28 Thread Tom Lane
Vik Fearing writes: > On 12/28/21 8:26 PM, Joel Jacobson wrote: >> Can with think of some other suitable reserved keyword? > I don't particularly like this whole idea anyway, but if we're going to > have it, I would suggest > JOIN ... USING KEY ... That would read well, which is nice, but

Re: Foreign key joins revisited

2021-12-28 Thread Alvaro Herrera
On 2021-Dec-27, Joel Jacobson wrote: > >On Mon, Dec 27, 2021, at 15:48, Isaac Morland wrote: > >I thought the proposal was to give the FK constraint name. > >However, if the idea now is to allow leaving that out also if there > >is only one FK, then that's also OK as long as people understand it

Re: Foreign key joins revisited

2021-12-28 Thread Adam Brusselback
> How about JOIN WITH? I'm -1 on this, reusing WITH is just likely to cause confusion because WITH can appear other places in a query having an entirely different meaning. I'd just avoid that from the start. >> Can with think of some other suitable reserved keyword? >FOREIGN? Or even spell out

Re: Foreign key joins revisited

2021-12-28 Thread Vik Fearing
On 12/28/21 8:26 PM, Joel Jacobson wrote: > On Mon, Dec 27, 2021, at 19:15, Tom Lane wrote: >> NATURAL JOIN is widely regarded as a foot-gun that the SQL committee >> should never have invented. Why would we want to create another one? >> >> (I suspect that making the constraint name optional

Re: Foreign key joins revisited

2021-12-28 Thread Tom Lane
"Joel Jacobson" writes: > Since my last email in this thread, I've learned KEY is unfortunately not a > reserved keyword. > This probably means the proposed "JOIN KEY" would be problematic, since a > relation could be named KEY. > Can with think of some other suitable reserved keyword?

Re: Foreign key joins revisited

2021-12-28 Thread Joel Jacobson
On Mon, Dec 27, 2021, at 19:15, Tom Lane wrote: > NATURAL JOIN is widely regarded as a foot-gun that the SQL committee > should never have invented. Why would we want to create another one? > > (I suspect that making the constraint name optional would be problematic > for reasons of syntax

Re: Foreign key joins revisited

2021-12-27 Thread Tom Lane
Isaac Morland writes: > On Mon, 27 Dec 2021 at 03:22, Joel Jacobson wrote: >> However, I see one problem with leaving out the key columns: >> First, there is only one FK in permission pointing to role, and we write a >> query leaving out the key columns. >> Then, another different FK in

Re: Foreign key joins revisited

2021-12-27 Thread Corey Huinker
> > > First, there is only one FK in permission pointing to role, and we write a > query leaving out the key columns. > Then, another different FK in permission pointing to role is later added, > and our old query is suddenly in trouble. > > We already have that problem with cases where two tables

Re: Foreign key joins revisited

2021-12-27 Thread Joel Jacobson
On Mon, Dec 27, 2021, at 17:03, Isaac Morland wrote: > On Mon, 27 Dec 2021 at 10:20, Joel Jacobson wrote: > > Foreign key constraint names have been given the same names as the referenced > tables. > > While I agree this could be a simple approach in many real cases for having > easy to

Re: Foreign key joins revisited

2021-12-27 Thread Isaac Morland
On Mon, 27 Dec 2021 at 10:20, Joel Jacobson wrote: > Foreign key constraint names have been given the same names as the > referenced tables. > While I agree this could be a simple approach in many real cases for having easy to understand FK constraint names, I wonder if for illustration and

Re: Foreign key joins revisited

2021-12-27 Thread Sascha Kuhl
Joel Jacobson schrieb am Mo., 27. Dez. 2021, 16:21: > >On Mon, Dec 27, 2021, at 15:48, Isaac Morland wrote: > >I thought the proposal was to give the FK constraint name. > >However, if the idea now is to allow leaving that out also if there > >is only one FK, then that's also OK as long as

Re: Foreign key joins revisited

2021-12-27 Thread Joel Jacobson
>On Mon, Dec 27, 2021, at 15:48, Isaac Morland wrote: >I thought the proposal was to give the FK constraint name. >However, if the idea now is to allow leaving that out also if there >is only one FK, then that's also OK as long as people understand it can break >in the same way NATURAL JOIN can

Re: Foreign key joins revisited

2021-12-27 Thread Isaac Morland
On Mon, 27 Dec 2021 at 03:22, Joel Jacobson wrote: > However, I see one problem with leaving out the key columns: > First, there is only one FK in permission pointing to role, and we write a > query leaving out the key columns. > Then, another different FK in permission pointing to role is

Re: Foreign key joins revisited

2021-12-27 Thread Joel Jacobson
On Sun, Dec 26, 2021, at 23:25, Corey Huinker wrote: > My second guess would be: > FROM permission p > LEFT JOIN role AS r ON [FOREIGN] KEY [(p.col1 [, p.col2 ...])] > > where the key spec is only required when there are multiple foreign keys in > permission pointing to role. > > But my first

Re: Foreign key joins revisited

2021-12-26 Thread Corey Huinker
> > > > Perhaps this would be more SQL idiomatic: > > FROM permission p >LEFT JOIN ON KEY role IN p AS r >LEFT JOIN team_role AS tr ON KEY role TO r >LEFT JOIN ON KEY team IN tr AS t >LEFT JOIN user_role AS ur ON KEY role TO r >LEFT JOIN ON KEY user IN ur AS u > > My second

Re: Foreign key joins revisited

2021-12-26 Thread Joel Jacobson
On Sun, Dec 26, 2021, at 22:38, Joel Jacobson wrote: > FROM permission p > LEFT JOIN ON KEY p.role r > LEFT JOIN team_role tr ON KEY role TO r > LEFT JOIN ON KEY tr.team t > LEFT JOIN user_role ur ON KEY role TO r > LEFT JOIN ON KEY ur.user u Hm, might be problematic to reuse dot

Re: Foreign key joins revisited

2021-12-26 Thread Sascha Kuhl
When you join by id, the join is unique. You can have combinations of fields, with multiple fields. Is it a maximum fields question. Isaac Morland schrieb am So., 26. Dez. 2021, 22:37: > On Sun, 26 Dec 2021 at 16:24, Joel Jacobson wrote: > > >> I think if we combine the ON KEY ... TO ... part

Re: Foreign key joins revisited

2021-12-26 Thread Joel Jacobson
On Sun, Dec 26, 2021, at 22:24, Joel Jacobson wrote: > FROM permission p >LEFT JOIN ON KEY p.permission_role_id_fkey r >LEFT JOIN team_role tr ON KEY team_role_role_id_fkey TO r >LEFT JOIN ON KEY tr.team_role_team_id_fkey t >LEFT JOIN user_role ur ON KEY user_role_role_id_fkey TO r

Re: Foreign key joins revisited

2021-12-26 Thread Isaac Morland
On Sun, 26 Dec 2021 at 16:24, Joel Jacobson wrote: > I think if we combine the ON KEY ... TO ... part of my idea, with your > idea, we have a complete neat solution. > > Maybe we can make them a little more similar syntax wise though. > > Could you accept "ON KEY" instead of "FOREIGN KEY" for

Re: Foreign key joins revisited

2021-12-26 Thread Joel Jacobson
On Sun, Dec 26, 2021, at 21:49, Isaac Morland wrote: > Right, sorry, that was sloppy of me. I should have noticed that I wrote "tr-> > ... AS tr". But in the case where the "source" > (referencing) table is already in the join, what's wrong with allowing my > suggestion? We do need another way

Re: Foreign key joins revisited

2021-12-26 Thread Joel Jacobson
On Sun, Dec 26, 2021, at 19:52, Joel Jacobson wrote: >LEFT JOIN role r ON KEY p.permission_role_id_fkey Ops! I see this doesn't quite work. We're missing one single bit of information. That is, we need to indicate if the foreign key is a) in the table we're currently joining or b) to some

Re: Foreign key joins revisited

2021-12-26 Thread Isaac Morland
On Sun, 26 Dec 2021 at 14:37, Joel Jacobson wrote: > Let's look at each row your example and see if we can work it out. > I've added the "FROM permission p" and also "AS [table alias]", > otherwise the aliases you use won't exist. > > > FROM permission p > > This row is obviously OK. We now

Re: Foreign key joins revisited

2021-12-26 Thread Joel Jacobson
On Sun, Dec 26, 2021, at 20:02, Isaac Morland wrote: > Is it going too far to omit the table name? I mean, any given foreign key can > only point to one other table: That actually how I envisioned this feature to work way back, but it doesn't work, and I'll try to explain why. As demonstrated,

Re: Foreign key joins revisited

2021-12-26 Thread Isaac Morland
On Sun, 26 Dec 2021 at 01:47, Joel Jacobson wrote: > On Sat, Dec 25, 2021, at 21:55, Joel Jacobson wrote: > > FROM permission p > > LEFT JOIN role r WITH p->permission_role_id_fkey = r > > LEFT JOIN team_role tr WITH tr->team_role_role_id_fkey = r > > LEFT JOIN team t WITH

Re: Foreign key joins revisited

2021-12-26 Thread Joel Jacobson
On Sun, Dec 26, 2021, at 19:54, Sascha Kuhl wrote: > Could you make > > JOIN key ? Not sure what you mean. Perhaps you can explain by rewriting the normal query below according to your idea? SELECT * FROM permission p LEFT JOIN role r ON p.role_id = r.id Given the foreign key on "permission"

Re: Foreign key joins revisited

2021-12-26 Thread Sascha Kuhl
Could you make JOIN key ? Joel Jacobson schrieb am So., 26. Dez. 2021, 19:52: > On Sun, Dec 26, 2021, at 19:33, Sascha Kuhl wrote: > > The Syntax is great. Which language does it come from. I consider it not > german. But I understand it mathematically. > > Great extension. > > It doesn't come

Re: Foreign key joins revisited

2021-12-26 Thread Joel Jacobson
On Sun, Dec 26, 2021, at 19:33, Sascha Kuhl wrote: > The Syntax is great. Which language does it come from. I consider it not > german. But I understand it mathematically. > Great extension. It doesn't come from any language. But I've seen similar features in ORMs, such as the jOOQ Java

Re: Foreign key joins revisited

2021-12-25 Thread Joel Jacobson
On Sat, Dec 25, 2021, at 21:55, Joel Jacobson wrote: > FROM permission p > LEFT JOIN role r WITH p->permission_role_id_fkey = r > LEFT JOIN team_role tr WITH tr->team_role_role_id_fkey = r > LEFT JOIN team t WITH tr->team_role_team_id_fkey = t > LEFT JOIN user_role ur WITH

Re: Foreign key joins revisited

2021-12-25 Thread Joel Jacobson
On Sat, Dec 25, 2021, at 22:06, David G. Johnston wrote: > On Saturday, December 25, 2021, Joel Jacobson wrote: > * Would require changes to the SQL standard, i.e. SQL committee work > > Huh? I mean, one could argue this is perhaps even the wrong forum to discuss this idea, since it's a

Re: Foreign key joins revisited

2021-12-25 Thread David G. Johnston
On Saturday, December 25, 2021, Joel Jacobson wrote: > > I've revisited the idea to somehow use foreign keys to do joins, > > -1 > This is somewhat addressed by the USING join form, but USING has other > drawbacks, why I tend to avoid it except for one-off queries. > I find this sufficient.

Foreign key joins revisited

2021-12-25 Thread Joel Jacobson
Hi, I've revisited the idea to somehow use foreign keys to do joins, in the special but common case when joining on columns that exactly match a foreign key. The idea is to add a new ternary operator, which would be allowed only in the FROM clause. It would take three operands: 1)