Sei sulla pagina 1di 3

PSG COLLEGE OF TECHNOLOGY, COIMBATORE -4

DEPARTMENT OF APPLIED MATHEMATICS AND COMPUTATIONAL SCIENCES


V M.Sc. Theoretical Computer Science – 15XT57 RDBMS Lab

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.

III) Create 2 tables,


RESERVATIONS table with 2 columns:
Name Null Type
---------- ------------- ---------------
FLIGHT_ID NOT NULL CHAR(6)
CUSTOMER_PHONE NOT NULL NUMBER and

FLIGHTS table with 2 columns:


FLIGHT_ID NOT NULL CHAR(6)
SEATS NOT NULL NUMBER
Make FLIGHT_ID the primary key in the FLIGHTS table.
Insert the following rows into FLIGHTS:
FLIGHT_ID SEATS
AC0529 120
AC0530 0
Create a trigger RES_TRG that will ensure that when a new row is inserted into the
RESERVATIONS table, the flight id is in the FLIGHTS table and that the number of seats on
this flight, SEATS is greater than 0. Here are the details of how the trigger should behave:
i. If flight id is not in the flights table it should raise application error ‘Invalid flight id’
ii. If flight id is in the flights table, (for example AC0529) but SEATS = 0, then it should raise
application error ‘Flight AC0529 has no seats left’
iii. If flight id is in the flights table and SEATS > 0, then it should update the appropriate row
in flights table by setting SEATS = SEATS – 1 for this flight.

Potrebbero piacerti anche