You are on page 1of 3

DATABASE DESIGN AND MANAGEMENT LAB

2. Create the following tables with given attributes having appropriate data type and specify the
necessary primary and foreign key constraints:
Employee (EmpId, Empname, Sal, Deptno)
Dept (Deptno, Dname, Loc,DeptmanagerId)

Create Table
create table Dept (Deptno varchar(10), Dname varchar(25), Loc varchar(25), DeptmanagerId integer,
constraint pk_dept primary key (deptno) );
create table Employee (EmpId integer, Empname varchar(25), Sal Float(10), Deptno Varchar(10),
constraint pk_emp primary key(empId), constraint fk_deptno FOREIGN KEY (Deptno)
REFERENCES Dept(Deptno));

Inserting Values
INSERT INTO Dept (`Deptno`, `Dname`, `Loc`, `DeptmanagerId`) VALUES ('AI001', 'AI',
"Coimbatore", '11001');
INSERT INTO Dept(`Deptno`, `Dname`, `Loc`, `DeptmanagerId`) VALUES ('CS001', 'CSE',
'Chennai', '12001');

INSERT INTO Employee (`EmpId`, `Empname`, `Sal`, `Deptno`) VALUES ( '1001', 'Bhuvaneshwar',
'12000', 'AI001');
INSERT INTO Employee (`EmpId`, `Empname`, `Sal`, `Deptno`) VALUES ('1002', 'Nithish',
'10000', 'AI001');
INSERT INTO Employee (`EmpId`, `Empname`, `Sal`, `Deptno`) VALUES ('1003', 'Ravi', '5000',
'CS001');
INSERT INTO Employee (`EmpId`, `Empname`, `Sal`, `Deptno`) VALUES ('1004', 'Priya', '15000',
'CS001');

Dept Table Employee Table


Queries
a) List the count of Employees and average salary of each department.

SELECT Deptno, COUNT(EmpId) AS 'Number of Employees', AVG(Sal) AS 'Average


Salary' FROM Employee GROUP BY Deptno;

b) List the employee name, department name and the salary of all the employees.

SELECT Employee.Empname as 'Employee_name', Employee.Sal, Dept.Dname FROM


Employee JOIN Dept ON Employee.Deptno = Dept.Deptno;

c) Display the Employee name and the respective department manager name.

SELECT Employee.Empname, Dept.DeptmanagerId FROM Employee JOIN Dept;


WARNING : Not Having Manager Name So I used ManagerID

d) Create a function to return the salary of the employee when Empid is given as input
parameter. Handle Exceptions.

create or replace function salary (EmpId NUMBER);


temp varchar(20);
BEGIN
SELECT sal INTO temp , sal from Employee WHERE EmpId = EmpId;
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('ERROR');
dbms_output.put_line('there is no empid');
End;
/

Output Not Come for This

You might also like