考虑以下两个描述某个组织的员工和部门的关系模式(假设所有名称都是唯一的)。
EMPLOYEES (EmployeeName, Salary, DeptName)
DEPARTMENTS (DeptName, ManagerName, City)
在架构级别定义一个约束(SQL断言),该约束指定位于“旧金山”的任何部门的员工的工资都不超过“管理”部门中任何员工的工资。
我的方法:
create assertion CHECK_SALARY as CHECK
(
not exists (select * from EMPLOYEES
where select salary from EMPLOYEES
where EMPLOYEES.DeptName = 'San Fransisco' <
select salary from EMPLOYEES
where EMPLOYEES.DeptName = 'Management'
)
这是我写的东西,但它没有正常工作,任何建议表示赞赏
答案 0 :(得分:0)
尝试这样的事情:
CREATE ASSERTION check_salary AS CHECK (
NO EXTISTS ( SELECT * FROM employees
WHERE ( SELECT MAX(salary) FROM employees
WHERE "DeptName" = 'San Fransisco' )
< ( SELECT MIN(salary) FROM employees
WHERE "DeptName" = 'Management' ) )
);
答案 1 :(得分:0)
试试这个。
CREATE ASSERTION check_salary AS CHECK (NO EXTISTS ( SELECT * FROM Employees ee, departments dd WHERE ee.dept_name = dd.dept_name AND dd.city = 'San Francisco' AND ee.salary < (SELECT Max(salary_amount) FROM Employees e, Departments d WHERE e.dept_name =d.dept_name AND d.Dept_name = 'Management') ));