Hey I have a table that has two fields that have a relationship I would like
to enforce.
The fields are delivered and cancelled. They are both Yes/No Fields.
I would like to make a rule for both of them that states they both can't be
equal to yes to avoid user errors.
I understand you may have chosen this design because it is conducive
for bound controls for the table to resemble the input form. In which
case, you could use DRI (foreign key, Relationship enforcing data
integrity, whatever it's called in the UI): put the valid states in a
table. I think this might be easier to maintain.
In SQL DDL code it would look like this:
CREATE TABLE PackageStates (
delivered YESNO NOT NULL,
cancelled YESNO NOT NULL,
PRIMARY KEY (delivered, cancelled)
)
;
INSERT INTO PackageStates (delivered, cancelled)
VALUES (TRUE, FALSE)
;
INSERT INTO PackageStates (delivered, cancelled)
VALUES (FALSE, TRUE)
;
INSERT INTO PackageStates (delivered, cancelled)
VALUES (FALSE, FALSE)
;
CREATE TABLE Packages (
order_ID INTEGER NOT NULL PRIMARY KEY,
delivered YESNO NOT NULL,
cancelled YESNO NOT NULL,
FOREIGN KEY (delivered, cancelled)
REFERENCES PackageStates (delivered, cancelled)
)
;
This would be OK:
INSERT INTO Packages (order_ID, delivered, cancelled)
VALUES (1, FALSE, FALSE)
;
This would fail because delivered=TRUE, cancelled=TRUE is an illegal
state:
INSERT INTO Packages (order_ID, delivered, cancelled)
VALUES (2, TRUE, TRUE)
;
Jamie.
--