You are on page 1of 38

DEPARTMENT OF COMPUTER APPLICATIONS

CAE 6128 & DATABASE MANAGEMENT SYSTEMS


LABORATORY

RECORD NOTE BOOK

NAME :_________________________

RRN :_________________________

PROGRAMME :_________________________

SEMESTER & SECTION:_________________________

MONTH & YEAR :_________________________


BONAFIDE CERTIFICATE

Certified that this is the Bonafide record of the work done by

RRN: of

Master of Computer Applications during I semester for the course CAE 6128 -

Database management system Laboratory during the academic year 2023 - 2024.

Date:

Signature of Faculty-In-Charge

Submitted for the University Practical Examinations held on .


PAGE TEACHER’S
Ex.No DATE LIST OF EXPRIEMENTS No SIGN

1 Data Definition Language Commands

2 Data Manipulation Language Commands

3 Data Control Language, TCL Language Commands

4 In Built Functions

5 Nested Queries And Join Queries

6 Set operators

7 Views

8 Partitions and locks

9 Procedure and Function

10 Cursor (Implicit and explicit)

11 Exception handling
Ex No: 1
Date :

DATA DEFINITION LANGUAGE (DDL) COMMANDS

AIM:
To practice and implement data definition language commands and constraints.

Q1. Create a table called EMP with the following structure.

create table emp(empno number(6),ename varchar2(20)not null,job varchar2(10) not null,


deptno number(3),sal number(7,2));

Output:
Table created.

Q2: Add a column experience to the emp table. experience numeric null allowed.

alter table emp add(experience number(2));

Output:
Table altered.

Q3: Modify the column width of the job field of emp table.

alter table emp modify(job varchar2(12));

Output:
Table altered.

alter table emp modify(job varchar(13));

Output :
Table altered.

Q4: Create dept table with the following structure.

create table dept(deptno number(2) primary key,dname varchar2(10),loc


varchar2(10));

Output:
Table created.

1
Q5: create the emp1 table with ename and empno, add constraints to check the
empno value while entering (i.e) empno > 100.

create table emp1(ename varchar2(10),empno number(6) constraint ch


check(empno>100));

Output:
Table created.

Q6: drop a column experience to the emp table.

alter table emp drop column experience;

Output:
Table altered.

Q7: Truncate the emp table and drop the dept table

truncate table emp;

Output:
Table truncated.

drop table dept;


Output:
Table dropped.

Result:
Thus the data definition language commands was performed and executed
successfully.

2
Ex No: 2

Date :

DATA MANIPULATION LANGUAGE (DML) COMMANDS

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

Q1:Insert a single record into dept table.

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


Output:
1 row created.

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

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


Output:
1 row created.

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


Output:
1 row created.

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


Output:
1 row created.

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

SQL> select * from emp;

Output:

EMPNO ENAME JOB DEPTNO

-
1 Mathi AP 1 10000

3
2 Arjun ASP 2 12000
3 Gugan ASP 112000

update emp set sal=15000 where job='ASP';


Output:
2 rows updated.

select * from emp;

Output:

EMPNO ENAME JOB DEPTNO SAL

-
1 Mathi AP 1 10000
2 Arjun ASP 2 15000
3 Gugan ASP 115000

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

create table employee as select * from emp;


Output:
Table created.

desc employee;

Output:
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.

select ename, job from emp;

Output:
ENAME JOB
Mathi AP
Arjun ASP

4
Gugan ASP
Karthik Prof
Akalya AP
suresh lect
6 rows selected.
Q6: Delete only those who are working as lecturer.

select * from emp;

Output:

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

delete from emp where


job='lect';

Output:
1 row deleted.

select * from emp;

Output:
EMPNO ENAME JOB DEPTNO SAL

10000
1 Mathi AP 1
15000
2 Arjun ASP 2
15000
3 Gugan ASP 1
30000
4 Karthik Prof 2
10000
5 Akalya AP 1

5
Q7: List the records in the emp table orderby salary in ascending order.

select * from emp order by sal;

Output:
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.

select * from emp order by sal desc;

