I have 3 tables:
Molecule:
id
Atom:
id
MoleculeAtom: # Composite primary key
molecule_id
atom_id
My goal is to ensure that no combination of atoms which make up a molecule, are repeated. For example, the water molecule, I would store two rows in the MoleculeAtom table; 1 row for a hydrogen atom and 1 row for an oxygen atom. As you can see, I need to ensure that no other molecule has JUST hydrogen and oxygen, even though there may be other molecules which include hydrogen and oxygen.
At this point I have a query which identifies which molecules includes either hydrogen or oxygen, and only having 2 atoms in the MoleculeAtom table.
SELECT
m.id, m.name, (SELECT count(*) from molecule_atom where molecule_id = m.id group by molecule_id) as atomCount
FROM
molecule AS m
INNER JOIN
molecule_atom AS ma ON ma.molecule_id = m.id
WHERE
ma.atom_id IN (1,2)
HAVING atomCount = 2;
Which returns (demonstrative snippet):
+----+----------------------------+-----------+
| id | name | atomCount |
+----+----------------------------+-----------+
| 53 | Carbon Dioxide | 2 |
| 56 | Carbon Monoxide | 2 |
+----+----------------------------+-----------+
(I know, that both CO and CO2 have the same exact atoms, in differing quantities, but dis-regard that, as I am tracking the quantities as a another column in the same table.)
As of now I am pulling the above results and checking their atom_ids via PHP, which means I have to issue a separate query for each molecule, which seems inefficient, so I was looking to see if it's possible to do this checking using strictly SQL.
Excuse any mistakes which may be chemical related, it's been a long time since chem101.