Ofer said:
You can do that using a form to enter data in the table.
The OP asked for a Validation Rule, which is wise because *only*
putting validation in the front end application is not good idea (see
http://www.dbazine.com/ofinterest/oi-articles/celko25).
I've implemented example Validation Rules as CHECK constraints:
CREATE TABLE Test8 (
two_possible_values CHAR(1) DEFAULT 'Y' NOT NULL,
CONSTRAINT two_possible_values__legal_values
CHECK (two_possible_values IN ('Y', 'N')),
dependent_col VARCHAR(20),
CONSTRAINT dependent_col__never_zero_length
CHECK (LEN(dependent_col) > 0),
CONSTRAINT dependent_col__only_when__two_possible_values_col__eq_Y
CHECK
(
IIF(two_possible_values = 'Y', 1, 0)
=
IIF(dependent_col IS NULL, 0, 1)
)
);
This fails the rule dependent_col__never_zero_length:
INSERT INTO Test8 (two_possible_values, dependent_col) VALUES ('Y', '')
This fails the rule dependent_col__never_zero_length:
INSERT INTO Test8 (two_possible_values, dependent_col) VALUES ('N',
'');
This fails the rule
dependent_col__only_when__two_possible_values_col__eq_Y:
INSERT INTO Test8 (two_possible_values, dependent_col) VALUES ('Y',
NULL);
This fails the rule
dependent_col__only_when__two_possible_values_col__eq_Y:
INSERT INTO Test8 (two_possible_values, dependent_col) VALUES ('N',
'Because');
This is legal:
INSERT INTO Test8 (two_possible_values, dependent_col) VALUES ('Y',
'Because');
This is legal:
INSERT INTO Test8 (two_possible_values, dependent_col) VALUES ('N',
NULL);
Jamie.
--