0% found this document useful (0 votes)
42 views51 pages

DBMS 1st Yr 2nd Sem

Uploaded by

sumansri459
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views51 pages

DBMS 1st Yr 2nd Sem

Uploaded by

sumansri459
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

THAVATHIRU SANTHALINGA ADIGALAR ARTS SCIENCE AND TAMIL COLLEGE

PERUR, COIMBATORE – 641010.

E-Mail: [email protected],Website:tsatamilcas.edu.in

Affiliated to Bharathiar University, Approved by UGC under Section 2(f) & 12(B)

Accredited With B+ Grade by NAAC

DEPARTMENT OF COMMERCE WITH COMPUTER APPLICATIONS

I - B.COM (CA)

SEMESTER – II

PROGRAMMING LAB - DATABASE MANAGEMENT SYSTEM PRACTICALS

NAME : ……………………………….

REGISTER NO : ……………………………….
THAVATHIRU SANTHALINGA ADIGALAR ARTS SCIENCE AND TAMIL COLLEGE
PERUR, COIMBATORE– 641 010
CERTIFICATE

Certified that this is a bonafide record work done by ________________________ with


the Register Number ______________ of B.COM (CA) Degree-Semester II, during the Academic
Year 2023 – 2024

Staff in charge Head of Department

Submitted for the Practical Examination held by BHARATHIYAR UNIVERSITY,


Coimbatore, at Thavathiru Santhalinga Adigalar Arts, Science and Tamil College, Perur,
Coimbatore – 641 010, on _______________ .

Internal Examiner External Examiner

Place : Perur
Date :
INDEX

S.No DATE TITLE PAGE SIGNATURE

1. IMPLEMENTATION OF DDL
COMMANDS

2. IMPLEMENTATION OF DML AND


DCL COMMANDS

3.
EMPLOYEE DATABASE

4.
STUDENT DATABASE

5. COLLEGE DATABASE

6. BANK DATABASE

7. FACTORIAL AND FIBONACCI


SERIES

8. PALINDROMEAND NUMBER
REVERSE

9. EXCEPTION HANDLING

10. TRIGGER CREATION :RECORD


INSERTION AND DELETION

11. TRIGGER CREATION : RECORD


UPDATION

12. CURSOR CREATION : RECORD


UPDATION

13. PROCEDURE CREATION :


RECORD SEEK AND DELETE

14. FUNCTION CREATION : RECORD


SEEK
EX.NO:1 IMPLEMENTATION OF DDL COMMANDS
DATE:

AIM:

To create a DDL to perform creation of table, alter, modify and drop column.

ALGORTHIM:
CREATION OF TABLE:

SQL>create table std(regno number(5),snamevarchar(20),age number(5),sdob date,sm1

number(4,2),sm2 number(4,2),sm3 number(4,4));

Table created.

SQL>insert into std values(101,’AAA’,16,’03-jul-88’,80,90,98);

1 row created.

SQL>insert into std values(102,’BBB’,18,’04-aug-89’,88,98,90);

1 row created.

OUTPUT:

Select * from std;

REGNO SNAME AGE SDOB SM1 SM2 SM3

101 AAA 16 03-jul-88 80 90 98

102 BBB 18 04-aug-89 88 98 90

ALTER TABLE WITH ADD:

SQL>create table student(id number(5),name varchar(10),game varchar(20));

Table created.

SQL>insert into student values(1,’mercy’,’cricket’);

1 row created.

SQL>alter table student add(age number(4));

SQL>insert into student values(2,’sharmi’,’tennis’,19);


OUTPUT:

Before ALTER: select * from student;

ID NAME GAME

1 Mercy Cricket

ADD: select * from student;

ID NAME GAME AGE

1 Mercy cricket

2 Sharmi Tennis 19

ALTER TABLE WITH MODIFY:

SQL>alter table student modify(sid varchar(10), game varchar(25));

OUTPUT:

MODIFY

desc student;

NAME NULL? TYPE

Sid Varchar(10)

Name Varchar(20)

Game Varchar(25)

Age Number(4)

DROP

SQL>drop table student;

SQL>Table dropped.

TRUNCATE TABLE

Example: Truncate table stud;

RESULT:

Thus the DDL commands have been executed successful.


EX.NO:2 IMPLEMENTATION OF DML AND DCL COMMANDS
DATE:

AIM;

To study the various DML commands and implement them on the database.

ALGORITHM:
Q1: Insert a single record into dept table.

Ans: SQL> insert into dept values (1,'IT','Tholudur');

1 row created.

Q2: Insert more than a record into emp table using a single insert command.

Ans: SQL> insert into emp values(&empno,'&ename','&job',&deptno,&sal);

Enter value for empno: 1

Enter value for ename: Mathi

Enter value for job: AP

Enter value for deptno: 1

Enter value for sal: 10000

