[go: up one dir, main page]

0% found this document useful (0 votes)
29 views19 pages

DBMS Unit Iv

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)
29 views19 pages

DBMS Unit Iv

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/ 19

Database Management Systems UNIT-IV

Introduction

SQL (Structured Query Language) is a database computer language designed

for managing data in relational database management systems (RDBMS).

SQL is a standardized computer language that was originally developed by IBM

for querying, altering and defining relational databases, using declarative statements.
SQL is pronounced /ˌɛs kjuː ˈɛl / (letter by letter) or /ˈsiːkwəl / (as a word).

Some of the popular relational database example are Oracle, MySQL, MSSQL, MariaDB,

PostgreSQL etc.

SQL is used to perform C.R.U.D (Create, Retrieve, Update & Delete) operations on
relational databases.

SQL can also perform administrative tasks on database such as database security,
backup, user management etc.

Characteristics of SQL / What Can SQL do?

― SQL is easy to learn.

― It can

• execute queries against a database.


• retrieve data from a database.

• Insert, update, and delete records from a database.

• create new databases, tables, views, stored procedures.

• set permissions on tables, procedures, and views.

SQL Datatypes
Data types are used to represent the nature of the data that can be

stored in the database table. For example, in a particular column of a table, if we want

to store set of characters then data type of this column should be character. Data types

mainly classified into three categories for every database.


1. Character Types

2. Numeric Types

3. Date and Time Types

Ch. Vijayananda Ratnam@Dept. of CSE 1


Database Management Systems UNIT-IV
Category Datatype Description

Character Types
CHAR(SIZE) It is used to store character data. It can be stored up to 2000 bytes.
It is used to store variable string data. It can be stored up to 4000
VARCHAR2(SIZE)
bytes.
It is the same as VARCHAR2(size). but it is suggested to use
VARCHAR(SIZE)
VARCHAR2(size)
It contains precision p and scale s. The precision p can range from 1
NUMBER(p, s)
Numeric
Types

to 38, and the scale s can range from -84 to 127.


It is a subtype of the NUMBER data type. The precision p can range
FLOAT(p)
from 1 to 126.
Date & Time

It is used to store a valid date-time. Its range varies from January 1,


DATE
Types

4712 BC to December 31, 9999 AD.


It is used to store the valid date in YYYY-MM-DD with time hh:mm:ss
TIMESTAMP
format.
Since blobs can store binary data, they can be used to store images
BLOB
Large Object

or other multimedia files. Its range goes up to 4 GB.


Types

It is used to store binary data in an external file. Its range goes up to


BFILE
4 GB.
CLOB It is used for single-byte character data. Its range goes up to 4 GB.

SQL Commands

SQL commands are instructions. They are used to communicate with the

database. They can perform various tasks like create a table, add data to tables, drop
the table, modify the table, set permission for users.

SQL Commands are categorized into 4 types. They are:

1. DDL (Data Definition Language)

2. DML (Data Manipulation Language)


3. DCL (Data Control Language)

4. TCL (Transaction Control Language)

1. DDL Commands

Ch. Vijayananda Ratnam@Dept. of CSE 2


Database Management Systems UNIT-IV
These are used to create database and its objects like tables, views, indices,

sequences, synonyms, etc., All the DDL statements are auto committed that means it

permanently saves all the changes in the database. Commands that come under DDL:

1. CREATE

2. ALTER
3. DROP

4. TRUNCATE

5. RENAME

1. 1 Create: Create is used for creating the database or its objects (Ex: table, view,
index, function, store procedure and triggers).

1. Table - to store data.


2. View - to project data in a desired format from one or more tables

3. Sequence - to generate numeric values.


4. Index - to improve performance of queries on the tables.

5. Synonym - alternative name of an object.

CREATE TABLE Statement:

It is used to create tables in the database. The table gets created as soon as the
CREATE TABLE script is executed and is ready to hold the data and its syntax is:
create table <table-name> (
column1 data_type (size) column_constraint,
column2 data_type (size) column_constraint,
.
.
.
columnN data_type (size) column_constraint,
table_constraint
);
In this syntax:

• First, specify the table name.


• Second, list all columns within the parentheses. In case a table has multiple

columns, you need to separate them by commas (,). A column definition

includes the column name followed by data type and its size and a column

constraint such as NOT NULL, primary key, check.


