0% found this document useful (0 votes)
17 views

DBMS 8446 PRAC Q4

Uploaded by

soodrushil2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

DBMS 8446 PRAC Q4

Uploaded by

soodrushil2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

DBMS PRACTICAL Q4

RUSHIL SOOD
8446

7.1
create database company;
use company;
create table employee( Fname varchar(15), Minit char(1), Lname varchar(15),Ssn char(9)
primary key, Bdate date, Address varchar(30),
sex char(1), Salary int, Super_ssn char(9), Dno char(2));
create table Department(Dname varchar(15), Dnumber char(2) primary key, Mgr_ssn
char(9), Mgr_start_date date,
foreign key(Mgr_ssn) references employee(Ssn));
create table dept_locations (Dnumber char(2), Dlocation varchar(15), primary key(Dnumber,
Dlocation),
foreign key(Dnumber) references Department(Dnumber));
create table project(Pname varchar(15), Pnumber char(2), Plocation varchar(15), Dnum
char(2), primary key(Pnumber, Plocation),
foreign key (Dnum) references Department(Dnumber));
create table Works_on (Essn char(9), Pno char(2), Hours int, primary key(essn, pno),
foreign key(Essn) references employee(Ssn), foreign key (pno) references project
(Pnumber));
create table dependent( Essn char(9), Dependent_name varchar (20), sex char(1), Bdate
date, Relationship varchar(10),
primary key(essn, dependent_name), foreign key(essn) references employee(ssn));
insert into employee values
('John', 'B', 'Smith', '123456789', '1965-01-09', '731 Fondren, Houston, TX', 'M', 30000,
'333445555', '5'),
('Franklin', 'T', 'Wong', '333445555', '1955-12-08', '638 Voss, Houston, TX', 'M', 40000, NULL,
'5'),
('Alicia', 'J', 'Zelaya', '999887777', '1968-01-19', '3321 Castle, Spring, TX', 'F', 25000,
'987654321', '4'),
('Jennifer', 'S', 'Wallace', '987654321', '1941-06-20', '291 Berry, Bellaire, TX', 'F', 43000,
'888665555', '4'),
('Ramesh', 'K', 'Narayan', '666884444', '1962-09-15', '975 Fire Oak, Humble, TX', 'M', 38000,
'333445555', '5'),
('Joyce', 'A', 'English', '453453453', '1972-07-31', '5631 Rice, Houston, TX', 'F',
25000,'333445555', '5'),
('Ahmad', 'V', 'Jabbar', '987987987', '1969-03-29', '980 Dallas, Houston, TX', 'M', 25000,
'987654321', '4'),
('James', 'E', 'Borg', '888665555', '1937-11-10', '450 Stone, Houston, TX', 'M', 55000, NULL,
'1');
insert into Department values ('Research', '5', '333445555', '1988-05-22'),
('Administration', '4', '987654321', '1995-01-01'),
('Headquarters', '1', '333445555', '1981-06-19');
insert into dept_locations values ('1', 'Houston'), ('4', 'Stafford'),('5', 'Houston'),('5','Bellaire'),
('5','Sugarland');
insert into project values ('ProductX', '1', 'Bellaire', '5'),('ProductY', '2', 'Sugarland', '5'),
('ProductZ', '3', 'Houston', '5'),('Computerization', '10', 'Stafford', '4'),
('Reorganization', '20', 'Houston', '1'),('Newbenefits', '30', 'Stafford', '4');
insert into Works_on values ('123456789', '1', 32.5),('123456789', '2', 7.5),('666884444', '3',
40.0),('453453453', '1', 20.0),
('453453453', '2', 20.0),('333445555', '2', 10.0),('333445555', '3', 10.0),('333445555', '10',
10.0),
('333445555', '20', 10.0),('999887777', '30', 30.0),('999887777', '10', 10.0),('987987987',
'10', 35.0),
('987987987', '30', 5.0),('987654321', '30', 20.0),('987654321', '20', 15.0),('888665555', '20',
NULL);
insert into dependent values ('333445555', 'Joy', 'F', '1958-05-03', 'Spouse'),
('333445555', 'Alice', 'F', '1986-04-05','Daughter'),('333445555', 'Franklin', 'M', '1983-10-25',
'Son'),
('987654321', 'Abner', 'M', '1942-02-28', 'Spouse'),('123456789', 'Michael', 'M', '1988-01-04',
'Son'),
('123456789', 'Alice', 'F', '1988-12-30', 'Daughter'),('123456789', 'Elizabeth', 'F', '1967-05-05',
'Spouse');
alter table employee add foreign key(Dno) references Department(Dnumber);
select * from employee;
select * from department;
select * from dept_locations;
select * from project;
select * from works_on;
select * from dependent;

select fname, lname from employee where super_ssn is null;


