Download presentation
Presentation is loading. Please wait.
Published byBeverley Farmer Modified over 9 years ago
1
Program with PL/SQL Lesson 5
2
Working with Composite Data Types
3
Composite Data Types Are of two types –PL/SQL RECORDS –PL/SQL Collections INDEX BY Table Nested Table VARRAY Contain internal components Are reusable
4
PL/SQL Records Must contain one or more components of any scalar, RECORD, or INDEX BY table data type, called fields Are similar in structure to records in a third generation language (3GL) Are not the same as rows in a database table Treat a collection of fields as a logical unit Are convenient for fetching a row of data from a table for processing
5
Creating a PL/SQL Record Syntax: Where field_declaration is: TYPE type_name IS RECORD (field_declaration[, field_declaration] …); Identifiertype_name; field_name{field_type | variable%TYPE | table.column%TYPE | table%ROWTYPE} [[NOT NULL] {:= | DEFAULT} expr]
6
PL/SQL Record Structure 100KingAD_PRES Field1 (data type)Field2 (data type)Field3 (data type) Field1 (data type) Field2 (data type)Field3 (data type) Employee_id number(6) Last_name varchar2(25)Job_id varchar2(10) Example:
7
The %ROWTYPE Attribute Declare a variable according to a collection of columns in a database table or view Prefix %ROWTYPE with the database table Fields in the record take their names and data types from the columns of the table or view
8
Advantages of Using %ROWTYPE The number and data types of the underlying database columns need not be known The number and data types of the underlying database column may change at run time The attribute is useful when retrieving a row with the SELECT * statement
9
INDEX BY Tables Are composed of two components: –Primary key of data type BINARY_INTEGER –Column of scalar or record data type Can increase in size dynamically because they are unconstrained
10
Creating an INDEX BY Table Syntax: Declare an INDEX BY table to store names Example: TYPE type_name IS TABLE OF {column_type | variable%TYPE | table.column%TYPE} [NOT NULL] | table.%ROWTYPE [INDEX BY BINARY_INTEGER]; Identifiertype_name;... TYPE ename_table_type IS TABLE OF employee.last_name%TYPE INDEX BY BINARY_INTEGER; Ename_tableename_table_type;
11
INDEX BY Table Structure... 1 2 3 Jones Smith Maduro... Unique identifier BINARY_INTEGER Column Scalar
12
Using INDEX BY Table Methods The following methods make INDEX BY tables easier to use: –EXISTS –COUNT –FIRST and LAST –PRIOR –NEXT –TRIM –DELETE
13
Writing Executable Statement
14
PL/SQL Block Syntax and Guidelines Statements can be split across line, but keywords must not split Lexical units can be classified as: –Delimiters –Identifiers –Literals Character and date literals must be enclosed in single quotation marks Numbers can be simple values or scientific notation –Comments
15
Identifiers Can contain up to 30 characters Must begin with an alphabetical character Can contain numerals, dollar signs, underscores, and nuber signs Cannot contain characters such as hyphens, slashes, and spaces Should not have the same name as a database table column name Should not be reserved words
16
Commenting Code Prefix single-line comments with two dashes (--) Place multiple-line comments between the symbols /* and */
17
SQL Functions in PL/SQL Available in procedural statements: –Single-row number –Single-row character –Data type conversion –Date –Timestamp –GREATEST and LEAST –Miscellaneous functions Not available in procedural statements –DECODE –Group functions
18
Nested Blocks and Variable Scope PL/SQL blocks can be nested wherever an executable statement is allowed A nested block becomes a statements An exception section can contain nested blocks The scope of an identifier is that region of a program unit (block, subprogram, or package) from which you can reference the identifier
19
Identifier Scope An identifier is visible in the regions where you can reference the identifier without having to qualify it: –A block can look up to the enclosing block –A block cannot look down to enclosed blocks
20
Qualify an Identifier The qualifier can be the label of an enclosing block Qualify an identifier by using the block label prefix
21
Determining Variable Scope > DECLARE v_salNUMBER(7,2) := 60000; v_commNUMBER(7,2) := v_sal * 0.20; v_messageVARCHAR2(255) := ‘eligible for commision’; BEGIN DECLARE v_salNUMBER(7,2) := 50000; v_commNUMBER(7,2) := 0; v_total_compNUMBER(7,2) := v_sal + v_comm; BEGIN v_message := ‘CLERK not’||v_message; outer.v_comm := v_sal * 0.30; END; v_message := ‘SALESMAN’||v_message; END;
22
Operators in PL/SQL Logical Arithmetic Concatenation Parentheses to control order of operations Exponential operator (**)
23
Rules of Precedence OperatorOperation **Exponentiation +, -Identity, negation *, /Multiplication, division +. -, ||Addition, subtraction, concatenation =,, =, <>, !=, ~=, ^=, IS NULL, LIKE, BETWEEN, IN Comparison NOTLogical negation ANDConjunction ORInclusion
24
Programming Guidelines Make code maintenance easier by: Documenting code with comments Developing a case convention for tho code Developing naming conventions for identifies and other objects Enhancing readability by indenting
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.