DBMS Record Lab Manual
DBMS Record Lab Manual
Section A
1. Perform the following:
QUERY:
show databases;
create database person;
use person;
create table employees(ID int primary key, Name varchar(50) NOT NULL,Age int,salary
float);
insert into employees values(1,'John Doe',30,50000);
insert into employees values(2,'Steve Smith',35,50000);
insert into employees values(3,'John Bosco',32,50000);
select * from employees;
delete from employees where id=1;
commit;
rollback;
select * from employees;
2. Perform the following: a. Altering a Table, Dropping/Truncating/Renaming Tables, Backing up /
Restoring a Database.
QUERY:
show databases;
use person;
show tables;
select * from employees;
alter table employees add address varchar(50);
truncate table employees;
rename table employees to company_workers;
select * from company_workers;
drop table company_workers;
3. For a given set of relation schemes, create tables and perform the following Simple Queries,
Simple Queries with Aggregate functions, Queries with Aggregate functions (group by and having
clause)
Query:
use person;
show tables;
create table EMPLOYEE(EMPNO VARCHAR(10) PRIMARY KEY,EMP_NAME
VARCHAR(20),DEPT VARCHAR(30),SALARAY INT, DOJ VARCHAR(20),BRANCH VARCHAR(30));
select * from EMPLOYEE;
insert into EMPLOYEE values("E101","AMIT","PRODUCTION",45000,"12-MAR-
00",'BANGLORE');
insert into EMPLOYEE values("E102","AMIT","HR",70000,"03-JUL-02",'BANGLORE');
insert into EMPLOYEE values("E103","SUNITA","MANAGER",120000,"11-JAN-01",'MYSORE');
insert into EMPLOYEE values("E105","SUNITA","IT",67000,"01-AUG-01",'MYSORE');
insert into EMPLOYEE values("E106","MAHESH","CIVIL",145000,"12-SEP-03",'MUMBAI');
select * from EMPLOYEE;
select EMPNO,SALARAY from EMPLOYEE;
SELECT COUNT(*) FROM EMPLOYEE;
SELECT COUNT(DISTINCT EMP_NAME) FROM EMPLOYEE;
SELECT EMP_NAME,SUM(SALARAY),COUNT(*) FROM EMPLOYEE GROUP BY (EMP_NAME);
SELECT EMP_NAME,SUM(SALARAY) FROM EMPLOYEE GROUP BY (EMP_NAME) HAVING
SUM(SALARAY)>120000;
SELECT EMP_NAME FROM EMPLOYEE ORDER BY EMP_NAME desc;
select * from EMPLOYEE WHERE EMP_NAME="AMIT" AND SALARAY>50000;
4. Execute the fallowing queries
a. How the resulting salaries if every employee working on the ‘Research’ Departments is given a
10% raise.
b. Find the sum of the salaries of all employees of the ‘Accounts’ department, as well as the
maximum salary, the minimum salary, and the average salary in this department.
QUERY:
USE PERSON;
CREATE TABLE DEPT_TABLE(DNAME VARCHAR(100) NOT NULL, DNUMBER INT PRIMARY
KEY,MGRSSN VARCHAR(100) NOT NULL,MGRSTARTDATE DATE NOT NULL);
SELECT * FROM DEPT_TABLE;
INSERT INTO DEPT_TABLE VALUES('RESEARCH',5,'333445555','1966-05-22'),
('ADMINISTRATION',4,'987654321','1995-01-01'),('HEADQUATERS',1,'888665555','1961-06-
19');
SELECT * FROM DEPT_TABLE;
CREATE TABLE EMPLOYEE_TABLE(FNAME VARCHAR(100) NOT NULL,MNIT VARCHAR(100)
NOT NULL,LNAME VARCHAR(100) NOT NULL, SSN VARCHAR(100) PRIMARY KEY, BDATE
DATE NOT NULL,
ADDRESS VARCHAR(224) NOT NULL,SEX VARCHAR(10) NOT NULL,SALARY INT NOT NULL,
SUPERSSN VARCHAR(100), DNO INT NOT NULL,FOREIGN KEY(DNO) REFERENCES
DEPT_TABLE(DNUMBER));
SELECT * FROM EMPLOYEE_TABLE;
INSERT INTO EMPLOYEE_TABLE VALUE('James', 'E', 'Borg', '888665555', '1937-11-10', '450
StoneHouston,TX', 'M', 55000,987654321, 1);
INSERT INTO EMPLOYEE_TABLE VALUES ('John', 'B', 'Smith', '123456789', '1965-01-09',
'731Fondren, Houston, TX', 'M', 30000, 333445555, 5),
('Franklin', 'T','Wong', '333445555', '1955-12-08', '638 Voss,Houston, TX,' ,'M', '40000',
'888665555', 5),
('Alicia', 'J', 'Zeleva', '999887777', '1968-01-19', '3321 Castle, Spring,TX', 'F', 25000,
987654321, 4),
('Jennifer', 'S', 'Walace', '987654321', '1941-06-20', '291Berry.BellaireTX', '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', '987987967', '1969-03-29', '980 Dalas,Houston, TX', 'M', 25000,
987654321, 4);
SELECT * FROM EMPLOYEE_TABLE;
UPDATE EMPLOYEE_TABLE SET SALARY=SALARY+(SALARY*(10/100))WHERE
SUPERSSN='333445555';
select sum(SALARY),min(SALARY),max(SALARY) from EMPLOYEE_TABLE e where
e.DNO=DEPT_TABLE.DNUMBER;
QUERY:
use person;
create table Departments(DepartmentID int primary key, Depatrmentname varchar(30));
insert into Departments values(1,'DEPATMENTS A');
insert into Departments values(2,'DEPATMENTS B');
insert into Departments values(3,'DEPATMENTS C');
insert into Departments values(4,'DEPATMENTS D');
insert into Departments values(5,'DEPATMENTS E');
create table Employees(EmployeeID int primary key,Name varchar(50),DepartmentID
int,Foreign key (DepartmentID)References Departments(DepartmentID));
insert into Employees values(1,'John Smith',1);
insert into Employees values(2,'Jane Doe',2);
insert into Employees values(3,'Mike Johnson',3);
insert into Employees values(4,'Sarah Williams',4);
insert into Employees values(5,'Chris Lee',5);
select Name from Employees where exists(select*from Departments where
Departments.DepartmentID=Employees.DepartmentID and Departments.DepartmentID=5);
select Departments.Depatrmentname,count(Employees.DepartmentID)as
NumberOfEmployees
from Departments
join Employees on Departments.DepartmentID=Employees.DepartmentID
group by Departments.Depatrmentname having count(Employees.DepartmentID)>=2;
6. Execute the fallowing queries
a. For each project, retrieve the project number, the project name, and the number of employee
who work on that project.(use GROUP BY)
QUERY:
use person;
create table projects(ProjectID int primary key,ProjectNumber int,ProjectName
varchar(50));
insert into Projects values(1,1001,'Project A');
insert into Projects values(2,1002,'Project B');
insert into Projects values(3,1003,'Project C');
create table Employees1(EmployeeID int primary key,Name varchar(50),BirthDate date);
insert into Employees1 values(1,'John Smith','1990-05-10');
insert into Employees1 values(2,'Jane Doe','1985-08-15');
insert into Employees1 values(3,'Mike Johnson','1992-02-20');
insert into Employees1 values(4,'Sarah Willioms','1991-11-30');
insert into Employees1 values(5,'Chris lee','1994-06-25');
create table EmployeeProjects(EmployeeID int ,ProjectID int,Foreign Key(EmployeeID)
REFERENCES Employees(EmployeeID),Foreign Key (ProjectID) REFERENCES
Projects(ProjectID));
insert into EmployeeProjects values(1,1),(1,2),(2,2),(3,3),(4,3),(5,1),(5,3);
select Name from Employees1 where YEAR(BirthDate) between 1990 and 1999;
7. For each Department that has more than five employees, retrieve the department
number and number of employees who are making salary more than 40000
QUERY:-
create database salary;
use salary;
-- Create the department table
CREATE TABLE Department (
DeptNumber INT PRIMARY KEY,
DeptName VARCHAR(50)
);
-- Retrieve departments with more than five employees making a salary more than 40000
SELECT d.DeptNumber,COUNT(e.EmpNumber) AS NumEmployeesAbove40k FROM
Department d INNER JOIN Employee e ON d.DeptNumber = e.DeptNumber
WHERE e.Salary > 40000 GROUP BY d.DeptNumber
HAVING COUNT(e.EmpNumber) > 0;
8. For each project on which more than two employees work, retrieve the project number, project
name and the number of employees who work on that project.
QUERY:
use salary;
-- Create the project table
CREATE TABLE Project1 (ProjectNumber INT PRIMARY KEY,ProjectName VARCHAR(50));
-- Retrieve projects with more than two employees working on the project
SELECT p.ProjectNumber,p.ProjectName,COUNT(pe.EmpNumber) AS
NumEmployeesOnProject FROM
Project1 p INNER JOIN ProjectEmployee pe ON p.ProjectNumber = pe.ProjectNumber
GROUP BY p.ProjectNumber, p.ProjectName HAVING COUNT(pe.EmpNumber) > 2;
9. For a given set of relation tables perform the following: Creating Views (with and without check
option), Dropping views, Selecting from a view.
QUERY:
use salary;
-- Create the Students table
CREATE TABLE Students (StudentID INT PRIMARY KEY,FirstName VARCHAR(50),LastName
VARCHAR(50),Age INT);
-- Create a view with check option to restrict age to be greater than or equal to 18
CREATE VIEW Adults AS SELECT StudentID, FirstName, LastName, Age
FROM Students WHERE Age >= 18 WITH CHECK OPTION;
a. Viewing all databases, Creating a Database, Viewing all Tables in a Database, Creating Tables
(With and Without Constraints), Inserting/Updating/Deleting Records in a Table, Saving (Commit)
and Undoing (rollback) Execute the following Queries:
show databases;
create database library;
use library;
a. List the details of Students who are all studying in 2nd sem BCA.
-- a.List the details of Students who are all studying in 2nd sem bca.
select * from student s,branch b where s.branchid=b.branchid and s.sem=2 and
Branchname='bca';
--b
select a.authorid,a.authorname,count(distinct b.bookid) as cbookid from
author a,book b where a.authorid=b.authorid group by a.authorid;
4.
a. Display the student details who borrowed more than two books.
b.Display the student details who borrowed books of more than one Author.
-- a.Display the student details who borrowed more than two books.
select s.usn,s.name from student s,borrow b where s.usn=b.usn group by
s.usn having count(distinct b.bookid)>2;
--b
select s.usn,s.name,count(distinct bk.authorid) from student s,book bk,
borrow br where s.usn=br.usn and br.bookid=bk.bookid group by s.usn having
count(distinct bk.authorid)>1;
5.
b. List the details of students who borrowed the books which are all published by the same
publisher.
a)
select s.usn,s.name,count(distinct bk.authorid) from student
s,book bk,borrow br where s.usn=br.usn and br.bookid=bk.bookid group by s.usn;
b)
select s.usn,s.name,count(bk.publisher) from student s,book bk,borrow
br where s.usn=br.usn and br.bookid=bk.bookid group by s.usn;
2. Consider the following schema: STUDENT (USN, name, date_of_birth, branch, mark1, mark2,
mark3, total, GPA)
b. Find the students who born on a particular year of birth from the date_of_birth column.
3)