I manage a MySQL database of ten or so tables.
In higher code, I have a list of primary keys belonging to entries of some table A
. These A
entries have foreign keys to entries in another table B
. The relationship is 1-to-many, so several A
s may point to the same B
.
Problem: I need to determine if the list of primary keys I hold in higher code (Scala) is homogeneous. That is, if their foreign keys to B
all correspond to the same entry in B
.
Question: Would it be possible to determine this by analysing the results of a single SQL call?
Best Answer
For the table
A
(withB_id
) and tableB
(withid
)Count of Primary Keys By Foreign Key
If
FK
is 0, thenPKCOUNT
is a count of Primary Keys that do not have a Foreign KeyTo be 100% homogenous,
FK
should never show up as zero(0
) (In a Perfect World).Count of Foreign Keys Used By Primary Keys
If
FKCount
is 0, thenB.id
is a Foreign Key Not Used By Any Primary KeyTo be 100% homogenous,
FKCount
should never show up as zero(0
) (In a Perfect World).