Is it possible to have optional relationships in mySQL? I have a table that stores jobs carried out by engineers and uses foreign keys to link to the different types of job that they may do. Here is the table:
(The different types of job are onsiterepair, servicereport, pattest and pressuretest)
Quite often an engineer will only carry out 1 or 2 of the types of job types available. In that case a query such as
INSERT job VALUES(NULL, 1, "12423/2", "2002-06-02", "12:00", 3, 2, NULL, NULL);
would be ideal but mySQL gives the error "Cannot add a child row: a foreign key constraint fails". Is there any way to make the foreign key relationships optional? I would prefer to keep the keys in if at all possible. I have thought about just having a "dummy" row in each of the 4 linked tables that I could link to instead of using NULL (e.g. row 1 could be a dummy row), but this doesn't seem a very elegant solution.
Thanks in advance for any help you may be able to provide.
Code:
CREATE TABLE job(
job_id INT AUTO_INCREMENT,
PRIMARY KEY(job_id),
engineer_id INT NOT NULL,
INDEX engineer_ind (engineer_id),
FOREIGN KEY (engineer_id)
REFERENCES engineer(engineer_id),
reference VARCHAR(10),
submitdate DATE NOT NULL,
submittime TIME NOT NULL,
onsiterepair_id INT,
INDEX onsiterepair_ind (onsiterepair_id),
FOREIGN KEY (onsiterepair_id)
REFERENCES onsiterepair(onsiterepair_id),
servicereport_id INT,
INDEX servicereport_ind (servicereport_id),
FOREIGN KEY (servicereport_id)
REFERENCES servicereport(servicereport_id),
pattest_id INT,
INDEX pattest_ind (pattest_id),
FOREIGN KEY (pattest_id)
REFERENCES pattest(pattest_id),
pressuretest_id INT,
INDEX pressuretest_ind (pressuretest_id),
FOREIGN KEY (pressuretest_id)
REFERENCES pressuretest(pressuretest_id)
);
(The different types of job are onsiterepair, servicereport, pattest and pressuretest)
Quite often an engineer will only carry out 1 or 2 of the types of job types available. In that case a query such as
INSERT job VALUES(NULL, 1, "12423/2", "2002-06-02", "12:00", 3, 2, NULL, NULL);
would be ideal but mySQL gives the error "Cannot add a child row: a foreign key constraint fails". Is there any way to make the foreign key relationships optional? I would prefer to keep the keys in if at all possible. I have thought about just having a "dummy" row in each of the 4 linked tables that I could link to instead of using NULL (e.g. row 1 could be a dummy row), but this doesn't seem a very elegant solution.
Thanks in advance for any help you may be able to provide.