L J INSTITUTE OF ENGINEERING & TECHNOLOGY, LJU SUBJECT: - DATABASE MANAGEMENT SYSTEM IPE PRACTICAL LIST_SEM-II_2022 SOLUTION 1. Write a PL/SQL block to swap two numbers without using third variable. Ans: SQL> set serveroutput on; SQL> declare a number; b number; begin a:=&a; b:=&b; dbms_output.put_line(a||''||b); a:=a+b; b:=a-b; a:=a-b; dbms_output.put_line('after swapping:'||a||' '||b); end; / OUTPUT: 2. Consider following relations: Employee(empno, emp_name, dept,salary, doj, branch) Answer the following queries in SQL: 1) Retrieve employee number and their salary Ans: select empno, salary from employee; 2) Retrieve total salary of employee group by employee name and count similar names Ans: SELECT EMP_NAME, SUM(SALARY),COUNT(*) FROM EMPLOYEE GROUP BY(EMP_NAME); 3) Retrieve total salary of employee group by employee name and salary which is greater than >120000 Ans: SELECT EMP_NAME, SUM(SALARY) FROM EMPLOYEE GROUP BY(EMP_NAME) HAVING SUM(SALARY)>120000; 4) Display name of employee in descending order Ans: select emp_name from employee order by emp_name desc; 5) Display details of employee whose name is AMIT and salary greater than 50000; Ans: select * from employee where emp_name='Amit' and salary>50000; 3. Given the table EMPLOYEE (EmpNo, Name, Salary, Designation, DeptID) write a cursor to select the five highest paid employees from the table. EMPLOYEE (EmpNo, Name, Salary, Designation, DeptID) Ans: declare i number:=0; cursor ec is select empno,name,salary from employee order by salary desc; r ec%rowtype; begin open ec; loop exit when i=5; fetch ec into r; dbms_output.put_line(r.empno||' '||r.name||' '||r.salary); i:=i+1; end loop; close ec; end; / OUTPUT: 4. Write PL/SQL block to print whether the given number is Armstrong number or not. If given number is Armstrong then insert that number into Armstrong table. Ans: declare n number:=&n; s number:=0; r number; leng number; m number; begin m:=n; leng:=length(to_char(n)); while n>0 loop r:=mod(n,10); s:=s+power(r,leng); n:=trunc(n/10); end loop; if m=s then insert into armstrong values(m); dbms_output.put_line('armstrong number'); else dbms_output.put_line('not armstrong number'); end if; end; / OUTPUT: 5. Write a PL/SQL cursor to display the names and branch of all students from the STUDENT relation. Ans: DECLARE CURSOR cur_test IS SELECT name,BRANCH FROM student; name1 student.name%TYPE; branch1 student.branch%type; BEGIN OPEN cur_test; if cur_test%isopen then Loop FETCH cur_test INTO name1,branch1; EXIT WHEN cur_test%NOTFOUND; DBMS_OUTPUT.PUT_LINE('Name: '||name1||’ ‘||'Branch: '||branch1); END LOOP; END IF; CLOSE cur_test; END; / OUTPUT: 6. Consider following relations: Supplier(sid,sname,status,city) Parts(pid,pname,color,weight,city) Sp(sid,pid,quantity) Answer the following queries: 1) Display the name of supplier who lives in ‘Ahmedabad’ Ans: Select sname from supplier where city=’Ahmedabad’ 2) Display the parts name which is not supplied yet Ans: Select sname from parts inner join sp on parts.pid<>sp.pid; 3) Find all supplier whose status is either 20 or 30 Ans: Select sname from supplier where status=20 or status=30; 4) Count how many times each supplier has supplied part ‘P2’ Ans: Select sid,count(*) from sp group by sid where pid=’P2’; 5) Delete a part from parts whose weight is more than 10kg. Ans: DELETE FROM Parts WHERE weight>10; 7. Consider following relations: employee(eid, name, address, dept, salary) Answer the following queries: 1) Display the name and salary of employee who is taking maximum salary. Ans: select name,salary from employee where salary=(select max(salary) from employee); 2) Display the name of employee who works in department having count of employee is less than 2. Ans: select name from employee where dept in(select dept from employee group by dept having count(*) < 2); 3) Display highest salary department wise and name of employee who is taking that salary. Ans: select name,salary from employee where salary in(select max(salary) from employee group by dept); 4) Display second highest salary from employee table Ans: select max(salary) from employee where salary <>(select max(salary) from employee); 5) Display the details of employee whose name starts with ‘A’. Ans: select * from employee where name like ‘A%’; 8. Write PL/SQL program to convert each digit of a given number into its corresponding word format. DECLARE num INTEGER; number_to_word VARCHAR2(100); digit_str VARCHAR2(100); len INTEGER; digit INTEGER; BEGIN num := &num; len := LENGTH(num); dbms_output.PUT_LINE('Input: ' ||num); FOR i IN 1..len LOOP digit := SUBSTR(num, i, 1); if(digit = 1) THEN digit_str := 'one '; elsif (digit = 2) THEN digit_str := 'two ' ; elsif (digit = 3) THEN digit_str :='three '; elsif (digit = 4) THEN digit_str :='four '; elsif (digit = 5) THEN digit_str :='five '; elsif (digit = 6) THEN digit_str :='six '; elsif (digit = 7) THEN digit_str :='seven '; elsif (digit = 8) THEN digit_str :='eight '; elsif (digit = 9) THEN digit_str :='nine '; elsif (digit = 0) THEN digit_str :='zero '; else digit_str :='nan '; end if; number_to_word := number_to_word || digit_str; END LOOP; dbms_output.PUT_LINE('Output: ' ||number_to_word); END; 9. Write PL/SQL program to find the sum of digits of a number. DECLARE --Declare variable n, temp_sum -- and r of datatype number n number; temp_sum number; r number; BEGIN n := &n; temp_sum := 0; -- here we check condition with the help of while loop -- here <> symbol represent for not null WHILE n <> 0 LOOP r := n mod 10; Or r := MOD(n,10); temp_sum := temp_sum + r; n := Trunc(n / 10); -- (Note:Trunc is used to remove decimal points from number,if we declare variable of int type --then there is no need of trunc) END LOOP; dbms_output.Put_line('sum of digits = ' || temp_sum); END; / 10. Consider the following Tables and Write PL/SQL program to Transfer All the Students having marks greater than 70 from ‘Student’ table into another table named ‘star_batch’ using Explicit Cursor. Student(id,name,mobile,marks,dob) Star_batch(id,name,mobile,marks,dob) 11. Consider the following schema & create table for that: BOOK (Book_id, Title, Publisher_Name, Pub_Year) BOOK_AUTHORS (Book_id, Author_Name) PUBLISHER (Name, Address, Phone) BOOK_COPIES(Book_id, Branch_id, No-of_Copies) BOOK_LENDING (Book_id, Branch_id, Card_No, Date_Out, Due_Date) LIBRARY_BRANCH (Branch_id, Branch_Name, Address) Write SQL queries to 1.Describe all the tables a. DESC BOOK; b. DESC BOOK_AUTHORS; c. DESC PUBLISHER; d. DESC BOOK_COPIES; e. DESC BOOK_LENDING; f. DESC LIBRARY_BRANCH; 2.Insert the details in all the tables INSERT INTO BOOK VALUES (1,‘DBMS‘,‘JAN-2017‘, EMCGRAW-HILL‘); INSERT INTO BOOK VALUES (50,‘ADBMS‘,‘JUN-2016‘, EMCGRAW-HILL‘); INSERT INTO PUBLISHER VALUES (EMCGRAW-HILL‘, 9989076587, EBANGALORE‘); INSERT INTO BOOK_AUTHORS VALUES (‘NAVATHE‘, 2); INSERT INTO LIBRARY_BRANCH VALUES (10,‘RR NAGAR‘,‘BANGALORE‘); INSERT INTO BOOK_COPIES VALUES (10, 1, 10); INSERT INTO BOOK_LENDING VALUES ( 1, 10, 101, ‘01-JAN-17‘,‘01-JUL-17’); INSERT INTO BOOK_LENDING VALUES ( 3, 14, 101, ‘11-JAN-17‘,‘11-MAR17’); 3.Retrieve details of all books in the library – id, title, name of publisher, authors SELECT BOOK_ID, TITLE, PUBLISHER_NAME, AUTHOR_NAME, FROM BOOK INNER JOIN BOOK_AUTHORS ON BOOK.BOOK_ID=BOOK_AUTHER.BOOK_ID; 4.Delete a book in BOOK table whose book_id is 50 . DELETE FROM BOOK WHERE Book_id=50; 5. Get the particulars of borrowers who have borrowed more than 3 books, but from Jan 2017 to Jun2017. SELECT CARD_NO FROM BOOK_LENDING WHERE DATE_OUT BETWEEN ‘01-JAN-2017‘ AND ‘01-JUL-2017‘ GROUP BY CARD_NO HAVING COUNT (*)>3; 12. Consider the following schema & create table for that: Physician(regno,name,phone_no,city) Patient(pname, street,city) Visit(pname,regno,visitdate,fee) Answer the following queries: 1) Get the name and regno of the physicians who lives in Mumbai 2) Find the name and city of the patient who visited physician on 01-aug-2022 3) Get the name of physician and total no of patients who visited that physician 4) Get the name of physician who have not visited any patient 13. Write queries for the following tables: T1 ( Empno, Ename , Salary, Designation) T2 (Empno, Deptno.) (1) Display all the details of the employee whose salary is lesser than 10K. (2) Display the Deptno in which Employee Seeta is working. (3) Add a new column Deptname in table T2. (4) Change the designation of Geeta from ‘Manager’ to ‘Senior Manager’. (5) Find the total salary of all the employees. (6) Display Empno, Ename, Deptno and Deptname. 14. Consider the relation Database. Person(SSN, name, city) Car(license_no, year, model, SSN) Accident(drive_no, SSN, license_no, accidentyear, damage_amt) 1) Find out total no of cars that has accident in 1988. 2) Find the name of driver who did not have an accident in 'Delhi' 3) Find the car, who don't have total damage of more than 1000rs. 4) Find the cars sold in 2006 and whose owner are from vadodara. 5) How many different models of car are used by Mr.abc. 15. Write a PL/SQL cursor to display the names and branch of all students from the STUDENT relation. Ans: DECLARE CURSOR cur_test IS SELECT name,BRANCH FROM student; name1 student.name%TYPE; branch1 student.branch%type; BEGIN OPEN cur_test; if cur_test%isopen then Loop FETCH cur_test INTO name1,branch1; EXIT WHEN cur_test%NOTFOUND; DBMS_OUTPUT.PUT_LINE('Name: '||name1||'Branch: '||branch1); END LOOP; END IF; CLOSE cur_test; END; / 16. Write a PL/SQL block to print the given number is prime or not. Ans: declare num number; i number:=1; c number:=0; begin num:=&num; for i in 1..num loop if((mod(num,i))=0) then c:=c+1; end if; end loop; if(c>2) then dbms_output.put_line(num||' not a prime'); else dbms_output.put_line(num||' is prime'); end if; end; / 17. Write a function that take employee no and return the salary with the help of following table: Emp(eno,ename,city,salary) 18. Write a procedure to display multiplication table of user input. 19. Write a trigger to check the pincode is exactly six digits or not for the following table: Emp(eno,ename,city,pincode) 20. Write a cursor to display the first ten records of the following table: Student(rollno,name,address,city)