0% found this document useful (0 votes)
52 views5 pages

Database Management System Lab (CSE304L) Exercise 5: Anu Likitha Immadisetty AP21110010963

The document details the creation of tables and views for an employee database. It includes the creation of tables for departments, employees, dependents, project locations, and project work. It then creates views to query data from these tables, including department employee salaries, project hours by employee, and an updatable department table. It also creates a materialized view to calculate average salaries. Finally, it demonstrates an insert transaction to add a new department and employee.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views5 pages

Database Management System Lab (CSE304L) Exercise 5: Anu Likitha Immadisetty AP21110010963

The document details the creation of tables and views for an employee database. It includes the creation of tables for departments, employees, dependents, project locations, and project work. It then creates views to query data from these tables, including department employee salaries, project hours by employee, and an updatable department table. It also creates a materialized view to calculate average salaries. Finally, it demonstrates an insert transaction to add a new department and employee.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Database Management System Lab (CSE304L)

Exercise 5

Anu Likitha Immadisetty


AP21110010963

-- Create all the tables given in following schema.

CREATE TABLE DEPARTMENT (


Dname varchar(25) not null,
Dnumber int not null,
Mgr_ssn char(9) not null,
Mgr_start_date date,
primary key (dnumber),
UNIQUE (dname)
);

CREATE TABLE EMPLOYEE (


Fname varchar(15) not null,
Minit varchar(1),
Lname varchar(15) not null,
Ssn char(9),
Bdate date,
Address varchar(50),
Sex char,
Salary decimal(10,2),
Super_ssn char(9),
Dno int,
primary key (ssn),
foreign key (dno) references DEPARTMENT(dnumber)
);
CREATE TABLE DEPENDENT (
Essn char(9),
Dependent_name varchar(15),
Sex char,
Bdate date,
Relationship varchar(8),
primary key (essn,dependent_name),
foreign key (essn) references EMPLOYEE(ssn)
);

CREATE TABLE DEPT_LOCATIONS (


Dnumber int,
Dlocation varchar(15),
primary key (dnumber,dlocation),
foreign key (dnumber) references DEPARTMENT(dnumber)
);

CREATE TABLE PROJECT (


Pname varchar(25) not null,
Pnumber int,
Plocation varchar(15),
Dnum int not null,
primary key (pnumber),
unique (pname),
foreign key (dnum) references DEPARTMENT(dnumber)
);
CREATE TABLE WORKS_ON (
Essn char(9),
Pno int,
Hours decimal(4,1),
primary key (essn,pno),
foreign key (essn) references EMPLOYEE(ssn),
foreign key (pno) references PROJECT(pnumber)
);

A)
i)
CREATE VIEW DepartmentEmployeeSalary AS
SELECT D.Dname, E.Fname, E.Salary
FROM Department D
JOIN Employee E ON D.Dnumber = E.Dno;

ii)
CREATE VIEW ProjectEmployeeHours AS
SELECT P.Pname, D.Dname, COUNT(W.Essn) AS NumEmployees, SUM(W.Hours) AS TotalHoursPerWeek
FROM Project P
JOIN Department D ON P.Dnum = D.Dnumber
JOIN Works_on W ON P.Pnumber = W.Pno
GROUP BY P.Pname, D.Dname
HAVING COUNT(W.Essn) > 1;

iii)
CREATE VIEW UpdatableDept AS SELECT * FROM DEPARTMENT;
SELECT * FROM UpdatableDept;
B)
-- Create a materialized view for calculating average salaries
CREATE VIEW SalarySummary AS
SELECT
'Employees' AS Category,
AVG(Salary) AS AverageSalary
FROM Employee
UNION ALL
SELECT
'Managers' AS Category,
AVG(Salary) AS AverageSalary
FROM Employee
WHERE Ssn IN (SELECT DISTINCT Mgr_ssn FROM Department)
UNION ALL
SELECT
D.Dname AS Category,
AVG(E.Salary) AS AverageSalary
FROM Department D
JOIN Employee E ON D.Dnumber = E.Dno
GROUP BY D.Dname
UNION ALL
SELECT
'Top Department(s)' AS Category,
D.Dname
FROM (
SELECT
D.Dname,
SUM(E.Salary) AS TotalSalary
FROM Department D
JOIN Employee E ON D.Dnumber = E.Dno
GROUP BY D.Dname
ORDER BY TotalSalary DESC
LIMIT 1
) AS D;
C)
START TRANSACTION;
INSERT INTO DEPARTMENT (Dnumber, Dname, Mgr_ssn, Mgr_start_date)
VALUES (5, 'Konda Department', '123456799', '2022-01-16');
INSERT INTO EMPLOYEE (Fname, Lname, Ssn, Bdate, Address, Sex, Salary, Super_ssn, Dno)
VALUES ('Boar', 'Head', '122456799', '1990-01-01', '123 Main St', 'M', 60000.00, '123456799', 5);
COMMIT;

You might also like