Is it possible to write/make a schema for a PostgreSQL db and then query let’s say
SELECT email, username, city_population
FROM users, cities
WHERE email = 'user@example.com'
And the connection between the users table and cities table is defined somewhere else, and PostgreSQL uses that informtion to join the tables. Is there something like that?
Edit: and without views
No, the
NATURAL JOINdescribed by Daniel is as close as SQL comes. There is a reason. While people often want to join based on equality of columns in the same domain, they sometimes want something more complex. I have seen a lot of queries which ask for things like “Who lives in a city within 300 miles of this city?”, “What are the 3 closest cities to this user, excluding the one they live in?”, or “What other users live in the same state as this user?” Having automatic joins based on some assumed join criteria would make it harder to write unambiguous queries to answer questions like that.That said, it is not uncommon to want what you’re looking for, so as Craig said, there are many tools which use foreign key definitions to provide default linkages.
Based on the question, I suspect that you might possibly be interested in HTSQL, which provides automated linking and other convenience features to assist in bridging the gap between a relational database and other formats which people might already know. Its development is funded in part by foundation grants, including the National Science Foundation. (I have no relationship to the project other than having read documentation, and having had a brief conversation with one of the authors at a conference.)