0% found this document useful (0 votes)
1 views11 pages

RDBMSunit-5 240926 214936

This document provides an overview of composite data types in PL/SQL, specifically focusing on collections and records. It discusses the types of collections available, such as Index-by Tables, Nested Tables, and VARRAY, as well as the different types of records including Table-based, Cursor-based, and Programmer-defined records. Additionally, it covers the creation of procedures and functions, the structure of packages, triggers, and the use of data dictionary views in PL/SQL.

Uploaded by

im4517894
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)
1 views11 pages

RDBMSunit-5 240926 214936

This document provides an overview of composite data types in PL/SQL, specifically focusing on collections and records. It discusses the types of collections available, such as Index-by Tables, Nested Tables, and VARRAY, as well as the different types of records including Table-based, Cursor-based, and Programmer-defined records. Additionally, it covers the creation of procedures and functions, the structure of packages, triggers, and the use of data dictionary views in PL/SQL.

Uploaded by

im4517894
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/ 11

UNIT – 5

Composite datatypes

PL/SQL data types has been broadly classified in two category - Scalar data
types and Composite datatypes. In previous post we discussed about scalar data
type which cover NUMBER , CHAR, VARCHAR2, LONG, etc types. The main
agenda of this post is to discuss one of the composite types in PL/SQL collections.

A composite data type stores values that have internal components and
internal components can be either scalar or composite. Internal components can be
of same data type and different data type.

PL/SQL allows us to define two kinds of composite data types :

1. Collection - The internal components must have the same data type and we can
access each element of a collection variable by its unique index, with this syntax:
variable_name(index).

2. Record - The internal components can have different data types and we can
access each field of a record variable by its name, with this syntax:

variable_name.field_name. Detailed discussion of Records in PL/SQL.

Collections in PL/SQL

Oracle provides three types of collections.

1. Index-by Table(associate array),

2. Nested Tables, and

3. VARRAY.

All these collections are like a single dimension array. Syntax of collection
declaration is as follows:-

TYPE type IS -- type is collection variable name, a valid identifier

{ assoc_array_type_def

| varray_type_def
| nested_table_type_def

};

Collection can be created in following ways :

1. Defines a collection type and then declare a variable of that type.

2. Use %TYPE to declare a collection variable of the same type as a previously


declared collection variable.

Record Type - Table-based, Cursor based and programmer defined

A record is a composite data structure composed of more than one atomic


elements/fields and each with its own value.In PL/SQL, Record type is local
type(declared in PL/SQL block or in package). Records in PL/SQL are very similar
in concept and structure to the rows of a database table. Each filed of record can be
accessed by name.

Different types of PL/SQL Record :-

We have three different types of records.

1. Table based - A record based on structure of columns of database tables.

(TABLE%ROWTYPE)

2. Cursor based - A record based on the cursor's SELECT statement.

(CURSOR%ROWTYPE)

3. Programmer/Used defined records - Programmer defines structure of record.

Lets see each of them one by one and understand how it is used in PL/SQL.

Table based record :- A record based on table type(termed as table record) is


created using %ROWTYPE attribute of table. general syntax is as follows:

--rcord_name is a valid identifier, table_name is database table

<record_name> <table_name>%ROWTYPE;
Note:- %TYPE provides structure of a column of table, where as %ROWTYPE
provides the datatypes of each of the columns in a table for the record's fields.

Following code declares a table record based on LOCATIONS table.

DECLARE

--TABLE BASED RECORD

V_LOC_REC LOCATIONS%ROWTYPE;

BEGIN

SELECT * into V_LOC_REC FROM LOCATIONS WHERE


POSTAL_CODE =

'500081';

DBMS_OUTPUT.PUT_LINE(V_LOC_REC.STREET_ADDRES
S || ' '

|| V_LOC_REC.STATE_PROVINCE);

END;

Procedures in PL/SQL

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

● At the schema level

● Inside a package

● Inside a PL/SQL block

At the schema level, subprogram is a standalone subprogram. It is created


with the CREATE PROCEDURE or the CREATE FUNCTION statement. It is
stored in the database and can be deleted with the DROP PROCEDURE or DROP
FUNCTION statement.

A subprogram created inside a package is a packaged subprogram. It is


stored in the database and can be deleted only when the package is deleted with the
DROP PACKAGE statement. We will discuss packages in the chapter 'PL/SQL
Packages'.

PL/SQL subprograms are named PL/SQL blocks that can be invoked with a
set of parameters. PL/SQL provides two kinds of subprograms −

● Functions − These subprograms return a single value; mainly used to compute


and return a value.

● Procedures − These subprograms do not return a value directly; mainly used to


perform an action.

Parts of a PL/SQL Subprogram

Each PL/SQL subprogram has a name, and may also have a parameter list.
Like anonymous PL/SQL blocks, the named blocks will also have the following
three parts

S.No Parts & Description


