Sie sind auf Seite 1von 8

A college consists of number of employees working in different departments.

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

insert into emp


1990',40)
insert into emp
1998',30)
insert into emp
1996',30)
insert into emp
1990',30)
insert into emp
1994',40)
insert into emp
1993',40)
insert into emp
1997',40)
insert into emp
1997',20)
insert into emp
1990',40)
insert into emp
1994',20)
insert into emp
1996',20)
insert into emp
16-1998',40)
insert into emp
2000',20)
insert into emp
12-1995',10)

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-

Calculate hra,da,gross and net by using PL/SQL program.


declare
cursor e is select * from emp;
begin

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

DBMS_OUTPUT.PUT_LINE('DO U WANNA MERGE DA WITH BASIC');


IF (CH ='Y') OR (CH ='y') THEN
UPDATE EMP SET I.DA=I.BASIC WHERE EMPNO=I.EMPNO;
END IF;
END IF;
END LOOP;
END;
CREATE OR REPLACE TRIGGER DATRIG BEFORE INSERT OR UPDATE ON EMP
FOR EACH ROW
BEGIN
DAPROCEDURE();
END;

Empno should be unique and has to be generated automatically.


DECLARE
CURSOR C IS SELECT * FROM EMPN;
K NUMBER(4):=1;
BEGIN
FOR I IN C LOOP
UPDATE EMPN SET EMPNO='E00'||K WHERE EMPNO=I.EMPNO;
K:=K+1;
END LOOP;
END;
If the employee is going to retire in a particular month, automatically a
message
has to be generated.
DECLARE
CURSOR C IS SELECT MONTHS_BETWEEN(SYSDATE,DOB)/12 FROM EMP;
BEGIN
FOR I IN C LOOP
IF ROUND(I.D)>60 THEN
DBMS_OUTPUT.PUT_LINE('U ARE GOING TO RETIRE THIS MONTH');
END IF;
END LOOP;
END;
The default value for date-of-birth is 1 jan, 1970.
ALTER TABLE EMP DOB DATE DEFAULT 01-JAN-1970;

When the employees called daily-wagers are to be added the constraint


that
salary should be greater than or equal to 5000 should be dropped.
DECLARE
CURSOR C IS SELECT * FROM EMPN;
BEGIN
FOR I IN C LOOP
IF I.NAME='DW' THEN
EXECUTE IMMEDIATE 'TRUNCATE TABLE EMPN';
EXECUTE IMMEDIATE
'ALTER TABLE EMPN DROP CONSTRAINT EMPN_PK';
END IF;
END LOOP;
END;
Display the information of the employees and departments with description
of
the fields.
DESC DEPT
Name
Null? Type
----------------------------------------- -------- ---------------------------DEPTNO
NOT NULL VARCHAR2(20)
DEPTNAME
VARCHAR2(20)
DESCRIPTION
VARCHAR2(20)
DESC EMP
Name
Null? Type
----------------------------------------- -------- ---------------------------EMPNO
NOT NULL VARCHAR2(20)
EMPNAME
VARCHAR2(20)
BASIC
NOT NULL NUMBER(10,2)
HRA
NUMBER(10,2)
DA
NUMBER(10,2)
DEDUCTIONS
NUMBER(10,2)
GROSS
NUMBER(11,2)
NET
NUMBER(11,2)
DOB
DATE
DEPTNO
VARCHAR2(20)

Display the average salary of all the departments.


SELECT AVG(NET) FROM EMP;
Display the average salary department wise.
SELECT AVG(NET) FROM EMP GROUP BY DEPTNO;
Display the maximum salary of each department and also all departments
put
together.
SELECT MAX(NET) FROM EMP GROUP BY DEPTNO;
SELECT MAX(NET) FROM EMP;
Commit the changes whenever required and rollback if necessary.
COMMIT;

Use substitution variables to insert values repeatedly.


INSERT INTO DEPT VALUES(&DEPTNO,&DNAME,&DESCR);
Assume some of the employees have given wrong information about dateofbirth.
Update the corresponding tables to change the value.
UPDATE EMP SET DOB=01-JAN-1965 WHERE DOB=01-JAN-1960;
Find the employees whose salary is between 5000 and 10000 but not
exactly
7500.
SELECT * FROM EMP WHERE NET BETWEEN 5000 AND 10000 AND NET NOT
IN(7500);
Find the employees whose name contains en.
SELECT * FROM EMP WHERE EMPNAME LIKE %EN%;
Try to delete a particular deptno. What happens if there are employees in it
and
if there are no employees.
DELETE FROM EMP WHERE DEPTNO=20;
IF EMPLOYEES ARE THERE IN THAT DEPARTMENT REFERENCIAL INTEGRITY
VIOLATES AND WE CANNOT DROP IT WITHOUT DROPPING CORRESPONDING
EMPLOYEE ENTRIES.
IF EMPLOYEES ARE NOT THERE THEN THERE IS NO ISSUES. DATA WILL BE
DELETED.


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;