DBMS All Exercise

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

DBMS PRACTICAL

Exercise – 1

Q.1. CREARE TABLE student (student number (int) (primary


key),student name (varchar),student address (varchar),student dob (date
and time), student branch (char).

Ans. CREATE TABLE student(student_number int,student_name


varchar(30),student_address varchar(30),student_dob date, student_branch
varchar(30));

Q.2. CREARE TABLE student_marksheet (student number (int) (primary


key), student sem (int),student total marks (int).

Ans. CREATE TABLE student_marksheet(student_number int, student_sem


varchar(30), student_dob date, student_brach varchar(30));

Q.3. Create a table employee with emp number(int) (primary key),emp


name(varchar),emp dob(date and time),emp address(varchar),emp
deptno(int), emp sal int.

Ans. CREATE TABLE employee(emp_number int, emp_name varchar(30),


emp_dob date, emp_address varchar(30), emp_deptno int).
Q.4. Create a table emp dept with Emp number (int) (primary key),emp
deptno(int),dept name(char).

Ans. CREATE TABLE emp_dept(emp_number int, empdept_name


varchar(30),emp_deptno int);

Q.5. CREARE TABLE emp salary with emp number(int),doj (date),emp


sal(big int).

Ans. CREATE TABLE emp_salary(emp_number int, dob date, emp_sal int);

Modify the table student mark sheet using the alter command:-

For modify the table syntax:

Add column - ALTER TABLE table_name ADD column_name datatype;

Delete column - ALTER TABLE table_name DROP COLUMN column_name


datatype;

Remove the table:


DROP TABLE table_name
Exercise – 2

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

INSERT INTO student (student_number, student_name, student_address,


student_dob, student_branch) VALUES (102, 'ramesh', 'delhi', '1987-12-21',
'ece');

INSERT INTO student (student_number, student_name, student_address,


student_dob, student_branch) VALUES (103, 'rahul', 'chennai', '1987-12-22',
'eee');

INSERT INTO student (student_number, student_name, student_address,


student_dob, student_branch) VALUES (104, 'narsh', 'hyd', '1987-12-23', 'cse');

INSERT INTO student (student_number, student_name, student_address,


student_dob, student_branch) VALUES (105, 'ramya', 'chennai', '1987-12-24',
'Cse');

Q.2. Select the table student using select command.

Ans. select * from student;


Q.3. Update student address in student table using update command.

Ans. update student set student_address= “bangalore” where student_number =


104;

Q.4. Delete a row from student table.

Ans. DELETE FROM student WHERE student_number = “105”;


Exercise – 3

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

INSERT INTO employee (emp_number, emp_name, emp_dob , emp_address ,


emp_deptno) values (1001,'suresh','1985-07-19','kr puram',556);

INSERT INTO employee (emp_number, emp_name, emp_dob , emp_address ,


emp_deptno) values (1002, 'chandu' , '1985-07-17' , 'cv raman nagar',557);

INSERT INTO employee (emp_number, emp_name, emp_dob , emp_address ,


emp_deptno) values (1003, 'ramya' , '1985-07-16' , 'hsr layout',558);

INSERT INTO employee (emp_number, emp_name, emp_dob , emp_address ,


emp_deptno) values (1004,'mohan','1985-07-15','btm layout',559);

Q.2. Select the table employee using select command.

Ans. select * from employee;

Q.3. Update emp dept numbers in employee table using update


command.
Ans. update employee set emp_address = 'chennai' where emp_number = 1004;

Q.4. Delete a row from employee table.

Ans. DELETE FROM employee WHERE emp_number = "1003";


Exercise – 4

Q.1. Write a query to display the student_name (first character) and


student_ number(First digit) using to_char.

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.2. Write a query to display the current system time.

Ans. SELECT TO_CHAR(SYSDATE, 'HH24:MI:SS') AS


current_system_time FROM DUAL;

Q.3. Write a query to display today’s date.

Ans. SELECT TO_DATE(SYSDATE, 'YYYY-MM-DD') AS today_date FROM


DUAL;

Q.4. write a query to display the sub string for the word ‘oracle’ from its
position 2 to 3.

Ans. select substr('oracle',2,3) from dual;


Q.5. Write a query to change the ‘or’ to ‘ab’ in the word ‘corporation’
using translate function.

Ans. select translate('corportion','or','ab')from dual;

Q.6. Write a query to maintain the first letter as caps using initcap
function.

Ans. select initcap('oracle')from dual;

Q.7. Write a query to display all characters is in caps using upper function.

Ans. select upper ('oracle') from dual;

Q.8. Write a query to display the all characters in small using lower
function.

Ans. select lower ('ORACLE') from dual;

Q.9. Write a query to display ‘smudde’ after leaving 5 blank places using
lpad function.

Ans. select LPAD('smudde',11,'*')from dual;

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.

Ans. select LTRIM(' abc')from dual;

Q.12. write a query to remove blanks at the right-end of a string with /abc.

Ans. select RTRIM('/abc ')from dual;

Q.13. write a query to display to the number of days between any two
months using month() function.

Ans. select months_between ('23-aug-2008','16-feb-2009')from dual;

Q.14. write a query to add the month with date and year using add months
Function.

Ans. select add_months('3-jan-09',4)from dual;


Exercise – 5

Q.1. Creation of simple PL/SQL program which includes declaration


section, executable section and exception handling section.

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;

(same code using while loop):


declare
I number;
begin
I:=1;
while (I<=10)
loop
dbms_output.put_line (I);
I:=I+1;
end loop;
end;

(same code using for loop):


Begin
For I in 1...10
loop
Dbms_output.put_line (I);
End loop;
End;

(Program using predefined exceptions):


declare
emp_number employee.emp_number%type=&number;
name employee.emp_name%type;
begin
select emp_name into name from employee
where emp_number=emp_number;
exception when NO_DATA_FOUND then
dbms_output.put_line(‘no such employee’);
end;
Exercise – 8

Q.1. Write a PL/SQL Program to create


i) to create procedures ii) pass parameters IN & OUT.

Ans.
create a procedure using IN parameter:

create or replace procedure p1 (eno IN number,employee_name varchar)


as
begin
update employee set employee_name=employee_name where
emp_number = eno;
end;

create a procedure using OUT parameter:

Create procedure p2(p2 emp_number int, p2 emp_deptno out int)


is
begin

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:

create or replace function add_fun(a number,b number)


return number as
C number;
begin
C:=a+b;
return c;
end;
/*add_fun specification*/
Declare
Result number;
Begin
Result:=add_fun(10,20);
Dbms_output.put_line(‘the sum of 10 and 20 is’||result);
End;

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;

You might also like