1 Declarative Part
It is an optional part. However, the declarative part for a subprogram
does not start with the DECLARE keyword. It contains declarations of
types, cursors, constants, variables, exceptions, and nested subprograms.
These items are local to the subprogram and cease to exist when the
subprogram completes execution.
2 Executable Part
This is a mandatory part and contains statements that perform the
designated action.
3 Exception-handling
This is again an optional part. It contains the code that handles run-time
errors.

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 [, ...])]

{IS | AS}

BEGIN

< procedure_body >

END procedure_name;

Functions in PL/SQL

A function is same as a procedure except that it returns a value. Therefore,


all the discussions of the previous chapter are true for functions too.

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];
Packages in PL/SQL

Packages are schema objects that groups logically related PL/SQL types,
variables, and subprograms. A package will have two mandatory parts −

● Package specification

● Package body or definition

Package Specification

The specification is the interface to the package. It just DECLARES the


types, variables, constants, exceptions, cursors, and subprograms that can be
referenced from outside the package. In other words, it contains all information
about the content of the package, but excludes the code for the subprograms.

All objects placed in the specification are called public objects. Any
subprogram not in the package specification but coded in the package body is
called a private object.

The following code snippet shows a package specification having a single


procedure. You can have many global variables defined and multiple procedures or
functions inside a package.

CREATE PACKAGE cust_sal AS

PROCEDURE find_sal(c_id customers.id%type);

END cust_sal;

When the above code is executed at the SQL prompt, it produces the following

result −

Package created.

Triggers
A trigger is a stored procedure in a database that 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 specific table
columns are updated. In simple words, a trigger is a collection of SQL statements
with particular names that are stored in system memory. It belongs to a specific
class of stored procedures that are automatically invoked in response to database
server events. Every trigger has a table attached to it.

Because a trigger cannot be called directly, unlike a stored procedure, it is


referred to as a special procedure. A trigger is automatically called whenever a data
modification event against a table takes place, which is the main distinction
between a trigger and a procedure. On the other hand, a stored procedure must be
called directly.

The following are the key differences between triggers and stored procedures:

 Triggers cannot be manually invoked or executed.


 There is no chance that triggers will receive parameters.

A transaction cannot be committed or rolled back inside a trigger.

Syntax:

create trigger [trigger_name]


[before | after]
{insert | update | delete}
on [table_name]
[for each row]
[trigger_body]

Data Dictionary View

In PL/SQL (Procedural Language/SQL), a Data Dictionary View


is a set of predefined views that provide information about the database
schema, objects, users, and other elements of the database structure.
These views are typically read-only and are automatically maintained
by Oracle. The data dictionary views in Oracle are divided into three
categories:

1. USER_ views

These views show information about the objects that are owned
by the current user. The current user can query only the objects that
belong to them.

Examples:

- `USER_TABLES`: Displays tables owned by the user.

- `USER_TAB_COLUMNS`: Displays columns in the user's tables.

- `USER_INDEXES`: Displays indexes owned by the user.

2. ALL_ views

These views provide information about all objects that the current
user has access to (whether owned by the user or not).

Examples:

- `ALL_TABLES`: Displays all tables accessible to the user.

- `ALL_TAB_COLUMNS`: Displays columns in all tables accessible


to the user.

- `ALL_INDEXES`: Displays indexes accessible to the user.

3. DBA_ views
These views are accessible only to users with DBA (Database
Administrator) privileges and show information about all database
objects regardless of ownership or access rights.

Examples:

- `DBA_TABLES`: Displays all tables in the database.

- `DBA_TAB_COLUMNS`: Displays all columns in all tables in the


database.

- `DBA_INDEXES`: Displays all indexes in the database.

Commonly Used Data Dictionary Views

1. USER_TABLES

- Displays all tables owned by the current user.

```sql

SELECT * FROM USER_TABLES;

```

2. USER_TAB_COLUMNS

- Displays columns in the tables owned by the current user.

```sql

SELECT * FROM USER_TAB_COLUMNS WHERE


TABLE_NAME = 'EMPLOYEES';

```

3. USER_CONSTRAINTS

- Displays constraints (like primary keys, foreign keys) on tables.


```sql

SELECT * FROM USER_CONSTRAINTS WHERE


TABLE_NAME = 'EMPLOYEES';

```

4. ALL_OBJECTS

- Provides information about all objects accessible to the current user.

```sql

SELECT OBJECT_NAME, OBJECT_TYPE FROM


ALL_OBJECTS WHERE OWNER = 'HR';

```

5. DBA_USERS

- Lists all users in the database (accessible only to DBAs).

```sql

SELECT USERNAME, ACCOUNT_STATUS FROM


DBA_USERS;

```

Using Data Dictionary Views in PL/SQL

In PL/SQL, you can query these views directly to get information


about the database. Here’s an example of a PL/SQL block that retrieves
all tables owned by the current user and prints their names:

```plsql
BEGIN

FOR rec IN (SELECT table_name FROM USER_TABLES)


LOOP

DBMS_OUTPUT.PUT_LINE('Table: ' || rec.table_name);

END LOOP;

END;

```

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy