Lab Manual For Question 2 in DBMS
Lab Manual For Question 2 in DBMS
In this
context, create two tables employee and department. Employee consists
of columns
empno, empname, basic, hra, da, deductions, gross, net, date-of-birth. The
calculation
of hra,da are as per the rules of the college. Initially only empno, empname,
basic have
valid values. Other values are to be computed and updated later.
Department contains
deptno, deptname, and description columns. Deptno is the primary key in
department
table and referential integrity constraint exists between employee and
department
tables. Perform the following operations on the the database:
Create tables department and employee with required constraints.
create table dept ( deptno varchar2(20) constraint dept_pk primary key,
deptname varchar2(20),
description varchar2(20));
create table emp (empno varchar2(20) constraint emp_pk primary key,
empname varchar2(20),
basic number(10,2),
hra number(10,2),
da number(10,2),
deductions number(10,2),
gross number(11,2),
net number(11,2),
deptno varchar2(20) references dept(deptno));
Initially only the few columns (essential) are to be added. Add the
remaining
columns separately by using appropriate SQL command
ALTER TABLE EMP ADD DOB DATE;
ALTER TABLE EMP ADD DOJ DATE;
Basic column should not be null
ALTER TABLE EMP ADD CONSTRAINT emp_nn1 not null ;
Add constraint that basic should not be less than 5000.
ALTER TABLE EMP ADD CONSTRAINT emp_chk1 check(basic>5000);
insert
insert
insert
insert
into
into
into
into
dept
dept
dept
dept
values
values
values
values
('10','ACCOUNTING','ST LOUIS')
('20','RESEARCH','NEW YORK')
('30','SALES','ATLANTA')
('40','OPERATIONS','SEATTLE')
values (1,'JOHNSON',6000,3400,800,null,null,null,12-17values (2,'HARDING',10000,4400,1800,null,null,null,'02-02values (3,'TAFT',7000,3600,1000,null,null,null,'01-02values (4,'HOOVER',5000,3400,1200,null,null,null,'04-02values (5,'LINCOLN',7000,3800,1300,null,null,null,'06-23values (6,'GARFIELD',8000,4400,900,null,null,null,'05-01values (7,'POLK',6000,3800,800',null,null,null,'09-22values (8,'GRANT',7000,3700,800',null,null,null,'03-30values (9,'JACKSON',8000,3400,900',null,null,null,'01-01values (10,'FILLMORE',9000,4600,950',null,null,null,'08-09values (11,'ADAMS',6000,3400,800',null,null,null,'03-15values (12,'WASHINGTON',7000,3800,800',null,null,null,'04values (13,'MONROE',6500,3400,800',null,null,null,'12-03values (14,'ROOSEVELT',6600,3400a,800',null,null,null,'10-
for i in e loop
update emp set hra=I.basic*0.5,
da=I.basic*0.1,
deductions=I.basic*0.15,
gross=I.basic+I.hra+I.da,
net=I.gross-I.deductions
wherE empno=I.empno;
end loop;
end;
Whenever salary is updated and its value becomes less than 5000 a trigger
has to
be raised preventing the operation.
CREATE OR REPLACE TRIGGER SALUPDTRIG BEFORE UPDATE ON EMP FOR
EACH ROW
DECLARE
BEGIN
IF :NEW.BASIC<5000 THEN
RAISE_APPLICATION_ERROR(-20101,'BASIC CANNOT BE LESS THAN 5000');
END IF;
END;
The assertions are: hra should not be less than 10% of basic and da should
not be
less than 50% of basic.
ALTER TABLE EMP ADD CONSTRAINT emp_chk2 check(DA>basic*0.1);
ALTER TABLE EMP ADD CONSTRAINT emp_chk3 check(HRA>basic*0.5);
The percentage of hra and da are to be stored separately.
ALTER TABLE EMP ADD HRAP NUMBER(5,2),DAP NUMBER(5,2);
When the da becomes more than 100%, a message has to be generated
and with
user permission da has to be merged with basic.
CREATE OR REPLACE PROCEDURE DAPROCEDURE AS
CURSOR C IS SELECT * FROM EMP;
CH CHAR(2) :='&CH';
BEGIN
FOR I IN C LOOP
IF I.DA=I.BASIC THEN
Create alias for columns and use them in queries.
SELECT EMPNO EMPLOYEE NUMBER, EMPNAME EMPLOYEE NAME FROM
EMP;
List the employees according to ascending order of salary.
SELECT * FROM EMP ORDER BY NET;
List the employees according to ascending order of salary in each
department.
SELECT * FROM EMP GROUP BY NET;
Amount 6000 has to be deducted as CM relief fund in a particular month
which
has to be accepted as input from the user. Whenever the salary becomes
negative it has to be maintained as 1000 and the deduction amount for
those
employees is reduced appropriately.
The retirement age is 60 years. Display the retirement day of all the
employees.
SELECT ADD_MONTHS(DOB,60*12) "RETIREMENT DATE" FROM EMP;
If salary of all the employees is increased by 10% every year, what is the
salary of
all the employees at retirement time.
SELECT (NET+NET*0.1)*MONTHS_BETWEEN(SYSDATE,DOJ)/12 FROM EMP;
Find the employees who are born in leap year.
SELECT * FROM EMP WHERE TO_NUMBER(TO_CHAR(DOB,YY))%4=0 OR
TO_NUMBER(TO_CHAR(DOB,YY))%400 OR TO_NUMBER(TO_CHAR(DOB,YY))
%100!=0;
Find the employees who are born on feb 29.
SELECT * FROM EMP WHERE TO_CHAR(DOB,MON DD)=FEB 29;
Find the departments where the salary of atleast one employee is more
than
20000.
SELECT * FROM DEPT WHERE DEPTNO IN(SELECT DEPTNO FROM EMP E,EMP EE
WHERE E.NET >EE.NET);
Find the departments where the salary of all the employees is less than
20000.
SELECT * FROM DEPT WHERE DEPTNO IN(SELECT DEPTNO FROM EMP WHERE
NET<20000);
On first January of every year a bonus of 10% has to be given to all the
employees. The amount has to be deducted equally in the next 5 months.
Write
procedures for it.
CREATE OR REPLACE PROCEDURE SALDED AS
CURSOR C IS SELECT * FROM EMP;
BEGIN
FOR I IN C LOOP
IF TO_CHAR(SYSDATE,MON DD)=JAN 01 THEN
UPDATE EMP SET NET=I.NET+I.NET*0.1 WHERE EMPNO=I.EMPNO;
END IF;
IF TO_CHAR(SYSDATE,MON DD)=FEB 01 THEN
UPDATE EMP SET NET=I.NET-I.NET*0.02 WHERE EMPNO=I.EMPNO;
END IF;
IF TO_CHAR(SYSDATE,MON DD)=MAR 01 THEN
UPDATE EMP SET NET=I.NET-I.NET*0.02 WHERE EMPNO=I.EMPNO;
END IF;
IF TO_CHAR(SYSDATE,MON DD)=APR 01 THEN
UPDATE EMP SET NET=I.NET-I.NET*0.02 WHERE EMPNO=I.EMPNO ;
END IF;
IF TO_CHAR(SYSDATE,MON DD)=MAY 01 THEN
UPDATE EMP SET NET=I.NET-I.NET*0.02 WHERE EMPNO=I.EMPNO;
END IF;
IF TO_CHAR(SYSDATE,MON DD)=JUN 01 THEN
UPDATE EMP SET NET=I.NET-I.NET*0.02 WHERE EMPNO=I.EMPNO;
END IF;
END LOOP;
END;
As a designer identify the views that may have to be supported and create
views.
CREATE VIEW EMPVIEW AS SELECT EMPNAME,NET FROM EMP;
CREATE VIEW EMPDEPT AS SELECT EMPNAME,DNAME FROM EMP,DEPT WHERE
EMP.DEPTNO=DEPT.DEPTNO;