任务是为常规约束编写一个alter查询。具体来说:“在Employee中,如果EmployeeLevel为NULL,则EmployeeSalary也必须为NULL”。如果有人能解释正确的解决方案,将不胜感激!
ALTER TABLE Employee
ADD CONSTRAINT
CHECK (
EmployeeLevel IS NULL
AND EmployeeSalary IS NULL
);
答案 0 :(得分:1)
可以接受以下语句:“如果employeeLevel不为null,则employeeSalary可以为null”。 “和反之亦然”不能。那将说“如果employeeSalary不为null,那么employeeLevel可以为null”,但是与原始问题陈述“如果EmployeeLevel为NULL,那么EmployeeSalary也必须为NULL”相矛盾。结果是直接的OR条件,恕我直言,意图的最清晰表达:
check ((EmployeeLevel is null and EmployeeSalary is null) or (EmployeeLevel is not null))
答案 1 :(得分:0)
这是一种方法:
CHECK (EMPLOYELEVEL IS NOT NULL OR EmployeeSalary IS NULL)
您可能会发现NOT
逻辑更清晰:
CHECK (NOT (EMPLOYELEVEL IS NULL AND EmployeeSalary IS NOT NULL))