RDBMSunit-5 240926 214936
RDBMSunit-5 240926 214936
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.
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:
Collections in PL/SQL
3. VARRAY.
All these collections are like a single dimension array. Syntax of collection
declaration is as follows:-
{ assoc_array_type_def
| varray_type_def
| nested_table_type_def
};
(TABLE%ROWTYPE)
(CURSOR%ROWTYPE)
Lets see each of them one by one and understand how it is used in PL/SQL.
<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.
DECLARE
V_LOC_REC LOCATIONS%ROWTYPE;
BEGIN
'500081';
DBMS_OUTPUT.PUT_LINE(V_LOC_REC.STREET_ADDRES
S || ' '
|| V_LOC_REC.STATE_PROVINCE);
END;
Procedures in PL/SQL
● Inside a package
PL/SQL subprograms are named PL/SQL blocks that can be invoked with a
set of parameters. PL/SQL provides two kinds of subprograms −
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
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
{IS | AS}
BEGIN
END procedure_name;
Functions in PL/SQL
Creating a Function
RETURN return_datatype
{IS | AS}
BEGIN
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 Specification
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.
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.
The following are the key differences between triggers and stored procedures:
Syntax:
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:
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:
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:
1. USER_TABLES
```sql
```
2. USER_TAB_COLUMNS
```sql
```
3. USER_CONSTRAINTS
```
4. ALL_OBJECTS
```sql
```
5. DBA_USERS
```sql
```
```plsql
BEGIN
END LOOP;
END;
```