(select distinct P.pnumber from project P join Department D on P.Dnum=D.Dnumber join
Employee E on E.Ssn=D.Mgr_Ssn where E.Lname="Smith")
union
(select distinct W.Pno from Works_On W join Employee E on W.Essn=E.Ssn where
E.Lname="Smith");
select distinct essn from works_on where (pno, hours) in
(select pno, hours from works_on where essn = '123456789');
select lname, fname from employee where salary > all
(select salary from employee where dno = 5);
select E.fname, e.Lname from Employee E join Dependent D on D.essn=E.Ssn
where D.Dependent_Name=E.Fname and D.sex=E.sex;
select Fname, Lname from Employee where ssn not in (select Essn from Dependent);
select distinct E.fname, E.lname from employee E join dependent D on E.ssn = D.essn
join department F on E.ssn = F.mgr_ssn;
select E.Fname, E.Lname from employee E join Works_on W on E.Ssn = W.Essn join project
P on W.Pno = P.Pnumber
where P.Dnum = 5 group by E.Ssn, E.Fname, E.Lname having count(distinct P.Pnumber) =
(select count(*) from project where Dnum = 5);
select distinct essn from works_on where pno in (1, 2, 3);
select E.lname as Employee_Name, S.Lname as Supervisor_Name from employee as E,
employee as S where e.super_ssn = s.ssn;
select Fname, Lname, Address from employee E join department D on E.dno = D.dnumber
where dname = 'research';
select Pnumber, Dnum, Lname, Address, Bdate from ((project join department on Dnum =
Dnumber) join employee on Mgr_ssn = Ssn)
where Plocation = "Stafford";
select Sum(Salary) as Sum, Max(Salary) as max, Min(Salary) as min, avg(salary) as Average
from Employee;
select Sum(E.Salary) as Sum, Max(E.Salary) as max, Min(E.Salary) as min, avg(E.salary) as
Average from Employee E join Department D
on D.Dnumber=E.Dno where D.Dname='Research';
select count(*) as Total_Employees from employee;
select count(E.Ssn) as Research_Emp from employee E join department D on
E.Dno=D.Dnumber where D.Dname='Research';
select count(distinct salary) as 'Distinct salary'from employee;
select E.Fname, count(D.Dependent_Name) as No_dependents from Dependent D join
Employee E on D.Essn=E.Ssn group by E.ssn having count(D.Dependent_Name)>=2;
select Dno, count(*) as Employees, avg(Salary) as Avg_Salary from Employee group by Dno;
select Pnumber, Pname, count(*) as employees from Project, Works_on where Pnumber =
Pno group by Pnumber, Pname;
select Pnumber, Pname, count(*) as Employees from Project, Works_on where Pnumber =
Pno group by Pnumber, Pname having count(*) > 2;
select Pnumber, Pname, count(*) as Employees from Project, Works_on, Employee where
Pnumber = Pno and Ssn = Essn and Dno = 5 group by Pnumber, Pname;
select Dno, count(*) from Employee where Salary > 40000 and Dno in (select Dno from
Employee group by Dno having count(*) > 5) group by Dno;
OUTPUTS

TABLES
Employee

Department

Dept_locations
Project

Works_On
Dependent

QUERIES
18.

4.
16.

6.

7.

3.

17.
8.

1.

2.

19.
20.

21.

22.

23. .

5.

24.
.

25.

26.

27.

28.

7.3
create view Works_on1 as select Fname, Lname, Pname, Hours

from Employee, Project, Works_on where Ssn = Essn and Pno = Pnumber;
select * from Works_On1;

create view Dept_info(Dept_name, No_of_emps, Total_sal) as select Dname, count(*), sum(Salary)

from Department, Employee where Dnumber = Dno group by Dname;

select * from Dept_info;

select Fname, Lname from Works_on1 where Pname = 'ProductX';

select Fname, Lname from Employee, Project, Works_on where Ssn = Essn and Pno = Pnumber and
Pname = 'ProductX';

update Works_on1 set Pname = 'ProductY' where Lname = 'Smith' and Fname = 'John' and Pname =
'ProductX';

select * from Works_On1;

update Works_on set Pno = (select Pnumber from Project where Pname = 'ProductY')

where Essn in (select Ssn from Employee where Lname = 'Smith' and Fname = 'John')

and Pno = (select Pnumber from Project where Pname = 'ProductX');

select * from Works_On1;

drop view Works_on1;

update Project set Pname = 'ProductY' where Pname = 'ProductX';

select * from project;

update Dept_info set Total_sal = 100000 where Dname = 'Research';

create view Dept5emp as select * from Employee where Dno = 5;

select * from Dept5emp;

create view Basic_emp_data as select Fname, Lname, Address from Employee;

select * from Basic_emp_data;

drop table Dependent cascade;

OUTPUTS
VIEWS
Works_On1

Dept_Info

Dept5emp
Basic_emp_Data

QUERIES
V1. Retrieve the last name and first name of all employees who work on the ‘ProductX’ project,

UV1. Update the PNAME attribute of ‘John Smith’ from ‘ProductX’ to ‘ProductY’
Update Project and set Project to Project Y where Project is Project X

7.4
alter table Company.Employee add column Job varchar(12);

alter table Company.Employee drop column Address cascade;

alter table Company.Department alter column Mgr_ssn

drop default;

alter table Company.Department alter column Mgr_ssn set default '333445555';

desc Employee;

desc Department

OUTPUTS

QUERIES
Alter table Employee to add column Job.

Alter table Employee to drop column Address.

Alter table Department to set Mgr_ssn default to ‘333445555’.

You might also like