Ch. Vijayananda Ratnam@Dept. of CSE 3
Database Management Systems UNIT-IV
• Third, add any table-level constraints if applicable e.g., primary key, foreign key,

check.

Example: The following example shows how to create a new table named student

which has four columns: rollno, sname, gender, and cgpa.


create table student (
rollno number(2),
sname varchar2(20),
gender char(5),
cgpa number(5,2));

The DESCRIBE Command

DESCRIBE is most often used to view the definition of a table or a view. It gives
you a list of columns in the table or view, along with its datatypes, lengths, and

nullability. Enter the command DESC followed by the name of the table or view you
are interested in, as the following example shows:
desc student;
Column Null? Type
--------------------- ------ ----------------
ROLLNO - NUMBER(2,0)

SNAME - VARCHAR2(20)
GENDER - CHAR(5)

CGPA - NUMBER(5,2)

1. 2 ALTER TABLE Statement

ALTER TABLE is used to change the table structure or column definitions after the table
has been created in the database. It is used to add, drop, rename, and modify a

column in a table. The following illustrates the syntax:


ALTER TABLE <table-name> action;
Where table-name which you want to modify and the action that you want to perform

on the table.

Example:

We will use the student table that we created previously to demonstrate ALTER TABLE
Statement.

Ch. Vijayananda Ratnam@Dept. of CSE 4


Database Management Systems UNIT-IV

a. Syntax to add a new column to a table:


alter table <table-name> add column-name datatype(size) constraint;

For example, to add new column named dob to the student table:
alter table student add dob date;

Now dob column is appended at the end of the column list; you can view this by

applying desc on student table.

To add multiple columns, place the new columns inside the parenthesis separated

by commas as follows:
alter table <table-name> add (
column_name data-type(size) constraint,
column_name data-type(size) constraint,
...
);

Example:
alter table student add (
phone VARCHAR(20),
email VARCHAR(100)
);
Above statement adds two new columns named phone and email to the student
table.

b. Syntax to modify the existing column:


MODIFY option allows us to change the datatype, increase/decrease the size
of a column. When you want to change the datatype or decrease the size, the column

should be empty. Use the following syntax to modify the column properties:

alter table <table-name> modify column_name datatype(new-size) constraint;

For example, the following statement changes the phone column datatype to

number and its size to 10:

alter table student modify phone number(10);

Ch. Vijayananda Ratnam@Dept. of CSE 5


Database Management Systems UNIT-IV

Use desc command to view the changes. To modify multiple columns, place the

columns to be modified inside the parenthesis separated by commas.

c. Syntax to drop the existing column:


DROP option deletes the column from the table structure and the data stored

in that column. syntax to remove an existing column from a table is:


alter table <table-name> drop column <column-name>;

Example 1: To drop dob column from student table:


alter table student drop column dob;

Example 2: To drop multiple columns from student table:


alter table student drop (phone, email);

d. Syntax to rename the existing column:


alter table <table-name> rename column <column-name> to <new-name>;

Example:
alter table student rename column rollno to sid;

Above statement renames the column named rollno to sid.

e. Syntax to rename the table:

Use the following syntax to give new name to table:


alter table <table_name> rename to <new_table_name>;

Example: Below statement renames the table named student to std:


alter table student rename to std;

1.3 DROP TABLE Statement


The DROP TABLE statement is used to delete an existing table in a database. The

dropped table and its data remain no longer available for selection. Dropped table can

be recovered using FLASHBACK utility, if available in recyclebin. Dropping a table drops

the index and triggers associated with it. Its syntax is:

Ch. Vijayananda Ratnam@Dept. of CSE 6


Database Management Systems UNIT-IV
drop table <table_name> [PURGE];

Example 1: The below statement will drop the table and place it into the recyclebin.
drop table student;

Example 2: The below statement will drop the table and flush it out from the

recyclebin also.
drop table student purge;

1. 4 TRUNCATE TABLE Statement

The TRUNCATE TABLE statement is used to delete the data inside a table, but not the
table itself and its syntax is:
truncate table <table_name>;
Example:
truncate table student;

Now, the STUDENT table is truncated and the output from SELECT statement will be

as shown like −

select * from student;


no data found.

1. 5 RENAME Statement
RENAME table statement to rename an existing table in the database and

cannot be rollback. Its syntax is:


rename <table_name> to <new_name>;
Where
table_name is the name of the existing table which you want to rename and

