Suppose that F
is an Oracle table with two columns, X
and Y
, such that the constraint
CONSTRAINT unique_xy_pairs UNIQUE (X, Y)
is satisfied.
For each value [x] of F.X
, let S[x] represent the set consisting of the values returned by the following (pseudo-SQL) query:
SELECT Y FROM F WHERE X = [x];
Now, I want to enforce the constraint that for any two arbitrary values [x1] and [x2] of F.X
, their corresponding sets S[x1] and S[x2] must be either disjoint or identical.
How can I do this?
EDIT: A uniqueness constraint on F.Y
would not be a valid solution, since such a constraint could be violated by tables that nonetheless satisfy the constraint described in the original statement.
For example, the following is an F
table that does not satisfy a unique F.Y
constraint, but satisfies the "disjoint or identical" constraint in the original post:
X Y
10 0
10 1
20 2
20 3
20 4
30 0
30 1
Best Answer
After I posted my question, I thought of a possible solution.
For concreteness, let's say that this is the table definition for
F
:We could replace this
F
with tablesG
,H
, andI
, defined as followsThe idea is that
H
is now a table of "y-sets". Each y-set consists of all the [y]'s corresponding to a given value ofH.YSET_ID
. The fundamental difference betweenF
andH
is that forH
we can define a uniqueness constraint onH.Y
, which ensures that these y-sets are disjoint.Table
I
's only function is to relateG
andH
. (It would be convenient ifG.YSET_ID
could refer directly toH.YSET_ID
, rather than indirectly viaI.ID
, but my understanding is that a foreign key must always refer to a primary key...)(I don't know much about Oracle's version of SQL, so this solution is bound to be clumsy, or even syntactically incorrect.)
For example, if
F
is...then the corresponding tables
G
,H
, andI
could beAs pointed out by @ypercubeᵀᴹ, the original
F
can be recovered from the newG
,H
, andI
tables withEDIT: Yet another afterthought: it turns out the there is a surprisingly symmetrical variation of the solution above:
Even though it is not clear from my problem description, a table satisfying the desired constraint expresses a bijective function whose domain and codomain are both sets of disjoint sets. My second solution above makes this symmetry manifest.