Docsity

Unit 5 Data Structure, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
UNIT 5: PL/SQL COMPOSITE DATA TYPES
PL/SQL Composite Data Types: Records–Tables–arrays. Named Blocks: Procedures–
Functions Packages –Triggers–Data Dictionary Views.
1. Composite Data Types in PL/SQL
Definition:
1. Composite data types are used to store multiple values together in a single variable.
2. They can hold different types of data elements.
3. They are useful for handling complex data structures.
4. PL/SQL provides user-defined composite data types.
5. The main composite data types are Records, Tables, and Arrays.
Types of Composite Data Types:
1. Records
2. PL/SQL Tables
3. Arrays
Example:
DECLARE
TYPE student_record IS RECORD
(
roll_no NUMBER,
name VARCHAR2(20)
);
student student_record;
BEGIN
student.roll_no:=101;
student.name:='Rahul';
END;
/
2. Records in PL/SQL
Definition:
1. A record is a composite data type that stores related data items together.
2. A record contains multiple fields with different data types.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Unit 5 Data Structure and more Study notes Data Structures and Algorithms in PDF only on Docsity!

UNIT 5: PL/SQL COMPOSITE DATA TYPES

PL/SQL Composite Data Types: Records–Tables–arrays. Named Blocks: Procedures– Functions Packages –Triggers–Data Dictionary Views.

1. Composite Data Types in PL/SQL Definition: 1. Composite data types are used to store multiple values together in a single variable. 2. They can hold different types of data elements. 3. They are useful for handling complex data structures. 4. PL/SQL provides user-defined composite data types. 5. The main composite data types are Records, Tables, and Arrays. Types of Composite Data Types: 1. Records 2. PL/SQL Tables 3. Arrays Example: DECLARE TYPE student_record IS RECORD ( roll_no NUMBER, name VARCHAR2(20) ); student student_record; BEGIN student.roll_no:=101; student.name:='Rahul'; END; / 2. Records in PL/SQL Definition: 1. A record is a composite data type that stores related data items together. 2. A record contains multiple fields with different data types.

  1. Each field can store a different value.
  2. Records are similar to rows in a database table.
  3. Records improve data organization and readability. Features of Records:  Store multiple values.  Fields can have different data types.  Accessed using dot (.) notation.  Can represent a complete row.  Can be user-defined. Syntax: TYPE record_name IS RECORD ( field1 datatype, field2 datatype ); variable_name record_name; Example: DECLARE TYPE student_rec IS RECORD ( roll_no NUMBER, name VARCHAR2(20), age NUMBER ); student student_rec; BEGIN student.roll_no:=101; student.name:='Anu'; student.age:=20; DBMS_OUTPUT.PUT_LINE(student.name); END; / Output: Anu

Syntax: TYPE table_name IS TABLE OF datatype INDEX BY BINARY_INTEGER; variable_name table_name; Example: DECLARE TYPE names_table IS TABLE OF VARCHAR2(20) INDEX BY BINARY_INTEGER; names names_table; BEGIN names(1):='Rahul'; names(2):='Anu'; DBMS_OUTPUT.PUT_LINE(names(1)); END; / Output: Rahul

5. Operations on PL/SQL Tables Definition: 1. PL/SQL tables support various operations for managing data. 2. Elements are accessed using indexes. 3. Values can be inserted, updated, and deleted. 4. Collection methods are used to manage table data. 5. They provide efficient data processing. Important Methods: Method Purpose FIRST Returns first index LAST Returns last index NEXT Returns next index DELETE Deletes elements COUNT Counts elements

Example: DECLARE TYPE emp_table IS TABLE OF VARCHAR2(20) INDEX BY BINARY_INTEGER; emp emp_table; BEGIN emp(1):='John'; emp(2):='Smith'; DBMS_OUTPUT.PUT_LINE(emp.COUNT); END; / Output: 2

6. Arrays in PL/SQL Definition: 1. Arrays store multiple values of the same data type. 2. Each value is identified using an index number. 3. They allow easy access to stored elements. 4. Arrays are useful for processing large amounts of data. 5. PL/SQL arrays are implemented using collections. Features:  Store similar data elements.  Accessed by index.  Faster data processing.  Reduces repeated variables.  Supports collection operations. Syntax: TYPE array_name IS VARRAY(size) OF datatype; variable_name array_name; Example:

  1. Procedures do not return a value directly. Syntax: CREATE OR REPLACE PROCEDURE procedure_name (parameters) IS BEGIN statements; END; / Example: CREATE OR REPLACE PROCEDURE display_message IS BEGIN DBMS_OUTPUT.PUT_LINE('Hello PL/SQL'); END; / Execute: EXEC display_message; Output: Hello PL/SQL 9. Functions in PL/SQL Definition:
  2. A function is a named PL/SQL block that returns a value.
  3. Functions can accept parameters.
  4. They must contain a RETURN statement.
  5. Functions can be used in SQL expressions.
  6. They improve code reusability. Syntax: CREATE OR REPLACE FUNCTION function_name RETURN datatype

IS

BEGIN

RETURN value; END; / Example: CREATE OR REPLACE FUNCTION get_square(num NUMBER) RETURN NUMBER IS BEGIN RETURN num*num; END; / Calling function: SELECT get_square(5) FROM dual; Output: 25

10. Packages in PL/SQL Definition: 1. A package is a collection of related procedures, functions, variables, and cursors. 2. Packages are stored as database objects. 3. They provide modular programming. 4. Packages contain specification and body. 5. They improve security and performance. Components: 1. Package Specification 2. Package Body Syntax: CREATE PACKAGE package_name

CREATE OR REPLACE TRIGGER check_insert BEFORE INSERT ON STUDENT BEGIN DBMS_OUTPUT.PUT_LINE('Record Inserted'); END; /

12. Data Dictionary Views Definition: 1. Data dictionary contains information about database objects. 2. It stores metadata about tables, users, procedures, and privileges. 3. Oracle automatically maintains data dictionary information. 4. Users can access information through dictionary views. 5. It helps administrators manage databases. Common Data Dictionary Views: View Purpose USER_TABLES Information about tables USER_OBJECTS Database objects USER_PROCEDURES Stored procedures USER_TRIGGERS Triggers USER_SOURCE PL/SQL source code Example: Display tables: SELECT TABLE_NAME FROM USER_TABLES; Display procedures: SELECT OBJECT_NAME FROM USER_OBJECTS WHERE OBJECT_TYPE='PROCEDURE';