I'm pretty new to Oracle and database design in general but I have a question. I have two basic tables called employee and department. The employee table holds a salary property. Now I need to implement a new business rule: The total of all employee salaries in one department (an employee is part of a department) may not exceed 200k. I know this can be done with a materialized view, but can it also be done with unique indexes? If so, how would one approach this particular situation?
No, you can't use a unique index to enforce this sort of rule.
You could try to enforce this sort of rule via triggers but that tends to get rather difficult in a multiuser environment. You'd need to do things like lock the department
row to ensure that only one session can be modifying employee
information for a particular department
at a time which tends to introduce significant scalability issues. It also tends to involve quite a bit of code to handle all the potential cases.
This can't be done with any indexes. But it is can be done using a trigger.
If you add a column maxDeptSalary
to your departments
table, then you can create a trigger on Insert
and Update
in which you
maxDeptSalary
and get the value Sum
on existing records in your employee table for that department sum + new.value <= maxDeptSalary
But the reality is, this is should be done on application level, not the database
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.