new_name is the new table name.

Example:
rename student to std;

2. DML Statements
Commands that modify data are called data manipulation language commands.
Ch. Vijayananda Ratnam@Dept. of CSE 7
Database Management Systems UNIT-IV
2.1 INSERT: An INSERT statement is used to add new rows of data to a table and its

syntax is:
insert into <table_name> (column_list) values(value_list);
• First, specify the name of the table into which you want to insert.
• Second, specify a list of comma-separated column names within parentheses.

• Third, specify a list of comma-separated values that corresponds to the column

list.

• Character type values are always enclosed in single quotes; number values are
never in quotes;

• Date values are often (but not always) in the format ‘dd-mm-yyyy’ (for

example, ’12-06-2016’).

If the value list has the same order as the table columns, you can skip the column list
insert into table_name values (value_list);

Example 1: Let us create student table once again and try insert data into it by using

insert statement:
create table student (
rollno number(3),
sname varchar2(15),
cgpa number(5,2),
dob date);

insert into student (rollno, sname, cgpa, dob) values (501,'Akshara',87.5,


to_date('25-10-2002','dd-mm-yyyy'));

(or)
insert into student values (501,'Akshara',87.5, to_date('15-10-2002','dd-
mm-yyyy'));

After you execute the INSERT statement, the message “1 row(s) inserted” is displayed,

indicating that data has been inserted in the table. To verify that the row was added,

you can use a SELECT statement to view the table’s contents.


SQL> select * from student;
ROLLNO SNAME CGPA DOB
---------- --------------- ---------- ---------
501 Akshara 75.9 15-OCT-02
Ch. Vijayananda Ratnam@Dept. of CSE 8
Database Management Systems UNIT-IV

The following statements insert new rows into the student table:

insert into student values (555,'Yaswanth',85.3, to_date('25-07-2015','dd-


mm-yyyy'));

insert into student values (517,'Krishna',77.8, to_date('05-07-2000','dd-mm-


yyyy'));

insert into student values (510,'Gokul',66.7, to_date('18-12-2001','dd-mm-


yyyy'));

insert into student values (526,'Chaitanya',65.6, to_date('09-07-2001','dd-


mm-yyyy'));

See the following result:


select * from student;

ROLLNO SNAME CGPA DOB


---------- --------------- ---------- ---------
501 Akshara 75.9 15-OCT-02
555 Yaswanth 85.3 25-JUL-15
517 Krishna 77.8 05-JUL-00
510 Gokul 66.7 18-DEC-01
526 Chaitanya 65.6 09-JUL-01

2.2 UPDATE: It is used to change the existing rows in a table and its syntax is:

UPDATE <table_name> SET column_name1 = new_value1,…


WHERE condition;

Let's have a look at the UPDATE statement.


• First, The UPDATE clause identifies the name of the table which you want to
update.
• Second, The SET clause identifies the columns to be changed and the new
values to be assigned to these columns.
• Third, the optional WHERE clause determines which rows of the table should be
updated. If you omit it, the UPDATE statement will update all rows of the table.
Example 1- Update single column
Let's look at a very simple UPDATE example.
UPDATE student SET dob = '05-05-2001' WHERE rollno = 555;
This will update the dob to '05-05-2001' in the student table where the rollno is 555.
You can check it by executing select * from student;
ROLLNO SNAME CGPA DOB
------- ---------- ------- ----------
501 Akshara 75.9 15-OCT-02
555 Yaswanth 85.3 05-MAY-01

Ch. Vijayananda Ratnam@Dept. of CSE 9


Database Management Systems UNIT-IV
517 Krishna 77.8 05-JUL-00
510 Gokul 66.7 18-DEC-01
526 Chaitanya 65.6 09-JUL-01

Example 2- Update multiple columns


Let's us consider an example where you might want to update more than one
column with a single UPDATE statement.
UPDATE student SET sname= 'Raju', cgpa = 86.5 WHERE rollno = 510;

This would update the sname to ‘Raju' and cgpa to 86.5 where the rollno is 510. You

can check it by executing select * from student;


ROLLNO SNAME CGPA DOB
------- --------- ------- ---------
501 Akshara 75.9 15-10-12
555 Yaswanth 85.3 05-05-01
517 Krishna 77.8 05-07-00
510 Raju 86.5 18-12-01
526 Chaitanya 65.6 09-07-01

Example 3- Updating all rows


Let's us consider an example where you might want to update all rows in table
with a single UPDATE statement.
UPDATE student SET cgpa = 76.5;

This would update all the rows of cgpa to 76.5. You can check it by executing
select * from student;
ROLLNO SNAME CGPA DOB
------- --------- ------- ---------
501 Akshara 76.5 15-10-12
555 Yaswanth 76.5 05-05-01
517 Krishna 76.5 05-07-00
510 Raju 76.5 18-12-01
526 Chaitanya 76.5 09-07-01

2.3 DELETE: Delete statement used to delete one or more rows from a table and its
syntax is:
DELETE FROM <table_name> WHERE condition;
In this statement,

• First, you specify the name of the table from which you want to delete data.

• Second, the optional WHERE clause determines which rows of the table should
be deleted. If you omit it, the DELETE statement will remove all rows from the
table.

Ch. Vijayananda Ratnam@Dept. of CSE 10


Database Management Systems UNIT-IV
Note: It is faster and more effective to use the TRUNCATE TABLE statement to remove
all rows in a large table.

Example - 1: Deleting one row


The following statement deletes a row whose rollno is 510.
DELETE FROM student WHERE rollno = 510;
The WHERE clause identifies the exact record— where rollno is equal to 510 — to be
removed from the STUDENT table. After the record is deleted, the row for 'Raju' no
longer exists in the table.
select *from student;
ROLLNO SNAME CGPA DOB
------ ----------- ------- ----------
501 Akshara 76.5 15-OCT-12
555 Yaswanth 76.5 05-MAY-01
517 Krishna 76.5 05-JUL-00
526 Chaitanya 76.5 09-JUL-01

Example -2: Deleting all rows


The following example deletes all rows from the student table:
DELETE FROM student;
and we got 4 rows deleted.

2. 4 SELECT
Most of the SQL operations performed on a database in a typical organization
are SELECT statements, which enable users to retrieve data from tables. In essence,
the SELECT statement asks the database a question, that’s why it’s also known as a
query.

The syntax for an SQL statement is the basic structure, or rules, required to
execute the statement. Below shows the syntax for the SELECT statement.

SELECT [DISTINCT | UNIQUE] (*, column_name [AS alias],… )


FROM <table_name>
[WHERE condition]
[GROUP BY group_by_expression]
[HAVING condition]
[ORDER BY column_name];

In this statement:

Ch. Vijayananda Ratnam@Dept. of CSE 11


Database Management Systems UNIT-IV
• The only clauses required for the SELECT statement are SELECT and FROM. so
they are the only clauses discussed here.
• the optional WHERE clause determines which rows of the table should be
selected. If you omit it, all rows from the table will be retrieved.
• Square brackets indicate optional portions of the statement.
• An SQL statement should be ended with a semicolon.

Examples on select statement:


Statement Description Basic Syntax Example
SELECT * FROM SELECT *FROM
Statement to view all <table_name>; student;
columns of a table. * → refers all columns in a
table.
Statement to view one SELECT col_name FROM SELECT sname FROM
column of a table. <table_name>; student;

Statement to view SELECT sname, cgpa


SELECT col_name, col_name, FROM student;
multiple columns of a
... FROM <table_name>;
table.
SELECT sname AS
Statement to assign an names FROM student;
SELECT col_name [AS] alias
alias to a column during (or)
FROM <table_name>; SELECT sname names
display
FROM student;
Statement to perform SELECT rollno-
SELECT arithmetic expression 2*cgpa FROM
arithmetic operations
FROM <table_name>; student;
during retrieval.
SELECT DISTINCT col_name SELECT DISTINCT
cgpa FROM student;
FROM <table_name>;
Statement to eliminate (or)
(or)
duplicates in output SELECT UNIQUE cgpa
SELECT UNIQUE col_name FROM student;
FROM <table_name>;
Statement to perform SELECT sname ||
concatenation of SELECT col_name || col_name cgpa FROM student;
column contents during FROM <table_name>;
display.
What is a constraint and why should we use?
Constraints are nothing but conditions which are used to enforce business rules,
practices, and policies to ensure the accuracy and integrity of data.
(or)

Ch. Vijayananda Ratnam@Dept. of CSE 12


Database Management Systems UNIT-IV
A constraint is a rule that you define on a table that restricts the values in that table.

Constraints are used to limit the type of data that can go into a table. This
ensures the accuracy and reliability of the data in the table. If there is any violation
between the constraint and the data action, the action is aborted. So, constraints are
created to protect the integrity of the data and improve the quality of the data.

Constraints can be column level or table level. Column level constraints apply to
a column, and table level constraints apply to the whole table.

Constraints can be specified when the table is created with the CREATE TABLE
statement, or after the table is created with the ALTER TABLE statement.

There are five different types of SQL constraints. They are:


1. NOT NULL
2. UNIQUE
3. PRIMARY KEY
4. CHECK
5. FOREIGN KEY

Syntax for adding constraints at column level:


create table table_name (
...,
column_name data_type constraint constraint_name
constraint_type,
...
);

Where constraint is a keyword, constraint_name is user defined name and


constraint_type is type of constraint like unique, not null , primary key, etc.

Syntax for adding constraints at table level:


create table table_name (
column_name1 data_type,
...
...
column_name data_type,
constraint constraint_name constraint_type(column_names on
which the constraint is imposed),
...
);

1. NOT NULL Constraint


Ch. Vijayananda Ratnam@Dept. of CSE 13
Database Management Systems UNIT-IV
The NOT NULL constraint specifies that a column cannot contain NULL values.

This enforces a field to always contain a value, which means that you cannot insert a
new record, or update a record without adding a value to this field.

The NOT NULL constraints are inline constraints which are typically used in the column
definition of the CREATE TABLE statement.

EXAMPLE:
create table student (
sno number,
sname varchar2(20) constraint nn_sname NOT NULL
);

It is also possible to add a NOT NULL constraint to an existing table by using the ALTER
TABLE statement.
Syntax:
alter table <table-name> modify (column_name NOT NULL);

Example:
alter table student modify sname not null;

Below insert statement will validate the above not null constraint.
insert into student values(1,null);

insert into student values(1,null)


*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("STUDENT"."SNAME").

2. UNIQUE Constraint
The UNIQUE constraint ensures that values stored in a column or group of columns are
different or NULL values.

They could be added to the table as inline (column level) and out-of-line(table level).

Example (For column level):


create table student (
sno number constraint unq_sid UNIQUE,
sname varchar2(20),
aadhar number constraint unq_adhar UNIQUE
);

Example (For table level):


create table student (
Ch. Vijayananda Ratnam@Dept. of CSE 14
Database Management Systems UNIT-IV
sno number,
sname varchar2(20),
aadhar number,
constraint unq_sno_adhar UNIQUE(sno,aadhar)
);

In the above statement, for both columns will have one constraint or you can also create
two separate statements like

constraint unq_sno UNIQUE(sno),


constraint unq_adhar UNIQUE(aadhar)

It is also possible to add a UNIQUE constraint to an existing table by using the ALTER
TABLE statement.

Syntax:
alter table <table-name> add constraint constraint_name
constraint_type(col_1, ..., col_N);

Example:
alter table student add constraint unq_sno unique(sno);

Below insert statements will validate the above UNIQUE constraint.


insert into student values(1, null, null);
insert into student values(1, null, null); //duplicate value is not allowed.

*
ERROR at line 1:
ORA-00001: unique constraint (UNQ_SNO) violated.

Next, UNIQUE constraint column can accept NULL values.

3. PRIMARY KEY Constraint


The PRIMARY KEY Constraint uniquely identifies each record in a table. It is
combination of UNIQUE & NOT NULL Constraints.

The primary key column should be filled and allows only unique values. A table can

have only ONE primary key and it can be created for composite (Two or more columns) keys
also.

The following are rules that make a column a primary key:


• A primary key column cannot contain a NULL value or an empty string.
• A primary key value must be unique within the entire table.
• A primary key value should not be changed over time.

Ch. Vijayananda Ratnam@Dept. of CSE 15


Database Management Systems UNIT-IV

Example (For column level):


create table student (
sno number constraint pk_sid PRIMARY KEY,
sname varchar2(20)
);

Example (For table level):


create table student (
sno number,
sname varchar2(20),
constraint pk_sno PRIMARY KEY(sno,sname)
);

In the above statement primary key is called as composite primary key since it is a
combination of two columns.

It is also possible to add a PRIMARY KEY constraint to an existing table by using the
alter table statement.

Example:
alter table student add constraint pk_sno primary key(sno);

Below insert statements will validate the above PRIMARY KEY constraint.
insert into student values(1, null);
insert into student values(1, null); //duplicate value is not allowed.
*
ERROR at line 1:
ORA-00001: unique constraint (PK_SNO) violated.

insert into student values(null, "Chaitanya");


ORA-01400: cannot insert NULL into ("STUDENT"."SNO").

4. CHECK Constraint

The CHECK constraint is used to limit the values that can be placed in a column.
If you define a CHECK constraint on a column it will allows only certain values for this
column.

To create a CHECK constraint, you define a logical expression to validate the


data that is being inserted or updated. If the expression evaluates to true, Oracle
accepts the data otherwise rejects.

Ch. Vijayananda Ratnam@Dept. of CSE 16


Database Management Systems UNIT-IV
Syntax:
CREATE TABLE table_name (
...
column_name data_type CHECK (expression),
...
);

Note: for table level refer the general table – level syntax.

Example:
create table student
(
sid integer,
sname varchar2(20),
gender char(1) constraint chk_gen check( gender in ('M','F'))
);

For gender column, it allows only ‘M’ and ‘F’ values other than these values will be
rejected.

Below insert statement will validate the above CHECK constraint.


insert into student values(1, 'vijay','G');
*
ERROR at line 1:
ORA-02290: check constraint (CHK_GEN) violated

PL/SQL
An exception is a PL/SQL error that is raised during program execution, either implicitly
or explicitly by your program. The exceptions can have different causes such as coding
mistakes, bugs, even hardware failures.

It is not possible to anticipate all potential exceptions, however, you can write code to
handle exceptions to enable the program to continue running as normal.

The code that you write to handle exceptions is called an exception handler.
PL/SQL Exception message consists of three parts.
1. Type of Exception
2. An Error Code
3. A message

By handling the exceptions, we can ensure a PL/SQL block does not exit abruptly. Here
is the basic syntax of the exception-handling section:
Ch. Vijayananda Ratnam@Dept. of CSE 17
Database Management Systems UNIT-IV

BEGIN
-- executable section
...
-- exception-handling section
EXCEPTION
WHEN e1 THEN
-- exception_handler1
WHEN e2 THEN
-- exception_handler1
WHEN OTHERS THEN
-- other_exception_handler
END;
/
In this syntax, e1, e2 are exceptions. When an exception occurs in the executable
section, the execution of the current block stops and control transfers to the exception-
handling section.

If the exception e1 occurred, the exception_handler1 runs. If the exception e2


occurred, the exception_handler2 executes. In case any other exception raises, then
the other_exception_handler runs.

Example
Let us understand with help of EMP table we had created and used previously.
DECLARE
e_id emp.empno%type :=&e_id;
e_name emp.ename%type;
e_design emp.job%type;
BEGIN
SELECT ename, job INTO e_name, e_design FROM emp
WHERE empno = e_id;
DBMS_OUTPUT.PUT_LINE ('Name: '|| e_name);
DBMS_OUTPUT.PUT_LINE ('Designation: ' || e_design);
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('No such employee!');
WHEN others THEN
dbms_output.put_line('Error!');
END;
/
When the above code is executed at the SQL prompt, it produces the following result
No such employee!

PL/SQL procedure successfully completed.


Ch. Vijayananda Ratnam@Dept. of CSE 18
Database Management Systems UNIT-IV

Types of exceptions

There are two types of exceptions. They are:

1. Pre-defined Exceptions

2. User-defined Exceptions

Pre-defined Exceptions

PL/SQL predefines some common errors as exceptions. For example, PL/SQL

raises the predefined exception NO_DATA_FOUND if a SELECT INTO statement returns

no rows.
PL/SQL declares predefined exceptions globally in package STANDARD, which

defines the PL/SQL environment. So, you need not declare them yourself. You can write

handlers for predefined exceptions using the names in the following list:

Name Raised when…


CURSOR_ALREADY_OPEN When you open a cursor that is already open.
INVALID_CURSOR When you perform an invalid operation on a cursor like closing an
unopened cursor, fetch data from an unopened cursor.
NO_DATA_FOUND A SELECT INTO statement returns no rows.
TOO_MANY_ROWS A SELECT INTO statement returns more than one row.
ZERO_DIVIDE When you attempt to divide a number by zero.

******** End of Unit - 4 ********

Ch. Vijayananda Ratnam@Dept. of CSE 19

You might also like