Output:
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 30.

select * from emp where deptno=1;

Output:

EMPNO ENAME DEPTNO SAL

1 Mathi AP 10000
3 Gugan ASP
5 Akalya AP 10000

6
Q10: Display deptno from the table employee avoiding the duplicated values.

select distinct deptno from emp;


Output:

DEPTNO

1
2

Result:

Thus the DML commands using from where clause was performed and executed successfully.

7
Ex No: 3
Date :

DATA CONTROL LANGUAGE (DCL), TRANSACTION CONTROL


LANGUAGE (TCL) COMMANDS

AIM:
To study the various data language commands (DCL, TCL) and implements them on the
database.

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.

Grant all on employees to departments;

Output:
Grant succeeded.

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

Grant select, update , insert on employees to departments with grant option;

Output:
Grant succeeded.

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

Revoke all on employees from departments;

Output:
Revoke succeeded.

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

Revoke select, update , insert on employees from departments;

Output:
Revoke succeeded.

Q5: Write a query to implement the save point

SAVEPOINT S1;

Output:
Savepoint created.

select * from emp;

Output:
EMPNO ENAME JOB DEPTNO SAL
1 MATHI AP 1 10000
2 ARJUN ASP 2 15000
3 GUGAN ASP 1 15000
4 KARTHIIK PROF 2 30000

INSERT INTO EMP VALUES(5,'Akalya','AP',1,10000);

Output:1 row created.

select * from emp;

Output:

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

9
Q6: Write a query to implement the rollback

rollback s1;
select * from emp;

Output:

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

Q7: Write a query to implement the commit

COMMIT;

Output:
Commit complete.

Result:
The DCL,TCL commands was performed and executed successfully.

10
Ex No: 4
Date :

IN BUILT FUNCTIONS

AIM:
To perform nested Queries and joining Queries using DML command.

Q1: Display all the details of the records whose employee name starts with ‘A’.

select * from emp where ename like 'A%';


Output:
EMPNO ENAME JOB DEPTNO SAL

2 Arjun ASP 2 15000


5 Akalya AP 1 10000

Q2: Display all the details of the records whose employee name does not starts with ‘A’.
select * from emp where ename not like 'A%';

Output:

EMPNO ENAME JOB DEPTNO SAL

1 Mathi AP 1 10000
3 Gugan ASP 1 15000
4 Karthik Prof 2 30000

Q3: Display the rows whose salary ranges from 15000 to 30000.

select * from emp where sal between 15000 and 30000;

Output:

EMPNO ENAME JOB DEPTNO SAL

-
2 Arjun ASP 2 15000
3 Gugan ASP 1 15000
4 Karthik Prof 2 30000

11
Q4: Calculate the total and average salary amount of the emp table.

select sum(sal),avg(sal) from emp;

Output:
SUM(SAL) AVG(SAL)

80000 16000

Q5: Count the total records in the emp table.


select * from emp;

Output:
EMPNO ENAME JOB DEPTNO SAL

1 Mathi AP 1 10000
2 Arjun ASP 2 15000
3 Gugan ASP 115000
4 Karthik Prof 2 30000
5 Akalya AP 1 10000

select count(*) from emp;


Output:
COUNT(*)
5

Q6: Determine the max and min salary and rename the column as max_salary and
min_salary.

select max(sal) as max_salary, min(sal) as min_salary from emp;

Output:
MAX_SALARY MIN_SALARY

30000 10000

Q7: Display the month between “1-jun-10”and 1-aug-10 in full.

Select month_between (‘1-jun-2010’,’1-aug-2010’) from dual;

12
Q8: Display the last day of that month in “05-Oct-09”.
Select last_day ('1-jun-2009') from dual;

