NAME
dbfind_join_keys - find join keys between two tables
SYNOPSIS
int dbfind_join_keys ( Dbptr db1, Dbptr db2, Tbl **keys1, Tbl **keys2 )
DESCRIPTION
dbfind_join_keys implements the method Datascope employs to infer
the natural keys on which joins between two tables should occur.
The keys are inferred based on the primary, alternate, and foreign keys
for the two tables, and is based on matching
names for the keys of the two tables. It proceeds as follows:
-
1)
If any foreign key in the first table is a part of the primary
or alternate key in the second table, use it as the join key.
-
2)
If any foreign key in the second table is a part of the primary
or alternate key in the first table, use it as the join key.
-
3)
Compare the primary keys of the two tables; if any fields match,
use those as the join keys.
-
4)
Compare the alternate keys of the two tables; if any fields match,
use those as the join keys.
-
5)
Compare the primary keys of the first table to the alternate keys of
the second table; if any fields match, use those as the join keys.
-
6)
Compare the primary keys of the second table to the alternate keys of
the first table; if any fields match, use those as the join keys.
The process is complicated somewhat by the presence of ranges, for
example a time range. A range matches another range
with the same start and end names. (As a special concession to the
CSS 3.0 database, time::endtime also matches ondate::offdate. The
original decision to have the fields ondate and offdate was flawed.)
A range also matches a single field which has the same name as
the start of the range. For instance, time matches time::endtime.
Table which are the result of joins inherit a subset of the keys
possessed by their parents. This process is strictly ad-hoc,
and is not guaranteed to produce the correct subsequent joins.
However, it is very often correct, and by careful choice of the order in which
you join tables, you can usually force the correct join.
RETURN VALUES
dbfind_join_keys returns 0 for success; dbINVALID indicates a bad
dbptr, while 1 indicates a failure to find matching keys.
If successful, the matching keys for each table are returned in
the two Tbl lists. These lists should be freed with
freetbl(tbl,0).
LIBRARY
$(DBLIBS)
SEE ALSO
dbjoin(1), dbfind_join_tables(3)
AUTHOR
Daniel Quinlan