Here is a simple situation. (My apologies my SQL is quite rusty.)
(I am using MySQL and Java but it should not matter too much.)
Let’s say I have two tables.
Here is a simple SQL statement:
A:
insert into patient (patient_id) values (16)
Now there is another table person, which has a person_id and that has to be constrained to match patient_id.
B:
CONSTRAINT `person_id_for_patient` FOREIGN KEY (`patient_id`) REFERENCES `person` (`person_id`) ON UPDATE CASCADE)
What I am looking for is some pre-built tool, solution, etc., that might allow one to expand an insert statement such as A in order to satisfy constraints automatically.
In other words, that would automatically expand A into:
insert into person (person_id) values (16)
insert into patient (patient_id) values (16)
Any ideas?
Thank you
Misha
Consider these 2 solutions to handle your business logic requirements:
personand thepatienttables.This will help control the business logic. You’ll have to enforce that all code NOT directly insert into the
personusing adhoc SQL. How easy/hard this is depends on your environment.after inserton thepersontable to automatically insert intopatient. Creating triggers in MySQL. Triggers will definitely solve your dev problem here, but generally aren’t overall a great solution, as you tend to ‘forget’ that they’re there. Later, when you are investigating a defect/problem in your database, you may overlook the hidden logic in the trigger. Read more opinions on triggers.Either way, with either of these two, you’ll get the creation of a record into the
patientafterperson.The question then becomes – does this business rule need enforcing the other way around? What happens when a process tried to write into the
patienttable? Does it require that a match exist in thepersontable?