old 1: insert into emp values(&empno,'&ename','&job',&deptno,&sa


new 1: insert into emp values(1,'Mathi','AP',1,10000)

1 row created.

SQL> / Enter value for empno: 2

Enter value for ename: Arjun

Enter value for job: ASP

Enter value for deptno: 2

Enter value for sal: 12000

old 1: insert into emp values(&empno,'&ename','&job',&deptno,&sal)

new 1: insert into emp values(2,'Arjun','ASP',2,12000)

1 row created.

SQL> / Enter value for empno: 3

Enter value for ename: Gugan

Enter value for job: ASP

Enter value for deptno: 1

Enter value for sal: 12000

old 1: insert into emp values(&empno,'&ename','&job',&deptno,&sal)

new 1: insert into emp values(3,'Gugan','ASP',1,12000)

1 row created.

Q3: Update the emp table to set the salary of all employees to Rs15000/- who are working as
ASP

Ans: SQL> select * from emp;

EMPNO ENAME JOB DEPTNO SAL

---------- -------------------- ------------- ---------- ----------

1 Mathi AP 1 10000

2 Arjun ASP 2 12000

3 Gugan ASP 1 12000


SQL> update emp set sal=15000 where job='ASP';
2 rows updated.

SQL> select * from emp;

EMPNO ENAME JOB DEPTNO SAL

---------- -------------------- ------------- ---------- ----------

1 Mathi AP 1 10000

2 Arjun ASP 2 15000

3 Gugan ASP 1 15000

Q4: Create a pseudo table employee with the same structure as the table emp and insert rows
into the table using select clauses.

Ans: SQL> create table employee as select * from emp;

Table created.

SQL>desc employee;

Name Null? Type

----------------------------------------- -------- ----------------------------

EMPNO NUMBER(6)

ENAME NOT NULL VARCHAR2(20)

JOB NOT NULL VARCHAR2(13)

DEPTNO NUMBER(3)

SAL NUMBER(7,2)

Q5: select employee name, job from the emp table

Ans: SQL> select ename, job from emp;


ENAME JOB
-------------------- -------------
Mathi AP
Arjun ASP
Gugan ASP
Karthik Prof
Akalya AP
sureshlect
6 rows selected.
Q6: Delete only those who are working as lecturer
Ans: SQL> select * from emp;
EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ------------- ---------- ----------
1 Mathi AP 1 10000
2 Arjun ASP 2 15000
3 Gugan ASP 1 15000
4 Karthik Prof 2 30000
5 Akalya AP 1 10000
6 suresh lect 1 8000
6 rows selected.
SQL> delete from emp where job='lect';
1 row deleted.
SQL> select * from emp;
EMPNO ENAME JOB DEPTNO SAL
- --------- -------------------- ------------- ---------- ----------
1 Mathi AP 1 10000
2 Arjun ASP 2 15000
3 Gugan ASP 1 15000
4 Karthik Prof 2 30000
5 Akalya AP 1 10000

Q7: List the records in the emp table orderby salary in ascending
order.
Ans: SQL> select * from emp orderby sal;
EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ------------- ---------- ----------
1 Mathi AP 1 10000
5 Akalya AP 1 10000
2 Arjun ASP 2 15000
3 Gugan ASP 1 15000
4 Karthik Prof 2 30000

Q8: List the records in the emp table orderby salary in descending
order.
Ans: SQL> select * from emp order by sal desc;
EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ------------- ---------- ----------
4 Karthik Prof 2 30000
2 Arjun ASP 2 15000
3 Gugan ASP 1 15000
1 Mathi AP 1 10000
5 Akalya AP 1 10000

Q9: Display only those employees whose deptno is 1.


Solution: Use SELECT FROM WHERE syntax.

Ans: SQL> select * from emp where deptno=1;


EMPNO ENAME JOB DEPTNO SAL
---------- -------------------- ------------- ---------- ----------
1 Mathi AP 1 10000
3 Gugan ASP 1 15000
5 Akalya AP 1 10000

Q10: Display deptno from the table employee avoiding the duplicated
values.
Solution:
1. Use SELECT FROM syntax.
2.Select should include distinct clause for the deptno.
Ans: SQL> select distinct deptno from emp;
DEPTNO
----------
1

TCL COMMAND

COMMIT: command is used to save the Records.

ROLL BACK: command is used to undo the Records.


SAVE POINT command is used to undo the Records in a particular
transaction.

Queries:

Tables Used: Consider the following tables namely


“DEPARTMENTS” and “EMPLOYEES”

Their schemas are as follows , Departments ( dept _no , dept_ name


, dept_location ); Employees ( emp_id , emp_name , emp_salary );

Q1: Develop a query to grant all privileges of employees table into


departments table
Ans: SQL> Grant all on employees to

departments;

Grant succeeded.

Q2: Develop a query to grant some privileges of employees table into


departments table
Ans: SQL> Grant select, update , insert on departments to departments
with grant option;
Grant succeeded.

Q3: Develop a query to revoke all privileges of employees table from


departments table
Ans: SQL> Revoke all on employees from departments;
Revoke succeeded.

Q4: Develop a query to revoke some privileges of employees table


from departments table
Ans: SQL> Revoke select, update , insert on departments

from departments; Revoke succeeded.


Q5: Write a query to implement the save point

Ans: SQL> SAVEPOINT S1;

Savepoint created.

SQL> select * from emp;

EMPNO ENAME JOB DEPTNO SAL

---------- -------------------- ------------- ---------- ----------

1 Mathi AP 1 10000

2 Arjun ASP 2 15000

3 Gugan ASP 1 15000

4 Karthik Prof 2 30000

SQL> INSERT INTO EMP VALUES(5,'Raja','AP',3,12000);

1 row created.

SQL> select * from emp;

EMPNO ENAME JOB DEPTNO SAL

---------- -------------------- ------------- ---------- ----------

1 Mathi AP 1 10000

2 Arjun ASP 2 15000

3 Gugan ASP 1 15000

4 Karthik Prof 2 30000

5 Raja AP 3 12000

Q6: Write a query to implement the rollback


Ans: SQL> rollback s1;
SQL> select * from emp;

EMPNO ENAME JOB DEPTNO SAL

---------- -------------------- ------------- ---------- ----------

1 Mathi AP 1 10000

2 Arjun ASP 2 15000

3 Gugan ASP 1 15000

4 Karthik Prof 2 30000

Q6: Write a query to implement the commit

Ans: SQL> COMMIT;

Commit complete.

RESULT
Thus the DML, DCL,TCL commands was performed successfully and
executed.
EX.NO: 3 EMPLOYEE DATABASE
DATE:

AIM:

To create a table named employ and perform required query operations.

ALGORITHM:

Step 1 : Start the process.

Step 2 : Create a table named EMPLOYEE with necessary details.

Step 3 : Insert the values to the table employee.

Step 4 : Create a table named DEPARTMENT with necessary details.

Step 5 : Insert the values to the table department.

Step 6 : Perform all the queries for the given conditions.

Step 7 : Display all the queries for the given conditions.

Step 8 : Display the output.

Step 9 : Stop the process.


CODING:

SQL> create table itbprg1(empnovarchar(6) constraint pk_empno primary


key,empnamevarchar(30),addrvarchar(25),desigvarchar(15),dob date,gender char(1) constraint chk_gen
check(gender<>null),dojdate,salary number(10,2));

Table created.

SQL>insert into itbprg1 values('101','aaa','rk street','worker','10-jun-81','M','12-jun-2013',15000);

1 row created.

SQL> insert into itbprg1 values('102','bbb','tg nagar','manager','4-aug-1974','M','22-jul-2012',30000);

1 row created.

SQL>insert into itbprg1 values('103','ccc','ui street','clerk','13-feb-1977','F','03-mar-2010',8000);

1 row created.

SQL> select empname from itbprg1 where salary>10000 ;

EMPNAME
------------------------------
aaa
bbb

SQL> select * from itbprg1 order by empno;

EMPNO EMPNAME ADDR DESIG DOB GENDER DOJ SALARY


----- ------------------------------ ------------------------- --------------- --------- ---------- ---------
101 aaa rk streetworker 10-JUN-81 M 12-JUN-13 15000
102 bbb tg nagarmanager 04-AUG-74 M22-JUL-12 30000
103 cccui street clerk 13-FEB-77 F 03-MAR-10 8000

SQL> select * from itbprg1 where salary in(select max(salary) from itbprg1);

EMPNO EMPNAME ADDR DESIG DOB GENDER DOJ


SALARY
----------- ---------------------- ------------------ --------------- ------------- ------------- ------------ --------------
102 bbb tg nagar manager 04-AUG-74 M22-JUL-12 30000

SQL> select empname from itbprg1 where salary>(select salary from itbprg1 where
empname='ravi');

EMPNAME
bbb

RESULT:

Thus the Employee database has been executed successfully.


EX.NO:4 STUDENT DATABASE
DATE:

AIM:

To create a student detail table and perform required query operations.

ALGORITHM:

Step 1 : Start the process.

Step 2 : Create a student detail table with necessary fields.

Step 3 : Insert the values to the student table.

Step 4 : Perform all the queries for the given conditions.

Step 5 : Display all the queries for the given conditions.

Step 6 : Display the output.

Step 7 : Stop the process.


CODING:

SQL> create table student(stunamevarchar(15),gender varchar(6),rollnovarchar(10),deptvarchar(15),


addrvarchar(25),percent number(4,2));

Table created.

SQL> insert into student values('rahul','m','12ca14','it','tg nagar',75.5);


1 row created.
SQL> insert into student values('amutha','f','12it01','it','as street',95)
1 row created.
SQL> insert into student values('radhika','f','12it10','it','kl nagar',89.4)
1 row created.
SQL>insert into student values('raju','m','12ca34','it','rd nagar',65)
1 row created.

SQL> select avg(percent) from student;

AVG(PERCENT)
----------------
86.633333

SQL> select stuname from student where percent>80;

STUNAME
---------------
amutha
radhika

SQL> select * from student where percent=(select max(percent) from student);

STUNAME GENDER ROLLNO DEPT ADDR PERCENT


-----------------------------------------------------------------------------------------------------------
amutha f 12it01 it as street 95

SQL> select * from student where percent between 50 and 70;

STUNAME GENDER ROLLNO DEPT ADDR PERCENT


-----------------------------------------------------------------------------------------------------------
raju m 12ca34 it rdnagar 65

SQL> select * from student where percent>(select percent from student where rollno='12ca34');
STUNAME GENDER ROLLNO DEPT ADDR PERCENT
-----------------------------------------------------------------------------------------------------------
rahul m 12ca14 it tgnagar 75.5
amutha f 12it01 it as street 95
radhika f 12it10 it kl nagar 89.4

RESULT:

Thus the Student database has been executed successfully.


EX NO:5 COLLEGE DATABASE
DATE:

AIM:

To create staff and department detail tables and perform required query operation.

ALGORITHM:

Step 1: Start the process.

Step 2: Create staff and department detail tables with necessary fields.

Step 3: Insert the values into the two tables.

Step 4: Perform all the queries for given conditions.

Step 5: Display all the queries for the given conditions.

Step 6: Display the output.

Step 7: Stop the process.


CODING:

SQL> create table dept(deptcodevarchar(4) primary key, deptnamevarchar(30));

Table created.

SQL> create table staff(staffnovarchar(6) primary key, staffnamevarchar(30),doj date, deptcodevarchar(4)


references itbdept(deptcode), desigvarchar(15),basic number(7,2));

Table created.

SQL>descdept;
Name Null? Type
-----------------------------------------------------------------
DEPTCODE NOT NULL VARCHAR2(4)
DEPTNAME VARCHAR2(30)

SQL> insert into deptvalues('d101','aeronautical');


1 row created.

SQL> insert into deptvalues('d102','cse');


1 row created.

SQL> insert into deptvalues('d103','ECE');


1 row created.

SQL> insert into deptvalues('d104','IT');


1 row created.

SQL>insert into deptvalues('d105','Mechanical');


1 row created.

SQL>select * from dept;

DEPT DEPTNAME
---- ------------------------------
d101 aeronautical
d102 cse
d103 ECE
d104 IT
d105 Mechanical

SQL>desc staff;
Name Null? Type
----------------------------------------------------------------------------
STAFFNO NOT NULL VARCHAR2(6)
STAFFNAME VARCHAR2(30)
DOB DATE
DEPTCODE VARCHAR2(4)
DESIG VARCHAR2(15)
BASIC NUMBER(7,2)
DOJ DATE
SQL> insert into staff values('s101','arjun','15-mar-73','d101','professor',25000);
1 row created.

SQL> insert into staff values('s102','ramesh','23-jun-75','d102','Professor',25000);


1 row created.

SQL> insert into staff values('s202','Dharun','12-feb-81','d102','Lecturer',12000);


1 row created.

SQL> insert into staff values('s103','Prabhavathy','15-dec-82','d103','Lecturer',13000);


1 row created.

SQL> insert into staff values('s104','Vasu','13-dec-84','d104','Lecturer',14000);


1 row created.

SQL>insert into staff values('s105','dinesh','23-apr-84','d104','Lecturer',14000);


1 row created.

SQL> insert into staff values('s106','Surya','23-aug-85','d105','Lecturer',15000);


1 row created.

SQL> select * from staff;

STAFFNO STAFFNAME DOB DEPT DESIG BASIC DOJ


-----------------------------------------------------------------------------------------------------------
s101 arjun 15-MAR-73 d101 Professor 25000 23-JUN-96
s102 ramesh 23-JUN-75 d102 Professor 25000 15-JUN-96
s202 Dharun 12-FEB-81 d102 Lecturer 12000 12-FEB-12
s103 Prabhavathy 15-DEC-82 d103 Lecturer 13000 13-DEC-11
s104 Vasu 13-DEC-84 d104 Lecturer 14000 13-DEC-11
s105 dinesh 23-APR-84 d104 Lecturer 14000 12-JAN-12
s106 Surya 23-AUG-85 d105 Lecturer 15000 26-JUL-12

7 rows selected
.

SQL> Select doj,staffname from staff where doj<sysdate-730;

DOJ STAFFNAME
--------- ------------------------------
12-FEB-12 Dharun
13-DEC-11 Prabhavathy
13-DEC-11 Vasu
12-JAN-12 dinesh
26-JUL-12 Surya

SQL> select * from staff,dept where staff.deptcode=dept.deptcode and dept.deptname='cse’

STAFFNO STAFFNAME DOB DEPT DESIG BASIC DOJ DEPT


DEPTNAME
----------------------------------------------------------------------------------------------------------------------------------
---
s102 ramesh 23-JUN-75 d102 Professor 25000 15-JUN-96 d102 cse
s202Dharun 12-FEB-81 d102 Lecturer 12000 12-FEB-12 d102 cse

SQL> select staffname,deptname from staff,dept where staff.deptcode =dept.deptcode;

STAFFNAME DEPTNAME
------------------------------ ------------------------------
arjun aeronautical
ramesh cse
Dharun cse
Prabhavathy ECE
Vasu IT
dinesh IT
Surya Mechanical

7 rows selected.

SQL> select max(basic),min(basic),deptcode from staff group by deptcode;

MAX(BASIC) MIN(BASIC) DEPT


--------------------------------------------
25000 25000 d101
25000 12000 d102
13000 13000 d103
14000 14000 d104
15000 15000 d105

SQL> select deptcode,sum(basic) from staff group by deptcode;

DEPT SUM(BASIC)
--------------------------
d101 25000
d102 37000
d103 13000
d104 28000
d105 15000

SQL> select * from staff where basic>(select avg(basic) from staff);

STAFFN STAFFNAME DOJ DEPT DESIG BASIC DOJ


--------------------------------------------------------------------------------------------------------------
s101 arjun 15-MAR-73 d101 professor 25000 23-JUN-96
s102 ramesh 23-JUN-75 d102 Professor 25000 15-JUN-96

RESULT:

Thus the College database has been executed successfully.


EX NO:6 BANK DATABASE
DATE:

AIM:

To create account and borrower details tables and perform required query operation.

ALGORITHM:

Step 1 : Start the process.

Step 2 : Create account detail table with necessary fields.

Step 3 : Insert the values into the table.

Step 4 : Create borrower detail table with necessary fields.

Step 5 : Insert the values into the table.

Step 6 : Perform all the queries for given conditions.

Step 7 : Display all the queries for the given conditions.

Step 8 : Display the output.

Step 9 : Stop the process.


CODING:
SQL> create table acc1(acc_no number(4) primary key,cust_name char(30),br_name char(30),city
char(30));
Table created.

SQL> insert into acc1 values(1001,'Raja','ganapathy','coimbatore')


1 row created.
SQL> insert into acc1 values(1002,'Ramu','sidco','coimbatore')
1 row created.
SQL> insert into acc1 values(1003,'Sudha','gandhipuram','coimbatore')
1 row created.
SQL> insert into acc1 values(1004,'Kalpana','seerapalayam','coimbatore')
1 row created.
SQL> insert into acc1 values(1005,'Rajeswari','Singanallur','coimbatore')
1 row created.

SQL>select * from acc1;


ACC_NO CUST_NAME BR_NAME CITY
-----------------------------------------------------------------------
1001 Raja ganapathy coimbatore
1002 Ramusidco coimbatore
1003 Sudhagandhipuram coimbatore
1004 Kalpana seerapalayam coimbatore
1005 Rajeswari Singanallur coimbatore

SQL> create table bor1(acc_no number(4) constraint fk_ac1 references acc1(acc_no), amount number(8,2));
Table created.
SQL>insert into bor1 values(1001,25000)
1 row created.
SQL> insert into bor1 values(1002,50000)
1 row created.
SQL>insert into bor1 values(1003,36000)
1 row created.
SQL>insert into bor1 values(1004,70000)
1 row created.

Equi-join
SQL> select acc1.acc_no,cust_name,br_name,city,amount from acc1,bor1
where acc1.acc_no=bor1.acc_no;

ACC_NO CUST_NAME BR_NAME CITY AMOUNT


-------------------------------------------------------------------------------------------------
1001 Raja ganapathy coimbatore 25000
1002 Ramu sidco coimbatore 50000
1003 Sudhagandhipuram coimbatore 36000
1004 Kalpana seerapalayam coimbatore 70000

Non-equi join

SQL>select acc1.acc_no,cust_name,br_name,city,amount from acc1,bor1


where acc1.acc_no=bor1.acc_no and bor1.amount>30000;

ACC_NO CUST_NAME BR_NAME CITY AMOUNT


-------------------------------------------------------------------------------------------------
1002 Ramu sidco coimbatore 50000
1003 Sudhagandhipuram coimbatore 36000
1004 Kalpanaseerapalayam coimbatore 70000

Outer-join

SQL>select acc1.acc_no,cust_name,br_name,city,amount from acc1,bor1


where bor1.acc_no (+)=acc1.acc_no;

ACC_NO CUST_NAME BR_NAME CITY AMOUNT


-------------------------------------------------------------------------------------------------
1001 Raja ganapathy coimbatore
1002 Ramusidco coimbatore
1003 Sudhagandhipuram coimbatore
1004 Kalpana seerapalayam coimbatore
1005 Rajeswari Singanallur coimbatore

Self-join

SQL>select a.acc_no, a.amount from bor1 a,bor1 b where a.amount>b.amount;

ACC_NO AMOUNT
--------- ----------------
1002 25000
1003 25000
1004 25000
1004 50000
1002 36000
1004 36000

SQL>select * from acc1;

ACC_NO CUST_NAME BR_NAME CITY AMOUNT


-------------------------------------------------------------------------------------------------
1001 Raja ganapathy coimbatore
1002 Ramusidco coimbatore
1003 Sudhagandhipuram coimbatore
1004 Kalpana seerapalayam coimbatore
1005 Rajeswari Singanallur coimbatore

SQL>select * from bor1;

ACC_NO AMOUNT
--------- ---------
1001 25000
1002 50000
1003 36000
1004 70000

RESULT:

Thus the Bank database has been executed successfully.


EX NO: 7 FACTORIAL AND FIBONACCI SERIES
DATE:

AIM:

To find the factorial of a given number using PL/SQL.

ALGORITHM:

Step 1 : Start the program


Step 2 : Declare variables i , fact, num
Step 3 : Initialize i and fact to 1.
Step 4 : Accept value for num.
Step 5 : Calculate factorial as fact = fact*i
Step 6 : Increment i by 1. Repeat step 4 till i is less than or equal to num
Step 7 : Display fact
Step 8 : Declare variables a ,b, c,num
Step 9 : Initialize a to 1 and b to 0.
Step 10: Calculate c = a + b, a=b, b=c
Step 11: Display c as Fibonacci value
Step 12 : Increment i by 1. Repeat step 10 till i is less than or equal to num -1
Step 13: Stop the program
FACTORIAL

SQL> declare
2 fact number:=1;
3 num number:=&num;
4 i number;
5 begin
6 for i in 1 ..num loop
7 fact:=fact*i;
8 end loop;
9 dbms_output.put_line(fact);
10 end;
11 /
Enter value for num: 5
old 3: num number:=&num;
new 3: num number:=5;

120

PL/SQL procedure successfully completed.


FIBONACCI SERIES

SQL> set serveroutput on


SQL> declare
2 a number:=1;
3 b number:=0;
4 c number;
5 num number:=&num;
6 begin
7 dbms_output.put_line(b);
8 for i in 1..num-1 loop
9 c:=a+b;
10 a:=b;
11 b:=c;
12 dbms_output.put_line(c);
13 end loop;
14 end;
15 /
Enter value for num: 5
old 5: num number:=&num;
new 5: num number:=5;
0
1
1
2
3

PL/SQL procedure successfully completed.

RESULT:

Thus the Factorial and Fibonacci of given number has been executed successfully in PL/SQL.
EX.NO:8 PALINDROMEAND NUMBER REVERSE
DATE:

AIM:

To check whether the given string is palindrome or not.

ALGORITHM:

Step 1: Start the program.


Step 2: Declare the necessary variables.
Step 3: Find the length of the given string.
Step 4: Find the reverse of the string using loop.
Step 5: Check whether the reverse of the string is equal to given string .
Step 6: If it is equal print it is a palindrome
Step 7: Else print it is not a palindrome.
Step8: Stop the process.
CODING:
sql> declare
2 len number;
3 str varchar2(20) := '&input_string';
4 chkstr varchar2(20);
5 begin
6 len := length(str);
7 for i in reverse 1..len loop
8 chkstr := chkstr||substr(str,i,1);
9 end loop;
10 if chkstr = str then
11 dbms_output.put_line(str||' is a palindrome!');
12 else
13 dbms_output.put_line(str||' is not a palindrome!');
14 end if;
15 end;
16 /

enter value for input_string: madam


old 3: str varchar2(20) := '&input_string';
new 3: str varchar2(20) := 'madam';
madam is a palindrome!

pl/sql procedure successfully completed.


REVERSING NUMBER

AIM:

To perform the reverse of a given number.

ALGORITHM:

Step 1 : Start the program.


Step 2 : Declare the necessary variables.
Step 3 : Get the input for number.
Step 4 : Display the value of a.
Step 5 : Extract individual digits from the number
Step 6 : Join the digits and save it in another variable.
Step 7 : Print the reverse of the number.
Step8 : Stop the process.
CODING:

sql> declare
2 no integer;
3 rev integer:=0;
4 begin
5 no:=&no;
6 while (no>0) loop
7 rev:=rev*10+mod(no,10);
8 no:=trunc(no/10);
9 end loop;
10 dbms_output.put_line('reverse no. is'||' : '||rev);
11 end;
12 /

enter value for no: 352


old 5: no:=&no;
new 5: no:=352;
reverse no. is : 253

pl/sql procedure successfully completed.

RESULT:

Thus the Palindrome and Number Reverse of given number has been executed successfully in
PL/SQL.
EX. NO: 9 EXCEPTION HANDLING
DATE:

AIM:
To generate a user defined exception in the user table using PL/SQL.

ALGORITHM:
Step 1: Start the process.
Step 2: Create the table master with the given fields.
Step 3: Insert the value for all the fields.
Step 4: Declare an exception zero_balance and a variable balance.
Step 5: Assign the value of balance into price.
Step 6: Check if price=0. If true then transfer the control to exception part.
Step 7: Execute the statements in exception part.
Step 8: Print an exception information.
Step 9: Stop the process.
SQL> create table master(client_id number(6), client_name varchar2(30), addr varchar2(50), phone
number(10), balance number(10,2));
Table created.
SQL>desc master;

Name Null? Type


------------------------------------------------------------------
CLIENT_ID NUMBER(6)
CLIENT_NAME VARCHAR2(30)
ADDR VARCHAR2(50)
PHONE NUMBER(10)
BALANCE NUMBER(10,2)

SQL> insert into master values(101,'soundi','45ns nagar',2345,0);


1 row created.

SQL> insert into master values(102,'sangi','68gk nagar',5674,2000);


1 row created.

SQL> insert into master values(103,'maha','89mm street',8976,8000);


1 row created.

SQL>select * from master;

CLIENT_ID CLIENT_NAME ADDR PHONE BALANCE


-----------------------------------------------------------------------------------------------
101 soundarya 45ns nagar 2345 0
102 sangeetha 68gk nagar 5674 2000
103 mahalakshmi 89mm street 8976 8000

SQL> setserveroutput on
SQL> declare
2 zerobal exception;
3 bal number (8,2);
4 begin
5 select balance into bal from master where client_id=101;
6 if bal=0 then
7 raise zerobal;
8 end if;
9 exception
10 when zerobal then
11 dbms_output.put_line ('client cannot have zero balance');
12 end;
13 /
client cannot have zero balance
PL/SQL procedure successfully completed

RESULT:

Thus the Exception Handling has been executed successfully in PL/SQL.


EX. NO: 10 TRIGGER CREATION : RECORD INSERTION AND DELETION
DATE:

AIM:
To create a table and to create trigger to be fired when the record is deleted and inserted.

ALGORITHM:
Step 1: Start the process.
Step 2: Create tables product and vendor.
Step 3: Insert the values for the fields of the two tables.
Step 4: Create a trigger to be fired when the record from product table is deleted.
Step 5: Display an error message if the record is deleted on a particular day.
Step 6: Create a trigger to be fired when the record in vendor table is inserted
Step 7: Display a message if a record is inserted in vendor table.
Step 8: Stop the process.
SQL>create table product(product_code varchar2(7) constraint cpp primary key, product_name
varchar2(30),price number(6,2),quantity number(4));
Table created.

SQL>desc product;

Name Null? Type


------------------ -------- -------------------------
PRODUCT_CODE NOT NULL VARCHAR2(7)
PRODUCT_NAME VARCHAR2(30)
PRICE NUMBER(6,2)
QUANTITY NUMBER(4)

SQL> create table vendor(vendor_name varchar2(30), vendor_address varchar2(30), product_code


varchar2(7) product(product_code));
Table created.

SQL>descsvendor;

Name Null? Type


------------------ -------- -----------------
VENDOR_NAME VARCHAR2(30)
VENDOR_ADDRESS VARCHAR2(30)
PRODUCT_CODE VARCHAR2(7)

SQL> insert into product values(101,'Lux',25,2);


1 row created.

SQL> insert into product values(103,'Tide',60,1);


1 row created.

SQL> insert into product values(112,'Colgate',35,1);


1 row created.

SQL> insert into product values(102,'All Out',45,1);


1 row created.

SQL> insert into product values(123,'Vim',15,3);


1 row created.

SQL> insert into product values(113,'Ponds',48,1);


1 row created.

SQL>select * from product;

PRODUCT PRODUCT_NAME PRICE QUANTITY


------- ------------------ ---------- ----------
101 Lux 25 2
103 Tide 60 1
112 Colgate 35 1
102 All Out 45 1
123 Vim 15 3
113 Ponds 48 1
6 rows selected.

SQL> insert into vendor values('Kannan','Raja street',101);


1 row created.

SQL> insert into vendor values('Anitha','RS Puram',103);


1 row created.

SQL> insert into vendor values('Geetha','DB Road',112);


1 row created.

SQL> insert into vendor values('Raju','5 Corner',102);


1 row created.

SQL> insert into vendor values('Rahul','DB Road',123);


1 row created.

SQL> insert into vendor values('Sakshi','RK Road',113);


1 row created.

SQL>select * from vendor;

VENDOR_NAME VENDOR_ADDRESS PRODUCT


------------------------ ------------------- -------
Kannan Raja street 101
Anitha RS Puram 103
Geetha DB Road 112
Raju 5 Corner 102
Rahul DB Road 123
Sakshi RK Road 113
6 rows selected.
Trigger to prevent Deletion

SQL> set serveroutput on;


SQL> create trigger delete_trigger before delete on product
begin
raise_application_error(-20050,'Record cannot be deleted from product table')
end;
/
Trigger created.

Output:
SQL> delete from product where product_code='101';
delete from product where product_code='101'
*
ERROR at line 1:
ORA-20050: Record cannot be deleted from product table
ORA-06512: at "SYSTEM.DELETE_TRIGGER", line 2
ORA-04088: error during execution of trigger 'SYSTEM.DELETE_TRIGGER'

Trigger to insert a record

SQL> set serveroutput on


SQL> create trigger insert_trigger before insert on vendor
begin
dbms_output.put_line('Record inserted successfully into table vendor');
end;
/
Trigger created.

Output:
SQL> insert into vendor values('Anu','DB Road','112');
Record inserted successfully into table vendor

RESULT:

Thus the Trigger creation has been executed successfully in PL/SQL.


EX.NO:11 TRIGGER CREATION : RECORD UPDATION
DATE:

AIM:

To create a table named voter list and new list to perform required query operation.

ALGORITHM:

Step1: Start the process.

Step2: Create a table named voter list with necessary details.

Step3: Create a table named new list with necessary details

Step4: Insert the values into table named as ab and cd.

Step5: perform all the queries for a given conditions.

Step6: Display all the queries for the given conditions.

Step7: Display the output.

Step8: Stop the process.


SQL> create table ab(voter_id number(5) constraint ovid primary key, name varchar2(30),ward_no
number(4) constraint chk_wno check(ward_no<>null),dob date,addr varchar2(15));

Table created.

SQL> create table cd(voter_id number(5),ward_no number(4),name varchar2(30),descri char(50));

Table created.

SQL> insert into abvalues(101,'soundarya',234,'12-jun-93','34 annanagar');


1 row created.

SQL> insert into abvalues(102,'sangeetha',567,'18-mar-90','35 ssstreet');


1 row created.

SQL> insert into abvalues(103,'pooja',678,'8-jan-89','87 krnagar');


1 row created.

SQL> insert into cd values(101,234,'soundarya','exist');


1 row created.

SQL> insert into cd values(102,567,'sangeetha','exist');


1 row created.

SQL> insert into cd values(103,678,'pooja','exist');


1 row created.

Trigger to perform deletion

SQL> create or replace trigger v_trig after delete on ab for each row
2 declare
3 vid number(5);
4 begin
5 vid:=:old.voter_id;
6 update cd set descri='shifted' where voter_id=vid;
7 end;
8 /

Trigger created.

SQL>delete from ab where voter_id='101';

1 row deleted.

SQL>select * from cd;

VOTER_ID WARD_NO NAME DESCRI


---------------------------------------------------------------------
101 234 soundarya shifted
102 567 sangeetha exist
103 678 pooja exist
SQL>select * from ab;

VOTER_ID NAME WARD_NO DOB ADDR


-------------------------------------------------------------------------------------
102 sang 567 18-MAR-90 35 ssstreet
103 pooja 678 08-JAN-89 87 krnagar

RESULT:

Thus the Trigger updated been executed successfully in PL/SQL.


EX.NO:12 CURSOR CREATION : RECORD UPDATION

DATE:

AIM:

To create table to store salary details of the employee and use cursor to update the employee details using
PL/SQL.

ALGORITHM:

Step 1: Start the program.

Step 2: Create a table salary with necessary fields.

Step 3: Insert the values for the field in the table.

Step 4: Create a cursor to store the employee details.

Step 5: Calculate and update the employee’s net pay using the cursor.

Step 6: Display the payslip for the employee.

Step 7: Stop the process.


SQL> create table emp_salary(Emp_no number(4) Primary Key,Emp_name varchar2(30),Designation
varchar2(25),Dept varchar2(30),Basic number(5));

Table created.

SQL>descemp_salary;
Name Null? Type
--------------------- -------- --------------------
EMP_NO NOT NULL NUMBER(4)
EMP_NAME VARCHAR2(30)
DESIGNATION VARCHAR2(25)
DEPT VARCHAR2(30)
BASIC NUMBER(5)

SQL> insert into emp_salaryvalues(202,'Abi','Tester','Test Bench',2000);


1 row created.

SQL> insert into emp_salary values (203,'Bharathi','Designer', 'Design',1500);


1 row created.

SQL> insert into emp_salaryvalues(204,'Azar','Analyst', 'Development',2500);


1 row created.

SQL>select * from emp_salary;

EMP_NO EMP_NAME DESIGNATION DEPT BASIC


------- ------------ ------------- -------------------------- --------- -------------------
202 Abi Tester Test Bench 2000
203 Bharathi Designer Design 1500
204 Azar Analyst Development 2500

SQL>alter table emp_salary add Net_paynumber(5);


Table altered.

SQL>set serveroutput on
SQL>declare
2 netpay integer;
3 hra integer;
4 da integer;
5 pf integer;
6 detec integer;
7 allow integer;
8 basic integer;
9 cursor cursor_salary is select * from emp_salary;
10 salemp_salary%rowtype;
11 begin
12 open cursor_salary;
13 dbms_output.put_line('enoenamedeptdesigpfgross_paynet_pay');
14 loop
15 fetch cursor_salary into sal;
16 exit when cursor_salary%notfound;
17 hra:=sal.basic*(5/100);
18 da:=sal.basic*(13/100);
19 pf:=sal.basic*(20/100);
20 allow:=sal.basic+hra+da;
21 detec:=pf;
22 netpay:=allow-detec;
23 update emp_salary set Net_pay = netpay where
Emp_no=sal.Emp_no;
dbms_output.put_line(sal.emp_no||','||sal.emp_name||','||sal.dept||','||
sal.designation||’,’||allow||','||pf||','||netpay);
24 end loop;
25 close cursor_salary;
26 end;
27 /

OUTPUT:
eno ename dept desig pf gross_pay net_pay
202, Abi, Test Bench, Tester, 2360, 400, 1960
203,Bharathi,Design, Designer,1770, 300, 1470
204,Azar, Development, Analyst,2950, 500, 2450

PL/SQL procedure successfully completed.

SQL>select * from emp_salary;

EMP_NO EMP_NAME DESIGNATION DEPT BASIC NET_PAY


------- --------- ------------ ---------- ------ ------- --------- ----------------- ------------ ----
202 Abi Tester Test Bench 2000 1960
203 Bharathi Designer Design 1500 1470
204 Azar Analyst Development 2500 2450

RESULT:

Thus the Cursor created has been executed successfully in PL/SQL.


EX. NO:13 PROCEDURE CREATION : RECORD SEEK AND DELETE
DATE:

AIM:

To create a procedure and check if the date of the last purchase is greater than 1 year from the current date
using PL/SQL.

ALGORITHM:

Step 1: Start the process.

Step 2: Create a table named stock with necessary details

Step 3: Create a procedure check_stock with arguments item_id and new_purchase.

Step 4: If last_purchase date for the given item_id is greater than 1 year delete the item

Step 5: If date is less than 1 year update the stock with the new value.

Step 6: Display the details of the stock table to view the updated details.

Step 7: Stop the process.


SQL> create table stock(item_code varchar2(10) primary key,item_name varchar2(50),current_stock
number(5), last_purchase date);
Table created.

SQL> insert into stock values('pr101','Soap',30,'12-jun-16');


1 row created.

SQL> insert into stock values('pr102','Oil',30,'15-dec-15');


1 row created.

SQL> insert into stock values('pr112','Powder',25,'17-jan-17');


1 row created.

SQL> insert into stock values('pr123','Shampoo',15,'23-may-16');


1 row created.

SQL> insert into stock values('pr121','Cream',45,'03-apr-16');


1 row created.

SQL>desc stock;

Name Null? Type


------------------- -------- ------------------
ITEM_CODE NOT NULL VARCHAR2(10)
ITEM_NAME VARCHAR2(50)
CURRENT_STOCK NUMBER(5)
LAST_PURCHASE DATE

SQL>select * from stock;

ITEM_CODE ITEM_NAME CURRENT_STOCK LAST_PURC


--------- ---------- --------------- ------------------------ ----------------------
pr101 Soap 30 12-JUN-16
pr102 Oil 30 15-DEC-15
pr112 Powder 25 17-JAN-17
pr123 Shampoo 15 23-MAY-16
pr121 Cream 45 03-APR-16

Procedure for record deletion

SQL> create or replace procedure checkstock(item_id in char,new_purchase number)as


begin
delete from stock where item_code=item_id
andmonths_between(sysdate, last_purchase)>=12;
update stock set current_stock=current_stock+new_purchase
whereitem_code=item_id;
end;
/
Procedure created.

OUTPUT:

SQL> exec checkstock('pr102',20);


PL/SQL procedure successfully completed.

SQL> exec checkstock('pr112',25);


PL/SQL procedure successfully completed.

SQL>select * from stock;

ITEM_CODE ITEM_NAME CURRENT_STOCK LAST_PURC


--------- ---------- --------------- -------------------------------------------------
pr101 Soap 30 12-JUN-16
pr112 Powder 50 17-JAN-17
pr123 Shampoo 15 23-MAY-16
pr121 Cream 45 03-APR-16

RESULT:

Thus the Procedure creation and seeking record has been executed successfully in PL/SQL.
EX. NO: 14 FUNCTION CREATION : RECORD SEEK
DATE:

AIM:

To create a function and to search for address using phone number using PL/SQL.

ALGORITHM:

Step 1: Start the process.

Step 2: To create a table phone book with given fields such as phone no, user_
name, door no, street, city and pin code.

Step 3: Insert the values for the table.

Step 4: Create a function search_address

Step 5: Pass the phone number as argument and find the address

Step 6: Display the result.

Step 7: Stop the process.


SQL> create table phone(Ph_no number(7),User_namevarchar(30),Address varchar(30));
Table created.

SQL>desc phone;

Name Null? Type


-------------------- ---------- ------------------

PH_NO NOT NULL NUMBER(10)


USER_NAME VARCHAR2(30)
ADDRESS VARCHAR2(30)

SQL> insert into phone values(9800899090,'Shalini','RS Puram, CBE');


1 row created.

SQL> insert into phone values(9000080000,'Nandhu', 'TNagar,CHE');


1 row created.

SQL> insert into phone values(7890123456,'Amirtha','DB Road, CBE');


1 row created.

SQL>Select * from phone;

PH_NO USER_NAME ADDRESS


---------- -------------------- ---------------------
9800899090 Shalini RS Puram,CBE
9000080000 NandhuTNagar,CHE
7890123456 Amirtha DB Road,CBE

Function to search address

SQL>create or replace function searchaddress(ph in number) return varchar asadds varchar(30);


begin
selectUser_name||','||Address into adds from phone
wherePh_no= ph;
return adds;
exception
whenno_data_found then return 'Address not Found';
end;
/
SQL>set serveroutput on
SQL>declare
addsvarchar(30);
begin
adds:=searchaddress(9000080000);
dbms_output.put_line(adds);
end;
/

OUTPUT:

Nandhu,TNagar,CHE
PL/SQL procedure successfully completed.

RESULT:

Thus the Function creation has been executed successfully in PL/SQL.

You might also like