Académique Documents
Professionnel Documents
Culture Documents
Problem Sheet – 5
Active Databases
Consider the following database schema:
I) Enforce the following constraints using a set of CHECK constraints, referential integrity
constraints, or triggers.
Ex 1. The age of employees must be greater than 18.
Ex 2. The supervisor of an employee must be older than the employee.
Ex 3. The salary of an employee cannot be greater than the salary of his/her supervisor.
Ex 4. The manager of a department must be an employee of that department.
Ex 5. The location of a project must be one of the locations of its department.
Ex 6. The hire date of employees must be greater than their birth date.
Ex 7. A supervisor must be hired at least 1 year before every employee s/he supervises.
Ex 8. The attribute Department.NbrEmployees is a derived attribute from Employee.DNo.
Ex 9. An employee works at most in 4 projects.
Ex 10. An employee works at least 30h/week and at most 50 h/week on all its projects.
Ex 11. Among all employees working on a project, at most 2 can work for less than 10 hours.
Ex 12. Only department managers can work less than 5 hours on a project.
Ex 13. Employees that are not supervisors must work at least 10 hours on every project they
work.
Ex 14. The manager of a department must work at least 5 hours on all projects controlled by
the department.
Ex 15. The attribute Employee.SuperSSN is a derived attribute computed as follows.
Department managers are supervised by the manager of Department 1 (Headquarters).
Employees that are not managers are supervised by the manager of their department. Finally,
the manager of Department 1 has a null value in attribute SuperSSN.
Ex 16. The supervision relationship defined by Employee.SuperSSN must not be cyclic. (It is
supposed that attribute Employee.SuperSSN is not derived as stated above.)
II) A business rule states that each time one or more employees are added to the employees table,
an audit record must also be created. This rule could be enforced using a DML statement trigger.
Create an audit table by executing the following SQL statement:
CREATE TABLE audit_table
(action VARCHAR2(15), user_name VARCHAR2(30) DEFAULT USER, last_change_date TIMESTAMP
DEFAULT SYSTIMESTAMP);
Create a statement level trigger that inserts a row into the audit table immediately after one or
more rows are added to the employees table that you created in WS-1. The audit table row
should contain value “Inserting” in the action column. The other two columns should have their
default values. Test your trigger by inserting a row into employees, then querying the audit table
to see that it contains a row.
II) The table PRODUCTS has 2 columns, PRO_ID and PRICE. The table is subject to the
Business Rule: Product price cannot be changed (increased or decreased) by more than 15
percent.
Name Null Type
---------- ------------- --------------
PRO_ID NOT NULL NUMBER
PRICE NOT NULL NUMBER
Create the PRO_TRG trigger that raises the application error 'Price can not be changed by
more than 15%’ when an update statement tries to change price by more than 15 percent.
Insert suitable rows into the PRODUCTS table and test your trigger.