I'm having a problem with thinking of the way to connect two tables. I have one table with actions (RAD):
CREATE TABLE RAD (
rad_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
spec_id INT NULL,
predp_id INT NULL,
predf_id INT NULL,
strp_ID INT NULL,
strf_ID INT NULL,
---more fileds---
FOREIGN KEY (spec_id) REFERENCES SPEC(spec_id) ON DELETE SET NULL,
FOREIGN KEY (strp_ID) REFERENCES STRANKEP(strp_ID) ON DELETE CASCADE,
FOREIGN KEY (strf_ID) REFERENCES STRANKEF(strf_ID) ON DELETE CASCADE,
FOREIGN KEY (predp_id) REFERENCES PREDMETIP(predp_id) ON DELETE CASCADE,
FOREIGN KEY (predf_id) REFERENCES PREDMETIF(predf_id) ON DELETE CASCADE
) ENGINE=InnoDB COLLATE utf8_general_ci;
And one table of specifications (SPEC) based on whom bill will be made:
CREATE TABLE SPEC (
spec_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
---more fileds---
) ENGINE=InnoDB COLLATE utf8_general_ci;
As you can see action rad_id(RAD) row will be deleted if any client (strp_ID or strf_ID) will be deleted. The same goes for case(predp_id and predf_id).
Now I want to restrict delete of action rad_id(RAD row) if its included in specification. Therefore, when specification is made it inserts spec_id(SPEC) in spec-id(RAD) filed.
When specification is deleted field goes back to null and that works. BUT it allows me to delete the action rad_id(RAD) when it was included in specification(SPEC) and has that foreign key spec_id included in RAD table. And I can not let that happen. It should delete only when its null and specification key is not present.
The problem is specification will contain MULTIPLE actions rad_id's(RAD) so I can not tie it with one more column rad_id(RAD) as foreign key.
I don't know how to approach this problem.
RAD TABLE
rad_id spec_id
1 1
2 1
3 1
4 null
SPEC TABLE
spec_id rad_id-reference
1 1,2,3
As seen above SPEC table row will be made out of 3 rad_id's, I need a way to say rad_id's 1,2 and 3 can not be deleted if spec_id 1 exists. rad_id 4 can be deleted.
The problem is that I can not make rad_id-reference on SPEC table a FOREIGN KEY made out of 3 rad_id's.