100% found this document useful (1 vote)
177 views23 pages

DBMS Record Lab Manual

DBMS LAB MANUAL NEP syallabus part A and part B

Uploaded by

chiraggowd345
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
100% found this document useful (1 vote)
177 views23 pages

DBMS Record Lab Manual

DBMS LAB MANUAL NEP syallabus part A and part B

Uploaded by

chiraggowd345
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 23

DBMS PROGRAMS

Section A
1. Perform the following:

Tables (With and Without Constraints), Inserting/Updating/Deleting Records in a Table, Saving


(Commit) and Undoing (rollback)

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;

(Write Tables_in_person instead of Tables_in_lab)

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;

(The values are NULL)


5. Execute the fallowing queries
a. Retrieve the name of each employee Controlled by Department number 5 (use EXISTS
operator). b. Retrieve the name of each dept and number of employees working in each
Department which has at least 2 employees

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)

b. Retrieve the name of employees who born in the year 1990’s

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)
);

-- Create the employee table


CREATE TABLE Employee (EmpNumber INT PRIMARY KEY,EmpName VARCHAR(50),Salary
DECIMAL(10, 2),DeptNumber INT);

-- Insert sample data into the Department table


INSERT INTO Department (DeptNumber, DeptName)VALUES
(1, 'HR'),(2, 'Finance'),(3, 'IT'),
(4, 'Sales'),(5, 'Marketing');
-- Insert sample data into the Employee table
INSERT INTO Employee (EmpNumber, EmpName, Salary, DeptNumber)VALUES
(101, 'John Doe', 45000, 1),
(102, 'Jane Smith', 48000, 1),
(103, 'Michael Johnson', 52000, 2),
(104, 'Emily Brown', 39000, 2),
(105, 'David Lee', 42000, 3),
(106, 'Sarah White', 41000, 3),
(107, 'Kevin Davis', 38000, 3),
(108, 'Michelle Miller', 44000, 4),
(109, 'Robert Wilson', 46000, 4),
(110, 'Lisa Taylor', 43000, 4),
(111, 'Christopher Anderson', 55000, 5),
(112, 'Amanda Martin', 48000, 5);

-- 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));

-- Create the employee table


CREATE TABLE Employee1 (EmpNumber INT PRIMARY KEY,EmpName VARCHAR(50));

-- Create the project-employee mapping table


CREATE TABLE ProjectEmployee (ProjectNumber INT,EmpNumber INT);

-- Insert sample data into the Project table


INSERT INTO Project1 (ProjectNumber, ProjectName)VALUES
(1, 'Project A'),(2, 'Project B'),
(3, 'Project C'),(4, 'Project D');

-- Insert sample data into the Employee table


INSERT INTO Employee1 (EmpNumber, EmpName)VALUES
(101, 'John Doe'),
(102, 'Jane Smith'),
(103, 'Michael Johnson'),
(104, 'Emily Brown'),
(105, 'David Lee'),
(106, 'Sarah White'),
(107, 'Kevin Davis'),
(108, 'Michelle Miller');

-- Insert sample data into the ProjectEmployee table


INSERT INTO ProjectEmployee (ProjectNumber, EmpNumber)VALUES
(1, 101),(1, 102),(1, 103),(2, 103),(2, 104),(2, 105),
(3, 105),(3, 106),(4, 107),(4, 108),(4, 101);

-- 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);

-- Insert sample data into the Students table


INSERT INTO Students (StudentID, FirstName, LastName, Age)
VALUES
(1, 'John', 'Doe', 20),(2, 'Jane', 'Smith', 21),
(3, 'Michael', 'Johnson', 19),(4, 'Emily', 'Brown', 22);

-- Create the Courses table


CREATE TABLE Courses (CourseID INT PRIMARY KEY,CourseName VARCHAR(50),Instructor
VARCHAR(50));
-- Insert sample data into the Courses table
INSERT INTO Courses (CourseID, CourseName, Instructor)VALUES
(101, 'Math', 'Prof. Anderson'),
(102, 'Science', 'Prof. Johnson'),
(103, 'History', 'Prof. Smith');

-- Create a view that shows student names and ages


CREATE VIEW StudentNamesAndAges AS SELECT FirstName, LastName, Age
FROM Students;

-- Create a view that shows course names and instructors


CREATE VIEW CourseNamesAndInstructors AS SELECT CourseName, Instructor
FROM Courses;

-- 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;

-- Drop the view StudentNamesAndAges


DROP VIEW IF EXISTS StudentNamesAndAges;

-- Select from the view CourseNamesAndInstructors


SELECT * FROM CourseNamesAndInstructors;

-- Select from the view Adults


SELECT * FROM Adults;
Section B
Create the following tables with properly specifying Primary keys, Foreign keys and solve the
following queries.

BRANCH (Branchid, Branchname, HOD)

STUDENT (USN, Name, Address, Branchid, sem)

BOOK (Bookid, Bookname, Authorid, Publisher, Branchid)

AUTHOR (Authorid, Authorname, Country, age)

BORROW (USN, Bookid, Borrowed_Date)

1. Perform the following:

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;

create table branch(Branchid int, Branchname varchar(20), HOD varchar(30), primary


key(Branchid));
INSERT INTO branch (Branchid ,Branchname,hod )values(1,'BCA','profGoumathi');
INSERT INTO branch (Branchid ,Branchname,hod )VALUES(2,'BBA','prof.sandeep');
INSERT INTO branch (Branchid ,Branchname,hod ) values(3,'CSE','Prof. Mahesh');
INSERT INTO branch (Branchid ,Branchname,hod ) values(4,'ISE','Prof. Asha');
INSERT INTO branch (Branchid ,Branchname,hod ) values(5,'EEE','Prof. Elumalai');
select * from branch;