Output:
LAST_DAY(

30-JUN-09

Q9: Find how many job titles are available in employee table.
select count(job) from emp;

Output:
COUNT(JOB)

select count(distinct job) from emp;

Output:
COUNT(DISTINCTJOB)

Q10: What is the difference between maximum and minimum salaries of employees in the
organization?

select max(sal), min(sal) from emp;

Output:
MAX(SAL) MIN(SAL)

20000 10000

Result:
Thus the nested Queries and join Queries was performed and executed successfully.

13
Ex No: 5
Date :

NESTED QUERIES AND JOIN QUERIES


AIM:

To perform nested Queries and joining Queries using DML command.

Q1: Display all employee names and salary whose salary is greater than minimum salary of
the company and job title starts with ‘M’.

select ename,sal from emp where sal>(select min(sal) from emp where job like 'A%');

Output:
ENAME SAL

Arjun 12000
Gugan 20000
Karthik 15000

Q2: Issue a query to find all the employees who work in the same job as
Arjun.

select * from emp;

Output:

EMPNO ENAME

1 Mathi AP
2 Arjun ASP
3 Gugan ASP
4 Karthik AP

select ename from emp where


job=(select job from emp
where ename='Arjun');

Output:
ENAME
Arjun
Gugan

14
Q3: Issue a query to display information about employees who earn more than any
employee in dept 1.
Ans:
SQL> select * from emp where sal>(select max(sal) from emp where empno=1);

EMPNO ENAME JOB DEPTNO SAL

2 Arjun ASP 2 12000


3 Gugan ASP 2 20000
4 Karthik AP 1 15000

JOINS
Tables used
SQL> select * from emp;

EMPNO ENAME JOB DEPTNOSAL

1 Mathi AP 1 10000
2 Arjun ASP 2 12000
3 Gugan ASP 220000
4 Karthik AP 1 15000

SQL> select * from dept;

DEPTNO DNAME LOC

1 ACCOUNTING NEW YORK


2 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON

EQUI-JOIN
Q4: Display the employee details, departments that the departments are same in both the
emp and dept.
Solution:
1. Use select from clause. 2. Use equi join in select clause to get the result.

Ans:
SQL> select * from emp,dept where emp.deptno=dept.deptno;

15
EMPNO ENAME JOB DEPTNO SAL DEPTNO DNAME LOC
--
-
1000
1 Mathi AP 1 0 1 ACCOUNTING NEW YORK
1200
2 Arjun ASP 2 0 2 RESEARCH DALLAS
2000
3 Gugan ASP 2 0 2 RESEARCH DALLAS
1500
4 Karthik AP 1 0 1 ACCOUNTING NEW YORK

Q5: Display the employee details, departments that the departments are not same in both
the emp and dept.
Solution:
1.Use select from clause. 2. Use non equi join in select clause to get the result.
Ans:
SQL> select * from emp,dept where
emp.deptno!=dept.deptno;
EMPNO ENAME JOB DEPTNO SAL DEPTNO DNAME LOC

2 Arjun ASP 2 12000 1 ACCOUNTING NEW YORK


3 Gugan ASP 2 20000 1 ACCOUNTING NEW YORK
1 Mathi AP 1 10000 2 RESEARCH DALLAS

EMPNO ENAME JOB DEPTNO SAL DEPTNO DNAMELOC

4 Karthik AP 115000 2 RESEARCH DALLAS


1 Mathi AP 1 10000 30 SALES CHICAGO
2 Arjun ASP 2 12000 30 SALES CHICAGO
EMPNO ENAME JOB DEPTNO SAL DEPTNO DNAMELOC

3 Gugan ASP 2 20000 30 SALES CHICAGO


30
4 Karthik AP 115000 SALES CHICAGO
BOSTO
1 Mathi AP 1 10000 40 OPERATIONS N
EMPNO ENAME JOB DEPTNO SAL DEPTNO DNAMELOC

2 Arjun ASP 2 12000 40 OPERATIONS BOSTON


3 Gugan ASP 2 20000 40 OPERATIONS BOSTON
4 Karthik AP 115000 40 OPERATIONS BOSTON
16
12 rows
selected.

LEFTOUT-JOIN
Tables used
SQL> select * from stud1;
Regno Name Mark2 Mark3 Result
- -
101 john 89 80 pass
102 Raja 70 80 pass
103 Sharin 70 90 pass
104 sam 90 95 pass
SQL> select * from stud2;
NAME GRA
john s
raj s
sam a
sharin a
Q6: Display the Student name and grade by implementing a left outer join.
Ans: SQL> select stud1.name,grade from stud1 left outer join stud2 on stud1.name=stud2.name;
Name Gra

john s
raj s
sam a
sharin a
smith null

RIGHTOUTER-JOIN
Q7: Display the Student name, register no, and result by implementing a right outer join.
Ans:
SQL> select stud1.name, regno, result from stud1 right outer join stud2 on stud1.name =
stud2.name;
Name Regno Result

john 101 pass


raj 102 pass
sam 103 pass
sharin 104 pass
Rollno Name Mark1 Mark2 Total
-
1 sindu 90 95 185
2 arul 90 90 180

17
FULLOUTER-JOIN

Q8: Display the Student name register no by implementing a full outer join.
Ans:
SQL> select stud1.name, regno from stud1 full outer join stud2 on (stud1.name= stud2.name);
Name Regno
john 101
raj 102
sam 103
sharin 104

SELFJOIN

Q9: Write a query to display their employee names


Ans:
SQL> select distinct ename from emp x, dept y where x.deptno=y.deptno;
ENAME
Arjun
Gugan
Karthik
Mathi

Q10: Display the details of those who draw the salary greater than the average salary.
Ans:
SQL> select distinct * from emp x where x.sal >= (select avg(sal) from emp);
EMPNO ENAME JOB DEPTNO SAL

3 Gugan ASP 2 20000


4 Karthik AP 1 15000
11 kavitha designer 12 17000

Result:
Thus the nested Queries and join Queries was performed and executed successfully.

18
Ex No: 6
Date :

SET OPERATORS
AIM:
To perform set operations using DML Commands

Q1: Display all the dept numbers available with the dept and emp tables
avoiding duplicates.
Solution:
1. Use select from clause. 2. Use union select clause to get the result.
Ans:
SQL> select deptno from emp union select deptno from dept;

DEPTNO

1
2
12
30
40

Q2: Display all the dept numbers available with the dept and emp tables.
Solution:
1. Use select from clause. 2. Use union all in select clause to get the result.
Ans:
SQL> select deptno from emp union all select deptno from dept;

DEPTNO

1
2
2
1
12
1
2
30
40
9 rows selected.

19
Q3: Display all the dept numbers available in emp and not in dept tables and vice versa.
Solution:
1. Use select from clause.
2. Use minus in select clause to get the result.
Ans:
SQL> select deptno from emp minus select deptno from dept;

DEPTNO

12
SQL> select deptno from dept minus select deptno from emp;
DEPTNO
30
40

Result:
Thus the set operations using DML Commands was performed and executed successfully.

20
Ex No: 7
Date :
VIEWS
AIM:
To create and manipulate various database objects of the Table using views

Q1: The organization wants to display only the details of the employees those who are
ASP. (Horizontal portioning)
Solution:
1. Create a view on emp table named managers
2. Use select from clause to do horizontal partioning
Ans:
SQL> create view empview as select * from emp where job='ASP';
View created.
SQL> select * from empview;
EMPNO ENAME JOB DEPTNO SAL

120
2 Arjun ASP 2 00
200
3 Gugan ASP 2 00
Q2: The organization wants to display only the details like empno, empname,
deptno, deptname of the employees. (Vertical portioning)
Solution:
1. Create a view on emp table named general 2. Use select from clause to do vertical
partioning
Ans:
SQL> create view empview1 as select ename,sal from emp; View
created.
Q3: Display all the views generated.
Ans:
SQL> select * from tab;
Q4: Execute the DML commands on the view
created.
Ans:
SQL> select * from empview;
JO SA
EMPNO ENAME B DEPTNO L

2 Arjun ASP 2 12000

3 Gugan ASP 2 20000

21
Q5: Drop a view.
Ans: SQL> drop view empview1;
View dropped.

Result:
Thus the creation and manipulate various database objects of the table using views
was executed successfully.

22
Ex No: 8
Date :

VIEWS PARTITIONS AND LOCKS

AIM:

To create, test and use views, partitions and locks

Creating a View

CREATE VIEW salesforce AS


SELECT first_name || ' ' || last_name "Name",
salary*12 "Annual Salary"
FROM employees
WHERE department_id = 80;

The results of the script follow.

CREATE VIEW succeeded

PARTITIONING
PROD_ID CUST_ID TIME_ID CHANNEL_ID PROMO_ID QUANTITY_SOLD
AMOUNT_SOLD

116 11393 05-JUN-99 2 999 1 12.18


40 100530 30-NOV-98 9 33 1 44.99
118 133 06-JUN-01 2 999 1 17.12
133 9450 01-DEC-00 2 999 1 31.28
36 4523 27-JAN-99 3 999 1 53.89
125 9417 04-FEB-98 3 999 1 16.86
30 170 23-FEB-01 2 999 1 8.8
24 11899 26-JUN-99 4 999 1 43.04
35 2606 17-FEB-00 3 999 1 54.94
45 9491 28-AUG-98 4 350 1 47.45

23
You create time_range_sales as a partitioned table using the statement in the following
example. The time_id column is the partition key.

Example Range-Partitioned Table

CREATE TABLE time_range_sales


( prod_id NUMBER(6)
, cust_id NUMBER
, time_id DATE
, channel_id CHAR(1)
, promo_id NUMBER(6)
, quantity_sold NUMBER(3)
, amount_sold NUMBER(10,2))

PARTITION BY RANGE (time_id)


(PARTITION SALES_1998 VALUES LESS THAN (TO_DATE('01-JAN-1999','DD-MON-
YYYY')),
PARTITION SALES_1999 VALUES LESS THAN (TO_DATE('01-JAN-2000','DD-MON-
YYYY')),
PARTITION SALES_2000 VALUES LESS THAN (TO_DATE('01-JAN-2001','DD-MON-
YYYY')),
PARTITION SALES_2001 VALUES LESS THAN (MAXVALUE));

Locks

*LOCK TABLES table_name [READ | WRITE]


* LOCK TABLES table_name1 [READ | WRITE],
table_name2 [READ | WRITE],
... ;

MySQL UNLOCK TABLES statement

To release a lock for a table, you use the following statement:

UNLOCK TABLES;

Result:

Thus views, partitioning and locks was performed and executed successfully.

24
Ex No:9
Date :

FUNCTIONS AND PROCEDURES

AIM:

To create and test functions in sql

Syntax to create a function:

1. CREATE [OR REPLACE] FUNCTION function_name [parameters]


2. [(parameter_name [IN | OUT | IN OUT] type [, ...])]
3. RETURN return_datatype
4. {IS | AS}
5. BEGIN
6. < function_body >
7. END [function_name];

PL/SQL Function Example

Let's see a simple example to create a function.

1. create or replace function adder(n1 in number, n2 in number)


2. return number
3. is
4. n3 number(8);
5. begin
6. n3 :=n1+n2;
7. return n3;
8. end;
9. /

Now write another program to call the function.

1. DECLARE
2. n3 number(2);
3. BEGIN
4. n3 := adder(11,22);
5. dbms_output.put_line('Addition is: ' || n3);
6. END;
7. /

25
Output:

Addition is: 33
Statement processed.
0.05 seconds

Another PL/SQL Function Example

Let's take an example to demonstrate Declaring, Defining and Invoking a simple PL/SQL
function which will compute and return the maximum of two values.

1. DECLARE
2. a number;
3. b number;
4. c number;
5. FUNCTION findMax(x IN number, y IN number)
6. RETURN number
7. IS
8. z number;
9. BEGIN
10. IF x > y THEN
11. z:= x;
12. ELSE
13. Z:= y;
14. END IF;
15.
16. RETURN z;
17. END;
18. BEGIN
19. a:= 23;
20. b:= 45;
21.
22. c := findMax(a, b);
23. dbms_output.put_line(' Maximum of (23,45): ' || c);
24. END;
25. /

Output:

Maximum of (23,45): 45
Statement processed.
0.02 seconds

26
Create Function:
1. CREATE OR REPLACE FUNCTION totalCustomers
2. RETURN number IS
3. total number(2) := 0;
4. BEGIN
5. SELECT count(*) into total
6. FROM customers;
7. RETURN total;
8. END;
9. /

After the execution of above code, you will get the following result.

Function created.

1. DECLARE
2. c number(2);
3. BEGIN
4. c := totalCustomers();
5. dbms_output.put_line('Total no. of Customers: ' || c);
6. END;
7. /

After the execution of above code in SQL prompt, you will get the following result.

Total no. of Customers: 4


PL/SQL procedure successfully completed.

Create procedure:

Execute the following program to retrieve the customer name and address.

DECLARE

c_id customers.id%type;

c_name customers.name%type;

c_addr customers.address%type;

CURSOR c_customers is

SELECT id, name, address FROM customers;

BEGIN

27
OPEN c_customers;

LOOP

FETCH c_customers into c_id, c_name, c_addr;

EXIT WHEN c_customers%notfound;

dbms_output.put_line(c_id || ' ' || c_name || ' ' || c_addr);

END LOOP;

CLOSE c_customers;

END;

Output:

Ramesh Allahabad
Suresh Kanpur
Mahesh Ghaziabad
Chandan Noida
Alex Paris
Sunita Delhi
PL/SQL procedure successfully completed.

Result:

Thus functions and procedures was performed and executed successfully.

28
Ex No: 10
Date :
CURSOR(IMPICIT AND EXPLICIT)

AIM:

To create cursors using SQL query


Create customers table and have records:

ID NAME AGE ADDRESS SALARY


1 Ramesh 23 Allahabad 20000
2 Suresh 22 Kanpur 22000
3 Mahesh 24 Ghaziabad 24000
4 Chandan 25 Noida 26000
5 Alex 21 Paris 28000
6 Sunita 20 Delhi 30000

Let's execute the following program to update the table and increase salary of each customer
by 5000. Here, SQL%ROWCOUNT attribute is used to determine the number of rows
affected:

Create procedure:

1. DECLARE
2. total_rows number(2);
3. BEGIN
4. UPDATE customers
5. SET salary = salary + 5000;
6. IF sql%notfound THEN
7. dbms_output.put_line('no customers updated');
8. ELSIF sql%found THEN
9. total_rows := sql%rowcount;
10. dbms_output.put_line( total_rows || ' customers updated ');
11. END IF;
12. END;
13. /

Output:

6 customers updated
PL/SQL procedure successfully completed.

Now, if you check the records in customer table, you will find that the rows are updated.

29
1. select * from customers;

ID NAME AGE ADDRESS SALARY


1 Ramesh 23 Allahabad 25000
2 Suresh 22 Kanpur 27000
3 Mahesh 24 Ghaziabad 29000
4 Chandan 25 Noida 31000
5 Alex 21 Paris 33000
6 Sunita 20 Delhi 35000

2) PL/SQL Explicit Cursors

The Explicit cursors are defined by the programmers to gain more control over the context
area. These cursors should be defined in the declaration section of the PL/SQL block. It is
created on a SELECT statement which returns more than one row.

Following is the syntax to create an explicit cursor:

Syntax of explicit cursor

Following is the syntax to create an explicit cursor:

1. CURSOR cursor_name IS select_statement;;

Steps:

You must follow these steps while working with an explicit cursor.

1. Declare the cursor to initialize in the memory.


2. Open the cursor to allocate memory.
3. Fetch the cursor to retrieve data.
4. Close the cursor to release allocated memory.

1) Declare the cursor:

It defines the cursor with a name and the associated SELECT statement.

Syntax for explicit cursor decleration

1. CURSOR name IS
2. SELECT statement;

30
2) Open the cursor:

It is used to allocate memory for the cursor and make it easy to fetch the rows returned by the
SQL statements into it.

Syntax for cursor open:

1. OPEN cursor_name;
3) Fetch the cursor:

It is used to access one row at a time. You can fetch rows from the above-opened cursor as
follows:

Syntax for cursor fetch:

1. FETCH cursor_name INTO variable_list;

4) Close the cursor:

It is used to release the allocated memory. The following syntax is used to close the above-
opened cursors.

Syntax for cursor close:

1. Close cursor_name;

PL/SQL Explicit Cursor Example

Explicit cursors are defined by programmers to gain more control over the context area. It is
defined in the declaration section of the PL/SQL block. It is created on a SELECT statement
which returns more than one row.

Let's take an example to demonstrate the use of explicit cursor. In this example, we are using
the already created CUSTOMERS table.

Create customers table and have records:

ID NAME AGE ADDRESS SALARY


1 Ramesh 23 Allahabad 20000
2 Suresh 22 Kanpur 22000
3 Mahesh 24 Ghaziabad 24000
4 Chandan 25 Noida 26000
5 Alex 21 Paris 28000
6 Sunita 20 Delhi 30000

31
Result:

Thus cursors was performed and executed successfully.

32
Ex No: 11
Date :

EXCEPTION HANDLING(TRIGGERS)

AIM:

To create triggers and apply them using SQL

SELECT* FROM COUSTOMERS;

ID NAME AGE ADDRESS SALARY

1 Ramesh 23 Allahabad 20000

2 Suresh 22 Kanpur 22000

3 Mahesh 24 Ghaziabad 24000

4 Chandan 25 Noida 26000

5 Alex 21 Paris 28000

6 Sunita 20 Delhi 30000

1. DECLARE
2. c_id customers.id%type := 8;
3. c_name customers.name%type;
4. c_addr customers.address%type;
5. BEGIN
6. SELECT name, address INTO c_name, c_addr
7. FROM customers
8. WHERE id = c_id;
9. DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
10. DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr);
11. EXCEPTION
12. WHEN no_data_found THEN
13. dbms_output.put_line('No such customer!');
14. WHEN others THEN
15. dbms_output.put_line('Error!');
16. END;

33
OUTPUT:

No such customer!
PL/SQL procedure successfully completed.

1. DECLARE
2. c_id customers.id%type := 5;
3. c_name customers.name%type;
4. c_addr customers.address%type;
5. BEGIN
6. SELECT name, address INTO c_name, c_addr
7. FROM customers
8. WHERE id = c_id;
9. DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
10. DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr);
11. EXCEPTION
12. WHEN no_data_found THEN
13. dbms_output.put_line('No such customer!');
14. WHEN others THEN
15. dbms_output.put_line('Error!');
16. END;

