In the following scenario, I want to make sure that if you take a single entry of d, you can assume that it's associated b, and c have the same a_id:
CREATE TABLE A (
id INT NOT NULL,
);
CREATE TABLE B (
id INT NOT NULL,
a_id INT NOT NULL,
CONSTRAINT fk_b_a FOREIGN KEY (a_id) REFERENCES A (id) ON DELETE NO ACTION ON UPDATE CASCADE,
);
CREATE TABLE C (
id INT NOT NULL,
a_id INT NOT NULL,
CONSTRAINT fk_c_a FOREIGN KEY (a_id) REFERENCES A (id) ON DELETE NO ACTION ON UPDATE CASCADE,
);
CREATE TABLE D (
id INT NOT NULL,
b_id INT NOT NULL,
c_id INT NOT NULL,
CONSTRAINT fk_d_b FOREIGN KEY (b_id) REFERENCES B (id) ON DELETE NO ACTION ON UPDATE CASCADE,
CONSTRAINT fk_d_c FOREIGN KEY (c_id) REFERENCES C (id) ON DELETE NO ACTION ON UPDATE CASCADE,
);
Is there a way to ensure this with a check constraint? Or maybe my database design is flawed?