DBMS (Ankit) (Last)
DBMS (Ankit) (Last)
Design an ER diagram for a real-world system, identify entities, attributes, keys, relationships, and cardinalities for
the following problem:
Problem:
Consider a Library Management System. The library has multiple books, students, and employees. The students borrow
books from the library. Each book is written by one or more authors. Employees work in the library and manage the books
and transactions. A student can borrow many books, but each book can only be borrowed by one student at a time.
The system also keeps track of the due dates for borrowed books and fines for overdue books. Each book can have multiple
copies available in the library.
Assignment No 2
Convert an ER model to a relational model and apply normalization techniques for the
following problem:
Problem:
Consider the following University Management System with the following entities and relationships:
Entities:
Student: Attributes - StudentID, Name, DOB, Email
Course: Attributes - CourseID, CourseName, Credits
Professor: Attributes - ProfessorID, Name, Department
Relationships:
A Student enrolls in many Courses, and each Course can have many Students (Many-to-Many).
A Professor teaches many Courses, and each Course is taught by one Professor (One-to-Many).
Assignment No 3
Instructor
Queries:
Assignment No 4
Employee
Queries:
Query 3: To Display all the fields and the values of the table.
Ans: SELECT * FROM Employee
ID NAME DEPT_NAME SALARY
1 Amit CSE 35000
2 Rohit ME 25000
3 Abhishek CE 25000
4 Raja EE 32000
5 Bishal ECE 20000
Query 7: Display the id, name of all the employees from the employee table.
Ans: SELECT Id, Name FROM Employee;
ID NAME
1 Amit
2 Rohit
3 Abhishek
4 Raja
5 Bishal
Query 8: Display only the salary field from the table eliminating the duplicate values.
Ans: SELECT DISTINCT Salary FROM Employee;
SALARY
38000
32000
20000
25000
Query 9: Display the names of all the employees whose salary is > 30000 and belongs to hr
department.
Ans: SELECT Name
FROM Employee
WHERE Salary > 30000
AND Dept_name = 'HR';
no data found
Query 10: After giving 10% increment to the salary display the name & salary from employee table.
Ans: UPDATE Employee
SET Salary = Salary * 1.10;
NAME SALARY
Amit 41800
Rohit 27500
Abhishek 27500
Raja 35200
Bishal 22000
Employee
Queries:
Q.4) Find all the records whose name contains the letter "s".
Ans: SELECT *
FROM Employee
WHERE LOWER(Emp_name) LIKE '%s%';
EMP_ID EMP_NAME EMP_ADDRESS AGE SALARY
002 Suman Durgapur 23 10500
003 Sukanya Kharagpur 27 5005
Q.5) Find name of Employees whose salary has 4 digits & starts with 500.
Ans: SELECT Emp_name
FROM Employee
WHERE Salary LIKE '500%';
EMP_NAME
Rohan
Sukanya
Q.6) Find the name of all employees whose age is b/w 20 & 30.
Ans: SELECT Emp_name
FROM Employee
WHERE Age BETWEEN 20 AND 30;
EMP_NAME
Rohan
Suman
Sukanya
Arpan
Q.7) Find name of the Employee who are staying either in Kolkata or Durgapur.
Ans: SELECT Emp_name
FROM Employee
WHERE Emp_address = 'Kolkata' OR Emp_address = 'Durgapur';
EMP_NAME
Rohan
Suman
Arpan
Q.8) Find name of all the Employees whose city is not in Delhi.
Ans: SELECT Emp_name
FROM Employee
WHERE Emp_address NOT IN ('Delhi');
EMP_NAME
Rohan
Suman
Sukanya
Arpan
Q.9) Display the data in descending order based on the salary field.
Ans: SELECT *
FROM Employee
ORDER BY Salary DESC;
EMP_I
EMP_NAME EMP_ADDRESS AGE SALARY
D
002 Suman Durgapur 23 10500
004 Arpan Durgapur 30 9000
005 Arpita Delhi 35 8000
003 Sukanya Kharagpur 27 5005
001 Rohan Kolkata 22 5000
Assignment No 6
Employee
Queries:
NUMBER_OF_EMPLOYEES
5
Assignment No 7
Project_592
Employee_592
Queries:
Query 4: Find Pno, Pname, ename who are working on a project located at Bangalore.
Ans: SELECT P.Pno, P.Pname, E.ename
FROM Project_592 P
JOIN Employee_592 E ON P.Pno = E.Pno
WHERE P.Location = 'Bangalore';
PNO PNAME ENAME
P02 DBMS SG
Department_592
Instructor_592
Course_592
Queries:
Query 4: Retrieve the name of all instructors along with their dname, and building.
Ans: SELECT I.Name, I.dname, D.Building
FROM Instructor_592 I
JOIN Department_592 D ON I.dname = D.dname;
NAME DNAME BUILDING
OB CSE CS block
AD CSE CS block
MB ME ME block
RM CE CE block
MG ME ME block
Query 5: Find those instructors (name, C_id) who have taught courses.
Ans: SELECT I.Name, C.C_id
FROM Instructor_592 I
JOIN Course_592 C ON I.ID = C.ID;
NAME C_ID
OB CS201
AD CS101
MB ME301
RM CE203
Query 6: Find those instructors (name, C_id) who belong to the CSE department.
Ans: SELECT I.Name, C.C_id
FROM Instructor_592 I
JOIN Course_592 C ON I.ID = C.ID
WHERE I.dname = 'CSE';
NAME C_ID
OB CS201
AD CS101
Assignment No 9
emp_592
Queries:
Query 1: Display all employee names and their salary whose salary is greater than the minimum salary
of the employee and job title is 'AP'.
SELECT ename, salary
FROM emp_592
WHERE salary > (SELECT MIN(salary) FROM emp_592 WHERE job = 'AP')
AND job = 'AP';
ENAME SALARY
RS 15000
TD 18000
Query 2: Write a query to find all employees who work in the same job as 'AB'.
Ans: SELECT ename, job
FROM emp_592
WHERE job = (SELECT job FROM emp_592 WHERE ename = 'AB');
ENAME JOB
AB AP
RS AP
TD AP
Query 3: Write a query to display information about employees who earn more than any employee in
dept_no 1.
Ans: SELECT *
FROM emp_592
WHERE salary > ALL (SELECT salary FROM emp_592 WHERE dept_no = 1);
ENO ENAME JOB DEPT_NO SALARY
e02 SD Lecturer 2 20000
Query 4: Write a query to find the name of employees who work in the same department as
'TD'.
Ans: SELECT ename
FROM emp_592
WHERE dept_no = (SELECT dept_no FROM emp_592 WHERE ename = 'TD');
ENAME
AB
RS
TD
Query 5: Write a query to find the eno whose salary is less than the maximum salary of an employee in
dept_no 2.
Ans: SELECT eno
FROM emp_592
WHERE salary < (SELECT MAX(salary) FROM emp_592 WHERE dept_no = 2);
ENO
e01
e03
e04
e05
Assignment No 10
Dual Table: Dual is a special 1 row and 1 column table present by default in Oracle. It contains 1
column named "dummy" and contains one row with a value X. It is useful for constant expression with
select statement.
Queries:
Query 5: To display a floating point number in a round figure (ceil and floor value option).
Ans: SELECT CEIL(25.3) AS Ceil_Value, FLOOR(25.7) AS Floor_Value FROM DUAL;
CEIL_VALUE FLOOR_VALUE
26 25
Program1: Using PL/SQL write a program to find the sum of even and odd numbers in a given range.
Ans: DECLARE
start_num NUMBER := 1; -- starting number of the range
end_num NUMBER := 10; -- ending number of the range
even_sum NUMBER := 0; -- variable to store the sum of even numbers
odd_sum NUMBER := 0; -- variable to store the sum of odd numbers
BEGIN
FOR i IN start_num..end_num LOOP
IF MOD(i, 2) = 0 THEN
even_sum := even_sum + i; -- add to even sum
ELSE
odd_sum := odd_sum + i; -- add to odd sum
END IF;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Sum of even numbers: ' || even_sum);
DBMS_OUTPUT.PUT_LINE('Sum of odd numbers: ' || odd_sum);
END;
Output:
Statement processed.
Sum of even numbers: 30
Sum of odd numbers: 25
Program2: Using PL/SQL write a program to find the summation of first and last digit of a n-digit
number.
Ans: DECLARE
num NUMBER := 12345; -- input number
first_digit NUMBER; -- variable to store the first digit
last_digit NUMBER; -- variable to store the last digit
sum_digits NUMBER; -- variable to store the sum of first and last digit
BEGIN
last_digit := MOD(num, 10); -- get the last digit
-- find the first digit by dividing the number until it's a single digit
first_digit := num;
WHILE first_digit >= 10 LOOP
first_digit := FLOOR(first_digit / 10);
END LOOP;
Output:
Statement processed.
Sum of first and last digit: 6
Program3: Using PL/SQL write a program to reverse a number.
Ans: DECLARE
num NUMBER := 12345; -- input number
reversed_num NUMBER := 0; -- variable to store the reversed number
BEGIN
WHILE num > 0 LOOP
reversed_num := (reversed_num * 10) + MOD(num, 10); -- reverse the number
num := FLOOR(num / 10); -- remove the last digit from the number
END LOOP;
Output:
Statement processed.
Reversed number: 54321
*
**
***
****
*****
Ans: DECLARE
i NUMBER;
j NUMBER;
BEGIN
FOR i IN 1..5 LOOP -- 5 rows for the pattern
FOR j IN 1..i LOOP -- increasing number of stars in each row
DBMS_OUTPUT.PUT('*');
END LOOP;
DBMS_OUTPUT.PUT_LINE(''); -- move to the next line after each row
END LOOP;
END;
Output:
Statement processed.
*
**
***
****
*****
Program5: Write a PL/SQL program to generate the Fibonacci series up to nth term.
Ans: DECLARE
n NUMBER := 10; -- number of terms in the Fibonacci series
a NUMBER := 0; -- first term
b NUMBER := 1; -- second term
c NUMBER; -- variable to store the next term in the series
BEGIN
DBMS_OUTPUT.PUT_LINE('Fibonacci series:');
DBMS_OUTPUT.PUT_LINE(a); -- print the first term
DBMS_OUTPUT.PUT_LINE(b); -- print the second term
Output:
Statement processed.
Fibonacci series:
0
1
1
2
3
5
8
13
21
34