OUTPUT:

Name: alex
Address: paris
PL/SQL procedure successfully completed.

PL/SQL Trigger Example

Let's take a simple example to demonstrate the trigger. In this example, we are using the
following CUSTOMERS table:

Create table and have records:

ID NAME AGE ADDRESS SALARY


1 Ramesh 23 Allahabad 20000
2 Suresh 22 Kanpur 22000
3 Mahesh 24 Ghaziabad 24000
4 Chandan 25 Noida 26000
5 Alex 21 Paris 28000
6 Sunita 20 Delhi 30000

34
Create trigger:

Let's take a program to create a row level trigger for the CUSTOMERS table that would fire
for INSERT or UPDATE or DELETE operations performed on the CUSTOMERS table. This
trigger will display the salary difference between the old values and new values:

1. CREATE OR REPLACE TRIGGER display_salary_changes


2. BEFORE DELETE OR INSERT OR UPDATE ON customers
3. FOR EACH ROW
4. WHEN (NEW.ID > 0)
5. DECLARE
6. sal_diff number;
7. BEGIN
8. sal_diff := :NEW.salary - :OLD.salary;
9. dbms_output.put_line('Old salary: ' || :OLD.salary);
10. dbms_output.put_line('New salary: ' || :NEW.salary);
11. dbms_output.put_line('Salary difference: ' || sal_diff);
12. END;
13. /

After the execution of the above code at SQL Prompt, it produces the following result.

Trigger created.

Result:

Thus triggers using SQL commands are implemented and executed successfully.

35

You might also like