Tables (base and query results) represent application relationships.
From (an earlier version of) this answer:
For every base table, the DBA gives a predicate--a natural language fill-in-the-(named-)blanks statement template parameterized by column names.
-- chef with id NUMCUISINIER has name NOMCUIS and ...
CUISINIER(NumCuisinier, NomCuis, ...)
A base table holds the rows that, using its columns' values to fill in the (named) blanks, make a true statement aka proposition.
CUISINIER
NumCuisinier | NomCuis | ...
----------------------------
1 | DURAND | ... -- chef with id 1 has name 'DURAND' and ...
...
-- AND for every absent row (NUMCUISINIER, NOMCUIS, ...),
NOT (chef with id NUMCUISINIER has name NOMCUIS and ...)
Each SQL expression/clause transforms an old table value to a new value holding the rows that make a true statement from some new predicate that can be expressed in terms of the original's predicate.
The predicate of R JOIN S
is the predicate of R
AND
ed with the predicate of S
. The predicate of R ON/WHERE
condition
is the predicate of R
AND
ed with condition
.
/* rows where
chef with id c.NUMCUISINIER has name c.NOMCUIS and ...
AND kitchen with id a.NUMCUISINE ...
AND c.NUMCUISINE = a.NUMCUISINE
*/
CUISINIER c join ATELIER_CUISINE a on c.NumCuisine = a.NumCuisine
So we query by writing an SQL expression whose associated predicate characterizes the application relationship (aka association) whose rows we want.
FKs (foreign keys) are not relationships.
FK constraints are called relationships by some people, but they are not. They are facts.
A FK of a table is a set of columns. "FK" is also used to mean an associated constraint that we have when we have a FK. Which like every constraint is a true statement in every database state. (Equivalently, every application situation.) A FK constraint says that values for certain columns in a certain table are also values for certain columns in a certain other table where they form a CK (candidate key). (Equivalently, it says that if some values/entities satisfy a certain application relationship then some of them plus some other values/entities satisfy a certain other application relationship where the values/entities form a CK.) A PK (primary keys) is just some CK that you decided to call PK.)
A FK constraint has a certain associated application relationship, but that's not what is meant when "relationship" is used for "FK (constraint)". ("FK" is also used to mean a subrow value for the columns of a FK, or a value in a row for the column of a one-column FK.)
You need to know what each table means.
Predicates must be supplied by the designer along with a schema. You need to find out what the tables mean. Then express your query predicate in terms of them. Then convert to SQL.
Sometimes we guess at the predicates more or less successfully via common sense and naming. FKs and other constraints can help with guessing.
Common sense, names, PKs (underlined?) and FKs ("#"?) suggest table meanings for you like:
-- chef with id NUMCUISINIER has name NOMCUIS and start date DATEEMB and works in kitchen with id NUMCUISINE
CUISINIER(NumCuisinier, NomCuis, NateEmb, NumCuisine)
-- kitchen with id NUMCUISINE has phone number TELCUISINE and chef with id NUMCUISINIER as head chef
ATELIER_CUISINE(NumCuisine, TelCuisine, NumCuisinier)
FKs are not needed to query
In the SQL query above the rows that make the predicate into a true proposition are always the rows returned. It doesn't matter how many rows there are per NumCuisiner
or NumCuisine
value (ie whether they are PKs) or whether a value must appear in another table (ie whether they are FKs). Or what any other constraint is.
We need to know the predicates to query. When we know them we don't need to know any constraints. We don't need to know FKs.
FKs, CKs, PKs, alternate keys and UNIQUE column sets (superkeys) are irrelevant to querying except that if you know something is a superkey because of one then you can write queries involving extracting a value from a one-row result. But you could have expressed the same result without extractions.