Live data from Hacker News

SQL language proposal: JOIN FOREIGN

gist.github.com

11–20 of 206 posts

Re: SQL language proposal: JOIN FOREIGN

#11
It looked very useful and intuitive at first, but on further thought, I think the only time that you'd truly benefit from it if you SELECT *. For other SELECT queries with explicit field name list, you'd need to know the table the key constraint links to anyway.

SQL Views serve perfectly well for queries encouraged by the schema itself, and i think they are more sophisticated and practical way.

Re: SQL language proposal: JOIN FOREIGN

#12

This is my "Thanks, I hate it" response. Reason being if you use the example they gave: SELECT f.title, f.did, d.name, f.date_prod, f.kind FROM films f JOIN FOREIGN f.films_did_fkey d You need to implicitly know the table that films_did_fkey points to, because 'd' is just a table alias. I can't think of anywhere else in the SQL standard where you can introduce a table alias without explicitly referencing the table. I…

yeah, this saves like 10 characters of text in exchange for massive confusion.

In this simple example yes, but there can be multiple columns in a join.

The syntax proposal i O(1) syntax wise compared to O(2n) for a JOIN ON where n is the number of columns.

Re: SQL language proposal: JOIN FOREIGN

#13
post #4

I dig it! I prefer defining tables like this: CREATE TABLE category ( id int GENERATED ALWAYS AS IDENTITY, name text ); CREATE TABLE post ( id int GENERATED ALWAYS AS IDENTITY, category_id int REFERENCES category (id) ON DELETE CASCADE ); That is, category.id rather than category.category_id. But the USING clause doesn't work with that style, as far as I understand. This would make my queries nicer.

I've been quite happy having fully prefixed column names for a long time now. Makes joins easier, big views clearer, and random exports more readable out of the box. Also in my case it's also easier to line up unique column names with things like Clojure specs but I accept that's a niche concern.

Re: SQL language proposal: JOIN FOREIGN

#14

What I really need is something like: SELECT -col1, -col14 FROM table LIMIT 50; Where the minus sign means I don't want these two columns. I still don't see a way to do it easily (for Vertica and in Datagrip).

Some have support for this.

- BQ https://cloud.google.com/bigquery/docs/reference/standard-sq...

- CH https://clickhouse.com/docs/en/sql-reference/statements/sele...

Re: SQL language proposal: JOIN FOREIGN

#15

This is my "Thanks, I hate it" response. Reason being if you use the example they gave: SELECT f.title, f.did, d.name, f.date_prod, f.kind FROM films f JOIN FOREIGN f.films_did_fkey d You need to implicitly know the table that films_did_fkey points to, because 'd' is just a table alias. I can't think of anywhere else in the SQL standard where you can introduce a table alias without explicitly referencing the table. I…

Agreed. Maybe something like this instead:

    SELECT f.title, f.did, d.name, f.date_prod, f.kind
      FROM films f
      JOIN distributors d ON FOREIGN f.films_did_fkey
and

    SELECT f.title, f.did, d.name, f.date_prod, f.kind
      FROM distributors d
      JOIN films f ON FOREIGN films_did_fkey

Re: SQL language proposal: JOIN FOREIGN

#16
Of all the myriad indignities of SQL, this isn't near the top of my list. I also don't like making the names of objects like foreign keys and indexes first class concerns in your queries, that's a whole new layer of cognitive overhead.

Re: SQL language proposal: JOIN FOREIGN

#17
Please, no. A common problem in data warehousing is that a design with lots of foreign keys becomes slow to load. A standard solution is to move the checks for referential integrity elsewhere, then drop the foreign key constraint. This massively improves load performance.

This syntax change means that this solution can't be used because you have no idea what random queries out there might rely on the specific existence of a foreign key constraint for the definition of the query. Thereby meaning that if a foreign key constraint becomes a performance problem, we're stuck with it rather than having a solution.

Features have consequences. And I don't like the consequences of making business rules that are now explicit in the query, be instead implicit in the table design.

Re: SQL language proposal: JOIN FOREIGN

#18
post #15

This is my "Thanks, I hate it" response. Reason being if you use the example they gave: SELECT f.title, f.did, d.name, f.date_prod, f.kind FROM films f JOIN FOREIGN f.films_did_fkey d You need to implicitly know the table that films_did_fkey points to, because 'd' is just a table alias. I can't think of anywhere else in the SQL standard where you can introduce a table alias without explicitly referencing the table. I…

Agreed. Maybe something like this instead: SELECT f.title, f.did, d.name, f.date_prod, f.kind FROM films f JOIN distributors d ON FOREIGN f.films_did_fkey and SELECT f.title, f.did, d.name, f.date_prod, f.kind FROM distributors d JOIN films f ON FOREIGN films_did_fkey

+1 ... needed at least as an option where FK is not declared

Re: SQL language proposal: JOIN FOREIGN

#19

This is my "Thanks, I hate it" response. Reason being if you use the example they gave: SELECT f.title, f.did, d.name, f.date_prod, f.kind FROM films f JOIN FOREIGN f.films_did_fkey d You need to implicitly know the table that films_did_fkey points to, because 'd' is just a table alias. I can't think of anywhere else in the SQL standard where you can introduce a table alias without explicitly referencing the table. I…

I think your concern is addressed by the idea further down in proposal; giving the foreign keys the same names as the referenced tables. This example was provided: SELECT f.title, f.did, d.name, f.date_prod, f.kind FROM films f JOIN FOREIGN f.distributors d

No, my concern is not that you can also specify the table name that explicitly states the name of both tables, it's that you have proposed a syntax where it is possible to leave off the explicit table name.

If you got rid of the syntax that uses the underlying "magic" of needing to know which table the foreign key points to, I'd be more amenable.

Re: SQL language proposal: JOIN FOREIGN

#20

Earlier quoted context omitted.

yeah, this saves like 10 characters of text in exchange for massive confusion.

In this simple example yes, but there can be multiple columns in a join. The syntax proposal i O(1) syntax wise compared to O(2n) for a JOIN ON where n is the number of columns.

But that "simple example" is like 95% plus of all my joins, where I'm joining two tables on a foreign key.
Post reply on HN