Olaf Doschke
Programmer
I have a problem with either a circle reference or a constraint. It seems I have to choose one or the other to me, but perhaps there is something SQL 2008R2 offers I have overlooked.
To describe a bit, first:
1. There are production charges, which are tested in several tests. (so I have charges, 1:n related to tests)
2. Each test will require 1 or more samples (so I have samples 1:n related to tests)
So far a simple 1:n:m relationship between Charges-<Tests-<Samples
3. The samples are samples of the charges of course
I avoid the circle reference problem, by not letting samples reference the charges they come from, they indirectly reference the charge by the tests they are assigned to.
The number of samples needed depends on the tests, so they are created while putting together the test details, that's also a good reason to let samples reference the tests they are for, not the charge they come from.
The samples are simply numbered sequentially and the business logic of the application creates unique sample numbers for each charge by determining the max sample number from all samples related to any test of the same charge, and that works OK, even if not all tests of a charge are loaded into the test design UI. But by Murphy's law, we found double sample numbers in the database, where there shouldn't be.
The constraint that I would need to define in the database, to make it failsafe against this, would need to take the sample and lookup the indirectly related charge to make a check for uniqueness of charge ID and sample number.
I would be ok in doing such a constraint rule, but is there a better design I could also apply? For the ease of the sample number constraint, I could let samples reference the charges they come from, then simply all value tuple sample.chargeid,sample.number would need to be unique.
I just fear this will lead to even worse nightmares with samples referencing tests that in turn don't reference the same charge as the sample would. Each foreign key constraint can be okay and still you can get such references not closing the circle, but making a spiral reference to a wrong test or charge. This would lead to a ripple effect in the worst case you could hardly know which reference is right and which wrong.
Finally the question: How could I harden the database to not allow double sample numbers? Is there something SQL2008R2 offers about constraints with data from parent tables? Or is there something checking if a circle reference of three tables really does form a circle and has no inconsistency of direct vs indirect references?
Bye, Olaf.
To describe a bit, first:
1. There are production charges, which are tested in several tests. (so I have charges, 1:n related to tests)
2. Each test will require 1 or more samples (so I have samples 1:n related to tests)
So far a simple 1:n:m relationship between Charges-<Tests-<Samples
3. The samples are samples of the charges of course
I avoid the circle reference problem, by not letting samples reference the charges they come from, they indirectly reference the charge by the tests they are assigned to.
The number of samples needed depends on the tests, so they are created while putting together the test details, that's also a good reason to let samples reference the tests they are for, not the charge they come from.
The samples are simply numbered sequentially and the business logic of the application creates unique sample numbers for each charge by determining the max sample number from all samples related to any test of the same charge, and that works OK, even if not all tests of a charge are loaded into the test design UI. But by Murphy's law, we found double sample numbers in the database, where there shouldn't be.
The constraint that I would need to define in the database, to make it failsafe against this, would need to take the sample and lookup the indirectly related charge to make a check for uniqueness of charge ID and sample number.
I would be ok in doing such a constraint rule, but is there a better design I could also apply? For the ease of the sample number constraint, I could let samples reference the charges they come from, then simply all value tuple sample.chargeid,sample.number would need to be unique.
I just fear this will lead to even worse nightmares with samples referencing tests that in turn don't reference the same charge as the sample would. Each foreign key constraint can be okay and still you can get such references not closing the circle, but making a spiral reference to a wrong test or charge. This would lead to a ripple effect in the worst case you could hardly know which reference is right and which wrong.
Finally the question: How could I harden the database to not allow double sample numbers? Is there something SQL2008R2 offers about constraints with data from parent tables? Or is there something checking if a circle reference of three tables really does form a circle and has no inconsistency of direct vs indirect references?
Bye, Olaf.