DBMS 8446 PRAC Q4
DBMS 8446 PRAC 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;
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;
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';
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')
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);
drop default;
desc Employee;
desc Department
OUTPUTS
QUERIES
Alter table Employee to add column Job.