RECORD vs COLLECTION in PLSQL

In PL/SQL (Oracle’s Procedural Language for SQL), “record” and “collection” have specific meanings related to how data is structured and managed within the PL/SQL program:

Record:

A RECORD is a composite data type in PL/SQL that can hold a single r…


This content originally appeared on DEV Community and was authored by Pranav Bakare

In PL/SQL (Oracle's Procedural Language for SQL), "record" and "collection" have specific meanings related to how data is structured and managed within the PL/SQL program:

  1. Record:

A RECORD is a composite data type in PL/SQL that can hold a single row of data with multiple fields. Each field can have a different data type.

It is similar to a row in a table where each column corresponds to a field in the record.

Example: You can define a RECORD to store details of a single employee with fields like emp_id, emp_name, and emp_salary.

Example in PL/SQL:

DECLARE
TYPE emp_record IS RECORD (
emp_id NUMBER,
emp_name VARCHAR2(50),
emp_salary NUMBER
);
employee emp_record;
BEGIN
employee.emp_id := 101;
employee.emp_name := 'John Doe';
employee.emp_salary := 5000;
END;

  1. Collection:

A COLLECTION is a data structure that can hold multiple values of the same data type or composite types. In PL/SQL, there are three types of collections: INDEX BY tables (associative arrays), VARRAY (variable-size arrays), and NESTED TABLE.

Unlike a RECORD, a collection can store multiple rows (or multiple records). It allows you to handle bulk data efficiently.

Collections are useful for working with sets of data in loops or for batch processing.

Example of a Nested Table in PL/SQL:

DECLARE
TYPE emp_table IS TABLE OF VARCHAR2(50);
employees emp_table;
BEGIN
employees := emp_table('John Doe', 'Jane Smith', 'Michael Brown');
FOR i IN 1..employees.COUNT LOOP
DBMS_OUTPUT.PUT_LINE(employees(i));
END LOOP;
END;

Summary:

A RECORD is used to group related fields into a single unit, representing a row of data.

A COLLECTION is used to store multiple elements (rows, values, or records), allowing batch operations on sets of data.


This content originally appeared on DEV Community and was authored by Pranav Bakare


Print Share Comment Cite Upload Translate Updates
APA

Pranav Bakare | Sciencx (2024-10-12T21:11:22+00:00) RECORD vs COLLECTION in PLSQL. Retrieved from https://www.scien.cx/2024/10/12/record-vs-collection-in-plsql/

MLA
" » RECORD vs COLLECTION in PLSQL." Pranav Bakare | Sciencx - Saturday October 12, 2024, https://www.scien.cx/2024/10/12/record-vs-collection-in-plsql/
HARVARD
Pranav Bakare | Sciencx Saturday October 12, 2024 » RECORD vs COLLECTION in PLSQL., viewed ,<https://www.scien.cx/2024/10/12/record-vs-collection-in-plsql/>
VANCOUVER
Pranav Bakare | Sciencx - » RECORD vs COLLECTION in PLSQL. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/12/record-vs-collection-in-plsql/
CHICAGO
" » RECORD vs COLLECTION in PLSQL." Pranav Bakare | Sciencx - Accessed . https://www.scien.cx/2024/10/12/record-vs-collection-in-plsql/
IEEE
" » RECORD vs COLLECTION in PLSQL." Pranav Bakare | Sciencx [Online]. Available: https://www.scien.cx/2024/10/12/record-vs-collection-in-plsql/. [Accessed: ]
rf:citation
» RECORD vs COLLECTION in PLSQL | Pranav Bakare | Sciencx | https://www.scien.cx/2024/10/12/record-vs-collection-in-plsql/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.