Professional Documents
Culture Documents
Dbms Lab
Dbms Lab
Dbms Lab
LAB MANUAL
Course : B.Tech
Regulation : R19
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
ADDITIONAL PROGRAMS
2. Write a PL/SQL Code Creation of forms for any Information System such 29-31
as Student Information System, Employee Information System etc.
SQL:
SQL stands for Structured Query Language. It is used for storing and managing data in relational database
management system (RDMS). It is a standard language for Relational Database System. It enables a user to
create, read, update and delete relational databases and tables. All the RDBMS like MySQL, Informix,
Oracle, MS Access and SQL Server use SQL as their standard database language. SQL allows users to
query the database in a number of ways, using English-like statements.
Rules:
SQL follows the following rules:
1. Structure query language is not case sensitive. Generally, keywords of SQL are written in uppercase.
2. Statements of SQL are dependent on text lines. We can use a single SQL statement on one or multiple
text line.
3. Using the SQL statements, you can perform most of the actions in a database.
4. SQL depends on tuple relational calculus and relational algebra.
SQL process:
When an SQL command is executing for any RDBMS, then the system figure out the best way to carry out
the request and the SQL engine determines that how to interpret the task. In the process, various
components are included. These components can be optimization Engine, Query engine, Query dispatcher,
classic, etc. All the non-SQL queries are handled by the classic query engine, but SQL query engine won't
handle logical files.
Next To
SQL Commands:
SQL commands are instructions. It is used to communicate with the database. It is also used to perform
specific tasks, functions, and queries of data. SQL can perform various tasks like create a table, add data to
tables, drop the table, modify the table, set permission for users.
Types of SQL Commands:
There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.
1
DDL: (Data Definition Language)
DDL allows you to create SQL statements to make operations with database data structures (schemas,
tables etc.).
These are SQL DDL commands list and examples:
CREATE:
CREATE statement is used to create a new database, table, index or stored procedure.
Create database example:
CREATE DATABASE explainjava;
Create table example:
CREATE TABLE user (
id INT(16) PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL
);
DROP:
DROP statement allows you to remove database, table, index or stored procedure.
Drop database example:
DROP DATABASE explainjava;
Drop table example:
DROP TABLE user;
2
ALTER:
ALTER is used to modify existing database data structures (database, table).
Alter table example:
ALTER TABLE user ADD COLUMN lastname VARCHAR(255) NOT NULL;
RENAME:
RENAME command is used to rename SQL table.
Rename table example:
RENAME TABLE user TO student;
TRUNCATE:
TRUNCATE operation is used to delete all table records. Logically it’s the same as DELETE command.
Differences between DELETE and TRUNCATE commands are:
1. TRUNCATE is really faster
2. TRUNCATE cannot be rolled back
3. TRUNCATE command does not invoke ON DELETE triggers
Example:
TRUNCATE student;
SELECT:
SELECT query is used to retrieve a data from SQL tables.
Example:
SELECT * FROM student;
INSERT:
INSERT command is used to add new rows into the database table.
Example:
INSERT INTO student (name, lastname) VALUES ('Dmytro', 'Shvechikov');
UPDATE:
UPDATE statement modifies records into the table.
Example:
UPDATE student SET name = 'Dima' WHERE lastname = 'Shvechikov';
DELETE:
DELETE query removes entries from the table.
3
Example:
DELETE FROM student WHERE name = 'Dima';
GRANT:
GRANT command gives permissions to SQL user account.
For example, I want to grant all privileges to ‘explainjava’ database for user ‘dmytro@localhost’.
Create a user first:
CREATE USER 'dmytro'@'localhost' IDENTIFIED BY '123';
Then I can grant all privileges using GRANT statement:
GRANT ALL PRIVILEGES ON explainjava.* TO 'dmytro'@'localhost';
REVOKE:
REVOKE statement is used to remove privileges from user accounts.
Example:
REVOKE ALL PRIVILEGES ON explainjava.* FROM 'dmytro'@'localhost';
COMMIT:
COMMIT command finishes transaction and stores all changes made inside of a transaction.
Example:
INSERT INTO student (name, lastname) VALUES ('Dmytro', 'Shvechikov');
COMMIT;
ROLLBACK:
ROLLBACK statement reverts all changes made in the scope of transaction.
Example:
INSERT INTO student (name, lastname) VALUES ('Dmytro', 'Shvechikov');
ROLLBACK;
SAVEPOINT:
SAVEPOINT statement stores the transaction temporarily to restore/roll back.
Example:
INSERT INTO student (name, lastname) VALUES ('Dmytro', 'Shvechikov');
SAVEPOINT a;
INSERT INTO student (name, lastname) VALUES ('Angel', 'Paul');
ROLLBACK TO a;
4
EXPEREMENT 1
Aim: Data Definition Commands, Data Manipulation Commands for inserting and deleting data from
Tables.
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
Table altered.
Table altered.
Table renamed.
Table created.
Table truncated.
Table dropped.
7
EXPEREMENT 2
AIM: Data Manipulation commands for updating and retrieving of data from Tables and Transaction
Control Statements.
Table created.
1 row created.
1 row created.
1 row created.
1 row updated.
1 row deleted.
SNAME
----------
dustin
TCL Commands:
SQL> select * from sailors;
1.Commit:
SQL> commit;
Commit complete.
1 row created.
2. Savepoint:
SQL> savepoint a;
Savepoint created.
1 row created.
3.Rollback:
SQL> rollback to a;
Rollback complete.
10
EXPEREMENT 3
AIM: Basic Functions like Numeric, String, Date and Conversion functions.
Conversion functions
1. To_char:
SQL> select to_char(1234,'9999D99') from dual;
TO_CHAR(
--------
1234.00
TO_CHA
------
1,234
TO_CHAR(1234,'L
---------------
$1234
TO_CH
-----
1234
TO_CH
-----
1234-
TO_CHA
11
------
<1234>
TO_ CHAR
---
+10
TO_
---
A
TO_CHAR(10,'RN'
---------------
X
TO_CH
-----
0105
TO_C
----
90
2. To_number:
TO_NUMBER('12','99')
--------------------
12
SQL> select to_number('12','99D9') from dual;
12
TO_NUMBER('12','99D9'
)
----------------------
12
TO_NUMBER('12','99D9
9')
-----------------------
12
3. To-date:
TO_DATE('
---------
06-DEC-21
TO_DATE('
---------
21-OCT-18
String functions:
1. Lower():
SQL> select lower('SAI') from dual;
LOW
--------
sai
2. upper():
SQL> select upper('sai') from dual;
UPP
-----
SAI
3. concat():
SQL> select concat('hello','sai') from dual;
13
CONCAT('
--------
hellosai
4. initcap():
SQL> select initcap('sai') from dual;
INI
---
Sai
5. length():
3
6. Instr():
8. Lpad():
9. Rpad():
SQL> select rpad('ruhanika',10,'****') from dual;
RPAD('RUHA
ruhanika**
10. Ltrim():
14
SQL>select ltrim(' ruhanika') from dual;
LTRIM('R
ruhanika
11. Rtrim():
SQL>selectrtrim(' ruhanika ') from dual;
RTRIM('RUHANIK
ruhanika
Date functions:
1. Sysdate():
SQL> select sysdate from dual;
SYSDATE
20-NOV-15
2. last_day():
SQL> select last_day(sysdate) fromdual;
LAST_DAY(
30-NOV-15
3. next_day():
SQL> select next_day('20-nov-2015','friday') fromdual;
NEXT_DAY(
27-NOV-15
4. add_months():
SQL> select add_months(sysdate,2) from dual;
ADD_MONTH
20-JAN-16
5. months_between():
SQL> select months_between('20-nov-2015','20-jan-2016') from dual;
MONTHS_BETWEEN('20-NOV-2015','20-JAN-2016')
-2
6. least():
SQL> select least(10,11,12) from dual;
LEAST(10,11,12)
10
SQL> select least('s','f','a') from dual;
L
-
15
a
7. greatest():
SQL> select greatest(10,11,12) from dual;
GREATEST(10,11,12)
12
SQL> select greatest('s','f','a') from dual;
G
-
s
8. round():
16
EXPEREMENT 4
Aim: Database Querying: Basic simple queries.
SQL> create table sailors(sid int primary key not null,sname varchar2(20),srating int,sage
int,check(sage>=25));
Table created.
SQL> create table boats(bid int primary key not null,bname varchar2(20),bcolor varchar2(10));
Table created.
SQL> create table reserves(sid int,bid int,day varchar2(20),constraint fk1 foreign key(sid) references
sailors(sid),constraint fk2 foreign key(bid) references boats(bid));
Table created.
Insert values into Sailors Table:
SQL> insert into sailors values(22,'dustin',7,45);
1 row created.
SQL> insert into sailors values(22,'dustin',7,15);
insert into sailors values(22,'dustin',7,15)
*
ERROR at line 1:
ORA-02290: check constraint (SYSTEM.SYS_C004016) violated
17
SQL> insert into sailors values(32,'andy',8,26);
1 row created.
SQL> insert into sailors values(58,'rusty',10,36);
1 row created.
SQL> insert into sailors values(64,'horatio',7,36);
1 row created.
SQL> insert into sailors values(74,'horatio',9,35);
1 row created.
Insert values into Boats Table:
SQL> insert into boats values(101,'interlake','blue');
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
7 rows selected.
7 rows selected.
19
SID SNAME SRATING SAGE
---------- -------------------- ---------- ----------
58 rusty 10 36
SNAME
--------------------
lubber
andy
rusty
horatio
3. Find the names of the sailors who have reserved boat no 103?
SQL> select sname from sailors s,reserves r where s.sid=r.sid and r.bid=103;
SNAME
--------------------
dustin
horatio
SID
----------
22
22
31
64
5. Find the name of the sailors who have reserved a green boat.
SQL> select sname from sailors s,boats b,reserves r where s.sid=r.sid and r.bid=b.bid and
b.bcolor='green';
SNAME
--------------------
dustin
horatio
BCOLOR
----------
red
7. Find all the sailors who have reserved atleast one boat
SQL> select distinct s.sname from sailors s ,reserves r where s.sid=r.sid;
SNAME
--------------------
lubber
dustin
horatio
8. Compute and increment the rating and display the name and sid of sailors who have sailed 2
different boats on same day
SQL> select distinct s1.sname, s1.sid, s1.srating+1 as inc_rate
2 From sailors s1, reserves r1, reserves r2
3 Where r1.bid<>r2.bid and r1.sid =r2.sid and r1.day=r2.day and r1.sid=s1.sid;
21
EXPEREMENT 5
Aggregate Functions:
1. Count():
22
SQL> select count(price) from product;
COUNT(PRICE)
----------------------
5
2. Sum ():
23
SQL> select max(quantity) from product;
MAX(QUANTITY)
--------------------
20
5. Min():
SQL> select min(price) from product;
MIN(PRICE)
------------------
5
Group by Clause:
Create table employ(sid int, name varchar2(20), dept varchar2(10), sal float);
Table created
Select * from employ
Sid name deptsal
1 Ayisha ece 6000
2 Sindhu it 5000
3 Sai it 8000
4 Lalli ece 8000
Having Clause:
SQL>select dept,sum(sal) from employ group by dept having sum(sal)>13000;
Dept sum(sal)
ece 14000
24
EXPEREMENT 6
SQL> create table sailors(sid int primary key not null,sname varchar2(20),srating int,sage
int,check(sage>=25));
Table created.
SQL> create table boats(bid int primary key not null,bname varchar2(20),bcolor varchar2(10));
Table created.
SQL> create table reserves(sid int,bid int,day varchar2(20),constraint fk1 foreign key(sid) references
sailors(sid),constraint fk2 foreign key(bid) references boats(bid));
Table created.
Insert values into Sailors Table:
SQL> insert into sailors values(22,'dustin',7,45);
1 row created.
SQL> insert into sailors values(22,'dustin',7,15);
insert into sailors values(22,'dustin',7,15)
*
ERROR at line 1:
ORA-02290: check constraint (SYSTEM.SYS_C004016) violated
1 row created.
1 row created.
1 row created.
1 row created.
26
1 row created.
1 row created.
1 row created.
7 rows selected.
7 rows selected.
27
1. Find the names of the sailors who have reserved boat 103
SQL> select sname from sailors where sid IN(select sid from reserves where bid=103);
SNAME
--------------------
dustin
horatio
2. Find the names of sailors who have reserved a red boat.
SQL> select sname from sailors where sid IN (select sid from reserves where bid in (select bid from
boats where bcolor = 'red'));
SNAME
--------------------
dustin
lubber
horatio
3. Find the names of sailors who have not reserved a red boat.
SQL> select sname from sailors
2 where sid NOT IN ( select sid from reserves where bid IN (select bid
3 from boats where bcolor = 'red'));
SNAME
--------------------
brutus
andy
rusty
horatio
4. Find the sailors whose raing is better than some sailor horatio
Select s.sid from sailors s
2 Where s.srating> ANY (select s1.srating from sailors s1 where s1.sname='horatio');
28
SID
----------
58
74
31
32
5. Find the sailors with highest rating
select s.sid from sailors s Where s.srating>=ALL(select s1.srating from sailors s1);
SID
----------
58
6. Find the names of the sailors who have reserved a boat number 103
SQL> select s.sname from sailors s where exists (select * from reserves r where r.bid=103 and
r.sid=s.sid);
SNAME
--------------------
dustin
horatio
SQL> Select s.sname from sailors s Where NOT EXISTS( select b.bid from boats b
2 Where NOT EXISTS(select r.bid from reserves r Where r.sid=s.sid and r.bid=b.bid));
SNAME
--------------------
29
dustin
EXPEREMENT 7
AIM: Queries Using Joins
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
30
Create table orders:
SQL> create table orders(sno int primary key not null,orderdate date,cid int,sid int,amount int);
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
7 rows selected.
7 rows selected.
INNER JOIN:
SQL> select cname,age,sid,amount from customer inner join orders on customer.cid=orders.cid;
7 rows selected.
LEFT JOIN:
SQL> select cname,age,sid,orderdate,amount from customer left join orders on
customer.cid=orders.cid;
10 rows selected.
RIGHT JOIN:
SQL> select cname,age,sid,amount from customer right join orders on customer.cid=orders.cid;
7 rows selected.
FULL JOIN:
SQL> select cname,age,sid,amount from customer full join orders on customer.cid=orders.cid;
10 rows selected.
CROSS JOIN:
SQL> select cname,age,amount from customer cross join orders;
49 rows selected.
35
EXPEREMENT 8
1. Create a view:
SQL> create or replace view an as select eid,name from employeee;
View created.
EID NAME
---------- --------------------
1 amit
2 sachin
3 vinay
4 vivek
2. Update view:
SQL> update an set name='sunil' where eid=4;
1 row updated.
EID NAME
---------- --------------------
1 amit
2 sachin
3 vinay
4 sunil
36
3. Delete from view:
SQL> delete from an where eid=4;
1 row deleted.
EID NAME
---------- --------------------
1 amit
2 sachin
3 vinay
4. Dropping view:
SQL> drop view an;
View dropped.
37
EXPEREMENT 9
Procedure:
A subprogram is a program unit/module that performs a particular task. These subprograms are combined
to form larger programs. This is basically called the 'Modular design'. A subprogram can be invoked by
another subprogram or program which is called the calling program.
A subprogram can be created −
Creating a Procedure:
A procedure is created with the CREATE OR REPLACE PROCEDURE statement. The simplified
syntax for the CREATE OR REPLACE PROCEDURE statement is as follows −
CREATE [OR REPLACE] PROCEDURE procedure_name
[(parameter_name[IN | OUT | IN OUT] type [,...])]
{AS/IS}
BEGIN
<procedure_body>
ENDprocedure_name;
Where,
procedure-name specifies the name of the procedure.
[OR REPLACE] option allows the modification of an existing procedure.
The optional parameter list contains name, mode and types of the parameters. IN represents the value
that will be passed from outside and OUT represents the parameter that will be used to return a value
outside of the procedure.
procedure-body contains the executable part.
38
Example
The following example creates a simple procedure that displays the string 'Hello World!' on the screen when
executed.
CREATE OR REPLACE PROCEDURE greetings
AS
BEGIN
dbms_output.put_line('Hello World!');
END;
/
When the above code is executed using the SQL prompt, it will produce the following result −
Procedure created.
40
PL/SQL procedure successfully completed.
iii. PL/SQL Program to find the total number of primes up to given number Procedures.
Procedure created.
41
6 end;
7 /
Output: 2
3
5
7
11
13
17
19
the total prime nos are:8
SNO SNAME
---------- ----------
1 jaya
3 ani
Procedure created.
42
SNO SNAME
---------- ----------
1 jaya
3 ani
4 sunil
Procedure dropped
Creating a Function:
A standalone function is created using the CREATE FUNCTION statement. The simplified syntax for
the CREATE OR REPLACE PROCEDURE statement is as follows −
CREATE [OR REPLACE] FUNCTION function_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])]
RETURN return_datatype
{IS | AS}
BEGIN
< function_body >
END [function_name];
Where,
function-name specifies the name of the function.
[OR REPLACE] option allows the modification of an existing function.
The optional parameter list contains name, mode and types of the parameters. IN represents the value
that will be passed from outside and OUT represents the parameter that will be used to return a value
outside of the procedure.
The function must contain a return statement.
The RETURN clause specifies the data type you are going to return from the function.
function-body contains the executable part.
i. PL/SQL Program to find the maximum number from the given numbers using Functions.
SQL> set serveroutput on
SQL> DECLARE
2 a number;
3 b number;
4 c number;
43
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 RETURN z;
16 END;
17 BEGIN
18 a:= 23;
19 b:= 45;
20 c := findMax(a, b);
21 dbms_output.put_line(' Maximum of (23,45): ' || c);
22 END;
23 /
Maximum of (23,45): 45
SNO SNAME
---------- ----------
1 jaya
3 ani
4 sunil
Function created.
44
/*calling function totalcust*/
45
SQL> drop function totalcust;
Function dropped.
46
EXPEREMENT 10
AIM: Implicit and Explicit Cursors.
CURSORS:
A CURSOR is nothing but a pointer between the contest area and physical data storage contest area is a memory
which is reserved by oracle for crossing SQL statements. Suppose use for select statement then some memory
was allocated and then memory is said to be in contest area.
CURSOR can be divided into two types
Implicit CURSOR
Explicit CURSOR
Explicit Cursor- You can explicitly declare a cursor to process the rows individually. A cursor declared by
the user is called Explicit Cursor. For Queries that return more than one row, you must declare a cursor
explicitly. The data that is stored in the cursor is called the Active Data set. The size of the cursor in
memory is the size required to hold the number of rows in the Active. Cursor can be used when the user
wants to process data one row at a time.
Explicit Cursor Management- The steps involved in declaring a cursor and manipulating data in the
active data set are:-
• Declare a cursor that specifies the SQL select statement that you want to process.
• Open the Cursor.
• Fetch the data from the cursor one row at a time.
• Close the cursor.
Declaration: The General Syntax to create any particular cursor is as follows:-
Cursor<cur name> is Sql Select Statement;
Opening a Cursor: The General Syntax to Open any particular cursor is as follows:-
Open Cursorname;
Fetching a record From the Cursor: The fetch statement retrieves the rows from the active set to the
variables one at a time. Each time a fetch is executed. The focus of the DBA cursor advances to the next
row in the Active set. One can make use of any loop structure (Loop-End Loop along with While, For) to
fetch the records from the cursor into variable one row at a time. The General Syntax to Fetch the records
from the cursor is as follows:-
Fetch cursorname into variable1,variable2,______
Closing a Cursor:- The General Syntax to Close the cursor is as follows:-
Close <cur name>;
47
Implicit Cursors:
Table created.
1 row created.
1 row created.
48
SQL> select * from emp;
Explicit Cursors:
49
EXPEREMENT 11
AIM: Triggers.
Trigger: A trigger is a stored procedure in database which automatically invokes whenever a special
event in the database occurs. For example, a trigger can be invoked when a row is inserted into a
specified table or when certain table columns are being updated.
Syntax:
create trigger [trigger_name]
[before | after]
{insert | update | delete}
on [table_name]
[for each row]
[trigger_body]
Explanation of syntax:
create trigger [trigger_name]: Creates or replaces an existing trigger with the trigger_name.
[before | after]: This specifies when the trigger will be executed.
{insert | update | delete}: This specifies the DML operation.
on [table_name]: This specifies the name of the table associated with the trigger.
[for each row]: This specifies a row-level trigger, i.e., the trigger will be executed for each row being
affected.
[trigger_body]: This provides the operation to be performed as trigger is fired
Table created.
Trigger created.
Insert:
SQL> insert into stud values(1,20);
before insert of1
1 row created.
SID SUB1
---------- ----------
1 20
Insert:
SQL> insert into stud values(2,16)
2 ;
before insert of2
1 row created.
SID SUB1
---------- ----------
1 20
2 16
3
Create trigger (After):
create or replace trigger trg2 after
2 insert on stud
3 for each row
4 begin
5 dbms_output.put_line('after insert of'||:new.sid);
6 end;
51
7 /
Trigger created.
Insert:
SQL> insert into stud values(3,16);
before insert of3
after insert of3
1 row created.
SID SUB1
---------- ----------
1 20
2 16
3 16
Create Trigger:
SQL> create or replace trigger trg1 before
2 update on stud
3 for each row
4 begin
5 dbms_output.put_line('before update of'||:new.sid);
6 end;
7 /
Trigger created.
Update:
SQL> update stud set sub1=30 where sid=2;
before update of2
1 row updated.
SID SUB1
---------- ----------
1 20
2 30
52
3 16
4 17
Create Trigger:
SQL> create or replace trigger trg2 after
2 delete on stud
3 for each row
4 begin
5 dbms_output.put_line('after delete'||:new.sid);
6 end;
7 /
Trigger created.
Delete:
SQL> delete from stud where sid=3;
after delete
1 row deleted.
SID SUB1
---------- ----------
1 20
2 30
4 17
EXPEREMENT 12
53
AIM: Exception Handling.
Typically, each block performs a logical action in the program. A block has the following structure:
DECLARE
declaration statements;
BEGIN
executable statements
EXCEPTIONS
exception handling statements
END;
Declare section starts with DECLARE keyword in which variables, constants, records as cursors can
be declared which stores data temporarily. It basically consits definition of PL/SQL identifiers. This
part of the code is optional.
Execution section starts with BEGIN and ends with END keyword. This is a mandatory section and
here the program logic is written to perform any task like loops and conditional statements. It supports
all DML commands, DDL commands and SQL*PLUS built-in functions as well.
Exception section starts with EXCEPTION keyword. This section is optional which contains
statements that are executed when a run-time error occurs. Any exceptions can be handled in this
section.
An exception is an error which disrupts the normal flow of program instructions. PL/SQL provides us
the exception block which raises the exception thus helping the programmer to find out the fault and
resolve it.
DECLARE
declarations section;
BEGIN
executable command(s);
EXCEPTION
WHEN exception1 THEN
statement1;
WHEN exception2 THEN
statement2;
[WHEN others THEN]
/* default exception handling code */
END;
When other keyword should be used only at the end of the exception handling block as no exception
handling part present later will get executed as the control will exit from the block after executing the
WHEN OTHERS.
Exceptions:
1. NO_DATA_FOUND: It is raised WHEN a SELECT INTO statement returns no rows. For eg:
2. TOO_MANY_ROWS: It is raised WHEN a SELECT INTO statement returns more than one
row.
3. VALUE_ERROR: This error is raised WHEN a statement is executed that resulted in an
arithmetic, numeric, string, conversion, or constraint error. This error mainly results from
programmer error or invalid data input.
4. ZERO_DIVIDE: It raises exception WHEN dividing with zero.
SID SUB1
---------- ----------
1 20
2 30
4 17
55
Without Exception:
SQL> set serveroutput on
SQL> declare
2 m number(5);
3 myerror exception;
4 begin
5 select sub1 into m from stud where sid=2;
6 if m is null then
7 raise myerror;
8 else
9 dbms_output.put_line(m);
10 end if;
11 exception
12 when no_data_found then
13 dbms_output.put_line('error of data');
14 when too_many_rows then
15 dbms_output.put_line('error of too many rows');
16 when myerror then
17 dbms_output.put_line('error found null');
18 end;
19 /
30
With Exception:
SQL> set serveroutput on
SQL> declare
2 m number(5);
3 myerror exception;
4 begin
5 select sub1 into m from stud where sid=3;
6 if m is null then
7 raise myerror;
8 else
9 dbms_output.put_line(m);
10 end if;
11 exception
12 when no_data_found then
13 dbms_output.put_line('error of data');
14 when too_many_rows then
15 dbms_output.put_line('error of too many rows');
16 when myerror then
17 dbms_output.put_line('error found null');
18 end;
19 /
error of data
56
PL/SQL procedure successfully completed.
57