Professional Documents
Culture Documents
DBMS All Exercise
DBMS All Exercise
DBMS All Exercise
Exercise – 1
Modify the table student mark sheet using the alter command:-
Q.1. Insert values into student table using field names using insert
Command.
Ans.
INSERT INTO student (student_number, student_name, student_address,
student_dob, student_branch) VALUES (101, 'Kishore', 'Bangalore', '1987-12-
20', 'cse');
Q.1. Insert values into employee table with field names using insert
Command.
Ans.
INSERT INTO employee (emp_number, emp_name, emp_dob , emp_address ,
emp_deptno) values (1000,'naveen','1985-07-18','silk board',555);
Ans.
SELECT
TO_CHAR(student_name, 'FM') AS first_character_of_name,
TO_CHAR(student_number, 'FM9') AS first_digit_of_number
FROM
student;
Q.4. write a query to display the sub string for the word ‘oracle’ from its
position 2 to 3.
Q.6. Write a query to maintain the first letter as caps using initcap
function.
Q.7. Write a query to display all characters is in caps using upper function.
Q.8. Write a query to display the all characters in small using lower
function.
Q.9. Write a query to display ‘smudde’ after leaving 5 blank places using
lpad function.
Q.10. Write a query using RPAD() function to display given string with
length of 10, if string characters is less than the length then the remaining
space will be filled with ‘#’. The string name is manipal.
Ans. select RPAD('manipal',10,'#')from dual;
Q.11. Write a query to remove blanks at the left-end of a string with abc.
Q.12. write a query to remove blanks at the right-end of a string with /abc.
Q.13. write a query to display to the number of days between any two
months using month() function.
Q.14. write a query to add the month with date and year using add months
Function.
Ans.
declare
sno student.student_number%type=&n;
name student.student_name%type;
begin
savepoint dup_data
select student_name into name from student where student-number=sno;
exception when NO_DATA_FOUND then
dbms_output.put_line(‘no such student ,so insert that sno into table’);
Insert into student values (&student number, &student_name);
dbms_output.put_line(‘student number’|student.|student-number);
dbms_output.put_line(‘student name’||student. student_name );
Rollback dup_data;
end;
Q.2. Insert data into student table and use commit rollback and save
point in PL/SQL block.
Ans.
COMMIT [TABLENAME | TRANSACTION] (syntax)
SQL -> commit [student | insert] (example)
Exercise – 6
Q.1. Develop a program that includes the features of nested if-case and
case expression. The program can be extended using the null if and
coalesce functions.
Ans.
NESTED-IF:
DECLARE
sno NUMBER(3):=&sno;
sname VARCHAR2(20):=’&sname’;
m1 NUMBER(3):=&m1;
m2 NUMBER(3):=&m2;
m3 NUMBER(3):=&m3;
total NUMBER(4);
avg1 NUMBER(5,2);
class VARCHAR(30);
BEGIN
total:=m1+m2+m3;
avg1:=total/3;
if m1>35 AND m2>35 AND m3>35 then
if avg1>60 then
class:=’first class’;
end if;
if avg1>50 AND avg1<60 then
class:=’second class’;
end if;
if avg1>40 AND avg1<50 then
class:=’third class’;
end if;
else
class:=’fail’;
end if;
insert into student(sno,sname,m1,m2,m3,total,avg1,class);
END;
NULL-IF:
DECLARE
v_num NUMBER:=&sv_user_num;
v_remainder NUMBER;
BEGIN
---------calculate the remainder and if it is zero
return a NULL v_remainder:=NULLIF(MOD(v_num,2),0);
DBMS_OUTPUT.PUT_LINE(‘v_remainder:’||v_remainder);
END;
CASE:
DECLARE
v_num1 NUMBER:=&sv_num1;
v_num2 NUMBER:=&sv_num2;
v_num3 NUMBER:=&sv_num3;
v_result NUMBER;
v_result:=CASE
WHEN v_num1 IS NOT NULL THEN v_num1
ELSE
CASE
WHEN v_num2 IS NOT NULL THEN v_num2
ELSE v_num3
END
END;
DBMS_OUTPUT.PUT_LINE(‘RESULT:’||v_result);
END;
COALESCE:
DECLARE
v_num1 NUMBER:=&sv_num1;
v_num2 NUMBER:=&sv_num2;
v_num3 NUMBER:=&sv_num3;
v_result NUMBER;
BEGIN
v_result:=COALESCE(v_num1,v_num2,v_num3);
DBMS_OUTPUT.PUT_LINE(‘RESULT:’||v_result);
END;
Exercise – 7
Q.1. Write a Program using loop, while loops, for loops along with error
handling and user defined exceptions.
To generate first 10 natural numbers using loop, while and for.
Ans.
declare
I number;
begin
I:=1;
loop
dbms_output.put_line(I);
I:=I+1;
exit when I>10;
end loop;
end;
Ans.
create a procedure using IN parameter:
select depino
into emp_dept
from employee
where emp_number=emp_number;
end;
Exercise – 9
Q.1. Write a PL/SQL Program using stored functions and invoke functions.
Ans.
Create function which adds two given numbers:
Q.2. Create a function which count total no.of employees having salary less
than 6000.
Ans.
create or replace function count_emp(esal number)return number as
cursor vin_cur as Select emp_number,emp_sal from emplpyee;
xno employee.emp_number%type;
xsal employee.emp_sal%type;
C number;
begin
open vin_cur;
C:=0;
loop
fetch vin_cur into xno,xsal;
if(xsal<esal) then
c:=c+1;
end if;
exit when vin_cur%notfound;
end loop;
close vin_cur;
return c;
end;
/
Function created.
/*function specification*/
declare
ne number;
xsal number;
begin
ne:=count_emp(xsal);
dbms_output.put_line(xsal);
dbma_output.put_line(‘there are ‘||ne||employees’);
end;
Exercise – 10
Q.1. Write a PL/SQL Program using BEFORE and AFTER Triggers – with
operations like insert/update/delete which will run on Sundays and all
weekdays between 17 pm to 9 am (the following day).
Ans.
create trigger trig1
before insert or update or delete on employee
for each row
declare
ctime number:=to_char(sysdate,’hh24’);
cday varchar2(10):=to_char(sysdate,’day’);
begin
if ctime<9 or ctime>17 then
raise_application_error(-20002,’cannot access data’);
end if;
if cday=’sunday’ orcday=’SUNDAY’ then
raise_application_error(-20002,’cannot access data’);
end if;
end;
Exercise – 11
Q.1. Write a PL/ SQL cursor program to fetch first three records of an
employee table.
Ans.
declare
cursor c2 is select * from employee;
erec employee%rowtype;
begin
open c2;
loop
fetch c2 into erec;
exit when(c2%ROWCOUNT>3);
dbms_output.put_line(‘----------------‘);
dbms_output.put_line(‘emp_number:’||erec.emp_number);
dbms_output.put_line(‘emp_name:’||erec.emp_name);
end loop;
dbms_output.put_line(‘no. of employees:’||c2%rowcount);
close c2;
end;