create table student(


usn varchar(20),
name varchar(20),
address varchar(20),
branchid int,
sem varchar(10),
PRIMARY KEY (usn),
FOREIGN KEY (branchid) REFERENCES branch(branchid));
insert into student values('1CR01','harish','bangalore',1,'2');
insert into student values('1CR02','bharat','mysore',2,'3');
INSERT INTO student values('1CR03','kiran','delhi',3,'6');
INSERT INTO student values('1CR04','mahi','chennai',4,'7');
INSERT INTO student values('1CR05','krishna','hubli',5,'4');
select * from student;

create table book(


bookid int,
bookname varchar(20),
authorid int,
publisher varchar(20),
branchid int,
PRIMARY KEY (bookid), FOREIGN KEY (branchid) REFERENCES branch(branchid));
INSERT INTO book values(1111,'cprog',123,'pearson',1);
INSERT INTO book values(2222,'dbms',124,'mcgrawhill',2);
INSERT INTO book values(3333,'oops',125,'sapna',3);
INSERT INTO book values(4444,'unix',126,'subhash',4);
INSERT INTO book values(5555,'c prog',127,'person',5);
select * from book;

create table author(


authorid int,
authorname varchar(20),
country varchar(20),
age int,PRIMARY KEY (authorid));
INSERT INTO author values(123,'Navathe','India',55);
INSERT INTO author values(124,'Ritche','UK',44);
INSERT INTO author values(125,'Ramakrishna','India',55);
INSERT INTO author values(126,'Sumitabha','India',38);
INSERT INTO author values(127,'Dennis','USA',66);
select * from author;

create table borrow(


usn varchar(20),
bookid int ,
bdate varchar(10),
FOREIGN KEY (usn) REFERENCES student(usn));
INSERT INTO borrow(usn,bookid,bdate) values('1CR01',2222,'10-Jan-00');
INSERT INTO borrow(usn,bookid,bdate) values('1CR02',3333,'5-Mar-16');
INSERT INTO borrow(usn,bookid,bdate) values('1CR03',5555,'1-Jun-10');
INSERT INTO borrow(usn,bookid,bdate) values('1CR04',2222,'19-May-00');
INSERT INTO borrow(usn,bookid,bdate) values('1CR05',1111,'22-Feb-15');
select * from borrow;
2.

a. List the details of Students who are all studying in 2nd sem BCA.

b. List the students who are not borrowed any books.

-- 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.List the students who are not borrowed any books.


select * from student s where s.usn not in(select b.usn from borrow b);
3.

a. Display the USN, Student name, Branch_name, Book_name, Author_name, Books_Borrowed_


Date of 2nd sem BCA Students who borrowed books.

b. Display the number of books written by each Author.

-- a.Display the USN, Student name, Branch_name, Book_name, Author_name,


Books_Borrowed_Date of 2nd sem MCA Students who borrowed books.
select s.usn,s.name,b.branchname,bk.bookname,br.bdate,a.authorname
from student s,branch b,book bk,author a,borrow br where s.branchid=b.branchid
and s.branchid=bk.branchid and a.authorid=bk.authorid and br.usn=s.usn and
bk.bookid=br.bookid and s.sem=2 and b.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.

a. Display the Book names in descending order of their names.

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)

1) Perform the following: a. Creating Tables (With and Without Constraints),


Inserting/Updating/Deleting Records in a Table, Saving (Commit) and Undoing (rollback)

create database details;


use details;
CREATE TABLE STUDENT1 (
USN VARCHAR(20) PRIMARY KEY,
name VARCHAR(100) NOT NULL,
date_of_birth DATE NOT NULL,
branch VARCHAR(50) NOT NULL,
mark1 INT NOT NULL,
mark2 INT NOT NULL,
mark3 INT NOT NULL,
total INT,
GPA FLOAT
);

-- Inserting data for John Smith


INSERT INTO STUDENT1 (USN, name, date_of_birth, branch, mark1, mark2, mark3, total,
GPA)
VALUES ('2023001', 'John Smith', '2003-03-15', 'Computer Science', 90, 85, 78, (90 + 85 +
78), ((90 + 85 + 78) / 3));

-- Inserting data for Jane Doe


INSERT INTO STUDENT1 (USN, name, date_of_birth, branch, mark1, mark2, mark3, total,
GPA)
VALUES ('2023002', 'Jane Doe', '2003-07-20', 'Electrical Engineering', 88, 92, 79, (88 + 92 +
79), ((88 + 92 + 79) / 3));
-- Inserting data for Michael Johnson
INSERT INTO STUDENT1 (USN, name, date_of_birth, branch, mark1, mark2, mark3, total,
GPA)
VALUES ('2023003', 'Michael Johnson', '2003-01-10', 'Mechanical Engineering', 78, 85, 90,
(78 + 85 + 90), ((78 + 85 + 90) / 3));

-- Inserting data for Prakash


INSERT INTO STUDENT1 (USN, name, date_of_birth, branch, mark1, mark2, mark3, total,
GPA)
VALUES ('20230012', 'Prakash', '1996-07-20', 'Electrical Engineering', 88, 92, 79, (88 + 92 +
79), ((88 + 92 + 79) / 3));
2) Execute the following queries:

a. Find the GPA score of all the students.

b. Find the students who born on a particular year of birth from the date_of_birth column.

a) select * from student1;

b) SELECT * FROM STUDENT1 WHERE EXTRACT(YEAR FROM date_of_birth) = 2003;

3)

a. List the students who are studying in a particular branch of study.

b. Find the maximum GPA score of the student branch-wise.

a) SELECT * FROM STUDENT1 WHERE branch = 'Computer Science';


b) SELECT branch, MAX(GPA) AS max_gpa FROM STUDENT1 GROUP BY branch;

You might also like