SlideShare a Scribd company logo
Working with Composite Data Types
Objectives After completing this lesson, you should be able to do the following: Create user-defined PL/SQL records Create a record with the %ROWTYPE attribute Create an INDEX BY table Create an INDEX BY table of records Describe the difference between records, tables, and tables of records.
Composite Data Types Are of two types: PL/SQL RECORDs PL/SQL Collections –  INDEX BY Table –  Nested Table –  VARRAY Contain internal components Are reusable
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
Creating a PL/SQL Record Syntax: field_name { field_type  |  variable%TYPE |  table.column%TYPE  |  table%ROWTYPE } [[NOT NULL] { := | DEFAULT }  expr ] Where  field_declaration  is: TYPE  type_name  IS RECORD ( field_declaration[, field_declaration]…); identifier type_name;
Creating a PL/SQL Record SET SERVEROUTPUT ON DECLARE TYPE EmpRec IS RECORD (  emp_name  VARCHAR2(50), job_title  VARCHAR2(9), salary  NUMBER(7,2)); emp_info EmpRec; BEGIN SELECT first_name||’ ‘||last_name, job_id, salary  INTO emp_info.emp_name, emp_info.job_title, emp_info.salary FROM employees WHERE employee_id = 105; DBMS_OUTPUT.PUT_LINE ('Nhan vien : '||emp_info.emp_name||' lam cong viec '|| emp_info.job_title ||' co muc luong '||emp_info.salary ); END; / Declare variables to store the name, job, and salary of a new employee.
Creating a PL/SQL Record SET SERVEROUTPUT ON DECLARE TYPE EmpRec IS RECORD (  emp_id  employees.employee_id%TYPE, job_title  VARCHAR2(9), salary  NUMBER(7,2)); emp_info  EmpRec; emp_null  EmpRec; emp  EmpRec; BEGIN emp_info.emp_id := 7788; emp_info.job_title := 'ANALYST'; emp_info.salary := 3500; DBMS_OUTPUT.PUT_LINE (‘Ma : ‘||emp_info.emp_id||’-’||emp_info.job_title|| ' - '||emp_info.salary ); emp := emp_info; DBMS_OUTPUT.PUT_LINE (‘Ma : ‘||emp.emp_id||’ – ‘||emp.job_title ||' - '||emp.salary ); emp := emp_null;  -- Tat ca cac field trong emp deu co gia tri null DBMS_OUTPUT.PUT_LINE (‘Ma : ‘||emp.emp_id||’ - '||emp.job_title ||' - '||emp.salary ); END; /
PL/SQL Record Structure
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.
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.
The %ROWTYPE Attribute Examples: Declare a variable to store the information about a department from the DEPARTMENTS table. dept_record departments%ROWTYPE; Declare a variable to store the information about an employee from the EMPLOYEES table. emp_record employees%ROWTYPE;
Creating a PL/SQL Record CREATE TABLE e_temp  AS SELECT * FROM employees WHERE employee_id = 50; SET SERVEROUTPUT ON DECLARE emp_rec employees%ROWTYPE; BEGIN SELECT * INTO emp_rec FROM employees WHERE employee_id = 105; INSERT INTO e_temp  VALUES (emp_rec.employee_id, emp_rec.first_name, emp_rec.last_name,  emp_rec.email, emp_rec.phone_number, emp_rec.hire_date, emp_rec.job_id,  emp_rec.salary, emp_rec.commission_pct, emp_rec.manager_id,  emp_rec.department_id); COMMIT; END; / SELECT * FROM e_temp;
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
Creating an INDEX BY Table TYPE  type_name  IS  TABLE OF { column_type | variable%TYPE | table.column%TYPE } [NOT NULL] | table.%ROWTYPE [INDEX BY BINARY_INTEGER]; identifier  type_name; Syntax: ... TYPE ename_table_type IS TABLE OF  employees.last_name%TYPE INDEX BY BINARY_INTEGER; ename_table ename_table_type; ... Declare an INDEX BY table to store names. Example:
INDEX BY Table Structure
Creating an INDEX BY Table SET SERVEROUTPUT ON DECLARE TYPE ename_table_type IS TABLE OF employees.last_name%TYPE INDEX BY BINARY_INTEGER; TYPE hiredate_table_type IS TABLE OF DATE INDEX BY BINARY_INTEGER; ename_table ename_table_type; hiredate_table hiredate_table_type; BEGIN ename_table(1) := 'CAMERON'; hiredate_table(8) := SYSDATE + 7; IF ename_table.EXISTS(1) THEN  DBMS_OUTPUT.PUT_LINE ( ename_table(1) || ‘ – ‘||hiredate_table(2)); END IF; EXCEPTION WHEN NO_DATA_FOUND THEN   DBMS_OUTPUT.PUT_LINE (‘Xu ly loi : ‘|| ename_table(1) || ‘ – ‘ ||hiredate_table(8)); END; /
Using INDEX BY Table Methods The following methods make INDEX BY tables easier to use: –  EXISTS –  COUNT –  FIRST and LAST –  PRIOR –  NEXT –  TRIM –  DELETE
INDEX BY Table of Records DECLARE TYPE dept_table_type IS TABLE OF departments%ROWTYPE INDEX BY BINARY_INTEGER; dept_table dept_table_type; -- Each element of dept_table is a record Define a TABLE variable with a permitted PL/SQL data type. Declare a PL/SQL variable to hold department information. Example:
Example of INDEX BY Table of Records SET SERVEROUTPUT ON DECLARE TYPE emp_table_type is table of employees%ROWTYPE INDEX BY BINARY_INTEGER; my_emp_table emp_table_type; v_count NUMBER(3):= 10; BEGIN FOR i IN 1..v_count LOOP SELECT * INTO my_emp_table(i) FROM employees WHERE employee_id = 100 + i; END LOOP; FOR i IN my_emp_table.FIRST..my_emp_table.LAST LOOP DBMS_OUTPUT.PUT_LINE(‘Row : ‘||i ||’ – ‘||my_emp_table(i).last_name || ‘  co muc luong ‘ || my_emp_table(i).salary); END LOOP; END; /
Example of INDEX BY Table of Records SET SERVEROUTPUT ON DECLARE TYPE emp_table_type is table of employees%ROWTYPE INDEX BY BINARY_INTEGER; my_table emp_table_type; v_count NUMBER(3):= 130; BEGIN FOR i IN 100..v_count LOOP SELECT * INTO my_table(i) FROM employees WHERE employee_id =  i; END LOOP; IF my_table.exists(99) THEN DBMS_OUTPUT.PUT_LINE(‘Khong ton tai trong bang’); ELSE DBMS_OUTPUT.PUT_LINE(my_table(106).last_name); END IF; END; /
SET SERVEROUTPUT ON DECLARE TYPE emp_table_type is table of employees%ROWTYPE INDEX BY BINARY_INTEGER; my_table emp_table_type; v_count NUMBER(3):= 130; truoc  NUMBER(3):= 1;  sau  NUMBER(3):= 1; BEGIN FOR i IN 100..v_count  LOOP SELECT * INTO my_table(i) FROM employees  WHERE employee_id =  i; END LOOP; DBMS_OUTPUT.PUT_LINE('TEST 1: '||my_table.count); DBMS_OUTPUT.PUT_LINE('Row first: '||my_table.first||' Row last: '||my_table.last); sau := my_table.NEXT(125); truoc := my_table.PRIOR(113); DBMS_OUTPUT.PUT_LINE('Row prior: '||truoc|| ‘ - Row next : ‘||sau); END ;
SET SERVEROUTPUT ON DECLARE TYPE emp_table_type is table of employees%ROWTYPE INDEX BY BINARY_INTEGER; my_table emp_table_type; v_count NUMBER(3):= 130; truoc  NUMBER(3):= 1;  sau  NUMBER(3):= 1; BEGIN FOR i IN 100..v_count  LOOP SELECT * INTO my_table(i) FROM employees  WHERE employee_id =  i; END LOOP; DBMS_OUTPUT.PUT_LINE('TEST 1: '||my_table.count); DBMS_OUTPUT.PUT_LINE('Row first: '||my_table.first||' Row last: '||my_table.last); --  my_table.DELETE; -- Xoa tat ca vung nho cap phat cho my_table --  my_table.DELETE(102); -- Xoa dong 102 my_table.DELETE(100,120); -- Xoa dong 100 --> 120 DBMS_OUTPUT.PUT_LINE ('TEST 2: '||my_table.count); DBMS_OUTPUT.PUT_LINE  ('Row first: '||my_table.first||' Row last: '||my_table.last); END ;
SET SERVEROUTPUT ON DECLARE TYPE CourseList IS TABLE OF VARCHAR2(20);  courses CourseList; BEGIN courses := CourseList ( 'Oracle9i SQL', 'Oracle FUNI', 'Oracle FUN II', 'Tuning', 'Oracle PL/SQL', 'Oracle Form', 'Oracle Report','Oracle 10g'); dbms_output.put_line ('1. '||courses.count||' - '||courses(courses.last)); courses.TRIM(2); dbms_output.put_line ('2. '||courses.count||' - '||courses(courses.last)); courses.DELETE(courses.LAST);  dbms_output.put_line ('3. '||courses.count||' - '||courses(courses.last)); courses.TRIM(4); dbms_output.put_line('4. '||courses.count||' - '||courses(courses.last));  END;
Summary In this lesson, you should have learned to: Define and reference PL/SQL variables of composite data types: –  PL/SQL records –  INDEX BY tables –  INDEX BY table of records Define a PL/SQL record by using the %ROWTYPE attribute
Practice 5 Overview This practice covers the following topics: Declaring INDEX BY tables Processing data by using INDEX BY tables Declaring a PL/SQL record Processing data by using a PL/SQL record
 
Ad

More Related Content

What's hot (20)

Oracle - Program with PL/SQL - Lession 18
Oracle - Program with PL/SQL - Lession 18Oracle - Program with PL/SQL - Lession 18
Oracle - Program with PL/SQL - Lession 18
Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09
Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 06
Oracle - Program with PL/SQL - Lession 06Oracle - Program with PL/SQL - Lession 06
Oracle - Program with PL/SQL - Lession 06
Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 11
Oracle - Program with PL/SQL - Lession 11Oracle - Program with PL/SQL - Lession 11
Oracle - Program with PL/SQL - Lession 11
Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 08
Oracle - Program with PL/SQL - Lession 08Oracle - Program with PL/SQL - Lession 08
Oracle - Program with PL/SQL - Lession 08
Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 02
Oracle - Program with PL/SQL - Lession 02Oracle - Program with PL/SQL - Lession 02
Oracle - Program with PL/SQL - Lession 02
Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 12
Oracle - Program with PL/SQL - Lession 12Oracle - Program with PL/SQL - Lession 12
Oracle - Program with PL/SQL - Lession 12
Thuan Nguyen
 
PL/SQL Code for Sample Projects
PL/SQL Code for Sample ProjectsPL/SQL Code for Sample Projects
PL/SQL Code for Sample Projects
jwjablonski
 
Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16
Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 15
Oracle - Program with PL/SQL - Lession 15Oracle - Program with PL/SQL - Lession 15
Oracle - Program with PL/SQL - Lession 15
Thuan Nguyen
 
Packages - PL/SQL
Packages - PL/SQLPackages - PL/SQL
Packages - PL/SQL
Esmita Gupta
 
Oracle PL sql 3
Oracle PL sql 3Oracle PL sql 3
Oracle PL sql 3
Sergio Ronchi
 
PL/SQL
PL/SQLPL/SQL
PL/SQL
Vaibhav0
 
Programming in Oracle with PL/SQL
Programming in Oracle with PL/SQLProgramming in Oracle with PL/SQL
Programming in Oracle with PL/SQL
lubna19
 
Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sql
Ñirmal Tatiwal
 
A green solution to solve a race condition problem
A green solution to solve a race condition problemA green solution to solve a race condition problem
A green solution to solve a race condition problem
Kai Zhou
 
Oracle PL/SQL Bulk binds
Oracle PL/SQL Bulk bindsOracle PL/SQL Bulk binds
Oracle PL/SQL Bulk binds
Scott Wesley
 
SQL Tuning Overview
SQL Tuning OverviewSQL Tuning Overview
SQL Tuning Overview
Kai Liu
 
Procedure n functions
Procedure n functionsProcedure n functions
Procedure n functions
Khadija Parween
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
Reka
 
Oracle - Program with PL/SQL - Lession 18
Oracle - Program with PL/SQL - Lession 18Oracle - Program with PL/SQL - Lession 18
Oracle - Program with PL/SQL - Lession 18
Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09
Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 06
Oracle - Program with PL/SQL - Lession 06Oracle - Program with PL/SQL - Lession 06
Oracle - Program with PL/SQL - Lession 06
Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 11
Oracle - Program with PL/SQL - Lession 11Oracle - Program with PL/SQL - Lession 11
Oracle - Program with PL/SQL - Lession 11
Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 08
Oracle - Program with PL/SQL - Lession 08Oracle - Program with PL/SQL - Lession 08
Oracle - Program with PL/SQL - Lession 08
Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 02
Oracle - Program with PL/SQL - Lession 02Oracle - Program with PL/SQL - Lession 02
Oracle - Program with PL/SQL - Lession 02
Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 12
Oracle - Program with PL/SQL - Lession 12Oracle - Program with PL/SQL - Lession 12
Oracle - Program with PL/SQL - Lession 12
Thuan Nguyen
 
PL/SQL Code for Sample Projects
PL/SQL Code for Sample ProjectsPL/SQL Code for Sample Projects
PL/SQL Code for Sample Projects
jwjablonski
 
Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16
Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 15
Oracle - Program with PL/SQL - Lession 15Oracle - Program with PL/SQL - Lession 15
Oracle - Program with PL/SQL - Lession 15
Thuan Nguyen
 
Programming in Oracle with PL/SQL
Programming in Oracle with PL/SQLProgramming in Oracle with PL/SQL
Programming in Oracle with PL/SQL
lubna19
 
Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sql
Ñirmal Tatiwal
 
A green solution to solve a race condition problem
A green solution to solve a race condition problemA green solution to solve a race condition problem
A green solution to solve a race condition problem
Kai Zhou
 
Oracle PL/SQL Bulk binds
Oracle PL/SQL Bulk bindsOracle PL/SQL Bulk binds
Oracle PL/SQL Bulk binds
Scott Wesley
 
SQL Tuning Overview
SQL Tuning OverviewSQL Tuning Overview
SQL Tuning Overview
Kai Liu
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
Reka
 

Similar to Oracle - Program with PL/SQL - Lession 05 (20)

SQL on Linux and its uses and application.pdf
SQL on Linux and its uses and application.pdfSQL on Linux and its uses and application.pdf
SQL on Linux and its uses and application.pdf
bhaveshsethi456
 
Exploring collections with example
Exploring collections with exampleExploring collections with example
Exploring collections with example
pranav kumar verma
 
Open Gurukul Language PL/SQL
Open Gurukul Language PL/SQLOpen Gurukul Language PL/SQL
Open Gurukul Language PL/SQL
Open Gurukul
 
SQL.pptx SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTE...
SQL.pptx SAMPLE QUESTION PAPER (THEORY)  CLASS: XII SESSION: 2024-25  COMPUTE...SQL.pptx SAMPLE QUESTION PAPER (THEORY)  CLASS: XII SESSION: 2024-25  COMPUTE...
SQL.pptx SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTE...
MafnithaKK
 
SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTER SCIENCE...
SAMPLE QUESTION PAPER (THEORY)  CLASS: XII SESSION: 2024-25  COMPUTER SCIENCE...SAMPLE QUESTION PAPER (THEORY)  CLASS: XII SESSION: 2024-25  COMPUTER SCIENCE...
SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTER SCIENCE...
MafnithaKK
 
SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTER SCIENCE...
SAMPLE QUESTION PAPER (THEORY)  CLASS: XII SESSION: 2024-25  COMPUTER SCIENCE...SAMPLE QUESTION PAPER (THEORY)  CLASS: XII SESSION: 2024-25  COMPUTER SCIENCE...
SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTER SCIENCE...
MafnithaKK
 
SQL.pptx SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTE...
SQL.pptx SAMPLE QUESTION PAPER (THEORY)  CLASS: XII SESSION: 2024-25  COMPUTE...SQL.pptx SAMPLE QUESTION PAPER (THEORY)  CLASS: XII SESSION: 2024-25  COMPUTE...
SQL.pptx SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTE...
MafnithaKK
 
MYSQL-database basic queries for good understanding
MYSQL-database basic queries for good understandingMYSQL-database basic queries for good understanding
MYSQL-database basic queries for good understanding
PriyadharshiniG41
 
SQL PPT.pptx
SQL PPT.pptxSQL PPT.pptx
SQL PPT.pptx
PriyaPandey767008
 
Chinabankppt
ChinabankpptChinabankppt
Chinabankppt
newrforce
 
SQL
SQLSQL
SQL
zekeLabs Technologies
 
PL_SQL - II.pptx
PL_SQL - II.pptxPL_SQL - II.pptx
PL_SQL - II.pptx
priyaprakash11
 
MYSQL-database different functions pptx.pdf
MYSQL-database different functions pptx.pdfMYSQL-database different functions pptx.pdf
MYSQL-database different functions pptx.pdf
viluThakkar
 
Oracle tips and tricks
Oracle tips and tricksOracle tips and tricks
Oracle tips and tricks
Yanli Liu
 
My SQL.pptx
My SQL.pptxMy SQL.pptx
My SQL.pptx
KieveBarreto1
 
SQL
SQLSQL
SQL
Vineeta Garg
 
Basic Sql Handouts
Basic Sql HandoutsBasic Sql Handouts
Basic Sql Handouts
jhe04
 
53 SQL Questions-Answers=53 SQL Questions-Answers
53 SQL Questions-Answers=53 SQL Questions-Answers53 SQL Questions-Answers=53 SQL Questions-Answers
53 SQL Questions-Answers=53 SQL Questions-Answers
marukochan23
 
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sql
N.Jagadish Kumar
 
Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standards
Alessandro Baratella
 
SQL on Linux and its uses and application.pdf
SQL on Linux and its uses and application.pdfSQL on Linux and its uses and application.pdf
SQL on Linux and its uses and application.pdf
bhaveshsethi456
 
Exploring collections with example
Exploring collections with exampleExploring collections with example
Exploring collections with example
pranav kumar verma
 
Open Gurukul Language PL/SQL
Open Gurukul Language PL/SQLOpen Gurukul Language PL/SQL
Open Gurukul Language PL/SQL
Open Gurukul
 
SQL.pptx SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTE...
SQL.pptx SAMPLE QUESTION PAPER (THEORY)  CLASS: XII SESSION: 2024-25  COMPUTE...SQL.pptx SAMPLE QUESTION PAPER (THEORY)  CLASS: XII SESSION: 2024-25  COMPUTE...
SQL.pptx SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTE...
MafnithaKK
 
SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTER SCIENCE...
SAMPLE QUESTION PAPER (THEORY)  CLASS: XII SESSION: 2024-25  COMPUTER SCIENCE...SAMPLE QUESTION PAPER (THEORY)  CLASS: XII SESSION: 2024-25  COMPUTER SCIENCE...
SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTER SCIENCE...
MafnithaKK
 
SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTER SCIENCE...
SAMPLE QUESTION PAPER (THEORY)  CLASS: XII SESSION: 2024-25  COMPUTER SCIENCE...SAMPLE QUESTION PAPER (THEORY)  CLASS: XII SESSION: 2024-25  COMPUTER SCIENCE...
SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTER SCIENCE...
MafnithaKK
 
SQL.pptx SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTE...
SQL.pptx SAMPLE QUESTION PAPER (THEORY)  CLASS: XII SESSION: 2024-25  COMPUTE...SQL.pptx SAMPLE QUESTION PAPER (THEORY)  CLASS: XII SESSION: 2024-25  COMPUTE...
SQL.pptx SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTE...
MafnithaKK
 
MYSQL-database basic queries for good understanding
MYSQL-database basic queries for good understandingMYSQL-database basic queries for good understanding
MYSQL-database basic queries for good understanding
PriyadharshiniG41
 
Chinabankppt
ChinabankpptChinabankppt
Chinabankppt
newrforce
 
MYSQL-database different functions pptx.pdf
MYSQL-database different functions pptx.pdfMYSQL-database different functions pptx.pdf
MYSQL-database different functions pptx.pdf
viluThakkar
 
Oracle tips and tricks
Oracle tips and tricksOracle tips and tricks
Oracle tips and tricks
Yanli Liu
 
Basic Sql Handouts
Basic Sql HandoutsBasic Sql Handouts
Basic Sql Handouts
jhe04
 
53 SQL Questions-Answers=53 SQL Questions-Answers
53 SQL Questions-Answers=53 SQL Questions-Answers53 SQL Questions-Answers=53 SQL Questions-Answers
53 SQL Questions-Answers=53 SQL Questions-Answers
marukochan23
 
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sql
N.Jagadish Kumar
 
Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standards
Alessandro Baratella
 
Ad

Recently uploaded (20)

AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
Asthma presentación en inglés abril 2025 pdf
Asthma presentación en inglés abril 2025 pdfAsthma presentación en inglés abril 2025 pdf
Asthma presentación en inglés abril 2025 pdf
VanessaRaudez
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
Automation Hour 1/28/2022: Capture User Feedback from Anywhere
Automation Hour 1/28/2022: Capture User Feedback from AnywhereAutomation Hour 1/28/2022: Capture User Feedback from Anywhere
Automation Hour 1/28/2022: Capture User Feedback from Anywhere
Lynda Kane
 
"PHP and MySQL CRUD Operations for Student Management System"
"PHP and MySQL CRUD Operations for Student Management System""PHP and MySQL CRUD Operations for Student Management System"
"PHP and MySQL CRUD Operations for Student Management System"
Jainul Musani
 
Learn the Basics of Agile Development: Your Step-by-Step Guide
Learn the Basics of Agile Development: Your Step-by-Step GuideLearn the Basics of Agile Development: Your Step-by-Step Guide
Learn the Basics of Agile Development: Your Step-by-Step Guide
Marcel David
 
Buckeye Dreamin 2024: Assessing and Resolving Technical Debt
Buckeye Dreamin 2024: Assessing and Resolving Technical DebtBuckeye Dreamin 2024: Assessing and Resolving Technical Debt
Buckeye Dreamin 2024: Assessing and Resolving Technical Debt
Lynda Kane
 
Hands On: Create a Lightning Aura Component with force:RecordData
Hands On: Create a Lightning Aura Component with force:RecordDataHands On: Create a Lightning Aura Component with force:RecordData
Hands On: Create a Lightning Aura Component with force:RecordData
Lynda Kane
 
Automation Dreamin': Capture User Feedback From Anywhere
Automation Dreamin': Capture User Feedback From AnywhereAutomation Dreamin': Capture User Feedback From Anywhere
Automation Dreamin': Capture User Feedback From Anywhere
Lynda Kane
 
Automation Dreamin' 2022: Sharing Some Gratitude with Your Users
Automation Dreamin' 2022: Sharing Some Gratitude with Your UsersAutomation Dreamin' 2022: Sharing Some Gratitude with Your Users
Automation Dreamin' 2022: Sharing Some Gratitude with Your Users
Lynda Kane
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
Salesforce AI Associate 2 of 2 Certification.docx
Salesforce AI Associate 2 of 2 Certification.docxSalesforce AI Associate 2 of 2 Certification.docx
Salesforce AI Associate 2 of 2 Certification.docx
José Enrique López Rivera
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
Asthma presentación en inglés abril 2025 pdf
Asthma presentación en inglés abril 2025 pdfAsthma presentación en inglés abril 2025 pdf
Asthma presentación en inglés abril 2025 pdf
VanessaRaudez
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
Automation Hour 1/28/2022: Capture User Feedback from Anywhere
Automation Hour 1/28/2022: Capture User Feedback from AnywhereAutomation Hour 1/28/2022: Capture User Feedback from Anywhere
Automation Hour 1/28/2022: Capture User Feedback from Anywhere
Lynda Kane
 
"PHP and MySQL CRUD Operations for Student Management System"
"PHP and MySQL CRUD Operations for Student Management System""PHP and MySQL CRUD Operations for Student Management System"
"PHP and MySQL CRUD Operations for Student Management System"
Jainul Musani
 
Learn the Basics of Agile Development: Your Step-by-Step Guide
Learn the Basics of Agile Development: Your Step-by-Step GuideLearn the Basics of Agile Development: Your Step-by-Step Guide
Learn the Basics of Agile Development: Your Step-by-Step Guide
Marcel David
 
Buckeye Dreamin 2024: Assessing and Resolving Technical Debt
Buckeye Dreamin 2024: Assessing and Resolving Technical DebtBuckeye Dreamin 2024: Assessing and Resolving Technical Debt
Buckeye Dreamin 2024: Assessing and Resolving Technical Debt
Lynda Kane
 
Hands On: Create a Lightning Aura Component with force:RecordData
Hands On: Create a Lightning Aura Component with force:RecordDataHands On: Create a Lightning Aura Component with force:RecordData
Hands On: Create a Lightning Aura Component with force:RecordData
Lynda Kane
 
Automation Dreamin': Capture User Feedback From Anywhere
Automation Dreamin': Capture User Feedback From AnywhereAutomation Dreamin': Capture User Feedback From Anywhere
Automation Dreamin': Capture User Feedback From Anywhere
Lynda Kane
 
Automation Dreamin' 2022: Sharing Some Gratitude with Your Users
Automation Dreamin' 2022: Sharing Some Gratitude with Your UsersAutomation Dreamin' 2022: Sharing Some Gratitude with Your Users
Automation Dreamin' 2022: Sharing Some Gratitude with Your Users
Lynda Kane
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
AI EngineHost Review: Revolutionary USA Datacenter-Based Hosting with NVIDIA ...
SOFTTECHHUB
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
Salesforce AI Associate 2 of 2 Certification.docx
Salesforce AI Associate 2 of 2 Certification.docxSalesforce AI Associate 2 of 2 Certification.docx
Salesforce AI Associate 2 of 2 Certification.docx
José Enrique López Rivera
 
Ad

Oracle - Program with PL/SQL - Lession 05

  • 2. Objectives After completing this lesson, you should be able to do the following: Create user-defined PL/SQL records Create a record with the %ROWTYPE attribute Create an INDEX BY table Create an INDEX BY table of records Describe the difference between records, tables, and tables of records.
  • 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: field_name { field_type | variable%TYPE | table.column%TYPE | table%ROWTYPE } [[NOT NULL] { := | DEFAULT } expr ] Where field_declaration is: TYPE type_name IS RECORD ( field_declaration[, field_declaration]…); identifier type_name;
  • 6. Creating a PL/SQL Record SET SERVEROUTPUT ON DECLARE TYPE EmpRec IS RECORD ( emp_name VARCHAR2(50), job_title VARCHAR2(9), salary NUMBER(7,2)); emp_info EmpRec; BEGIN SELECT first_name||’ ‘||last_name, job_id, salary INTO emp_info.emp_name, emp_info.job_title, emp_info.salary FROM employees WHERE employee_id = 105; DBMS_OUTPUT.PUT_LINE ('Nhan vien : '||emp_info.emp_name||' lam cong viec '|| emp_info.job_title ||' co muc luong '||emp_info.salary ); END; / Declare variables to store the name, job, and salary of a new employee.
  • 7. Creating a PL/SQL Record SET SERVEROUTPUT ON DECLARE TYPE EmpRec IS RECORD ( emp_id employees.employee_id%TYPE, job_title VARCHAR2(9), salary NUMBER(7,2)); emp_info EmpRec; emp_null EmpRec; emp EmpRec; BEGIN emp_info.emp_id := 7788; emp_info.job_title := 'ANALYST'; emp_info.salary := 3500; DBMS_OUTPUT.PUT_LINE (‘Ma : ‘||emp_info.emp_id||’-’||emp_info.job_title|| ' - '||emp_info.salary ); emp := emp_info; DBMS_OUTPUT.PUT_LINE (‘Ma : ‘||emp.emp_id||’ – ‘||emp.job_title ||' - '||emp.salary ); emp := emp_null; -- Tat ca cac field trong emp deu co gia tri null DBMS_OUTPUT.PUT_LINE (‘Ma : ‘||emp.emp_id||’ - '||emp.job_title ||' - '||emp.salary ); END; /
  • 9. 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.
  • 10. 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.
  • 11. The %ROWTYPE Attribute Examples: Declare a variable to store the information about a department from the DEPARTMENTS table. dept_record departments%ROWTYPE; Declare a variable to store the information about an employee from the EMPLOYEES table. emp_record employees%ROWTYPE;
  • 12. Creating a PL/SQL Record CREATE TABLE e_temp AS SELECT * FROM employees WHERE employee_id = 50; SET SERVEROUTPUT ON DECLARE emp_rec employees%ROWTYPE; BEGIN SELECT * INTO emp_rec FROM employees WHERE employee_id = 105; INSERT INTO e_temp VALUES (emp_rec.employee_id, emp_rec.first_name, emp_rec.last_name, emp_rec.email, emp_rec.phone_number, emp_rec.hire_date, emp_rec.job_id, emp_rec.salary, emp_rec.commission_pct, emp_rec.manager_id, emp_rec.department_id); COMMIT; END; / SELECT * FROM e_temp;
  • 13. 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
  • 14. Creating an INDEX BY Table TYPE type_name IS TABLE OF { column_type | variable%TYPE | table.column%TYPE } [NOT NULL] | table.%ROWTYPE [INDEX BY BINARY_INTEGER]; identifier type_name; Syntax: ... TYPE ename_table_type IS TABLE OF employees.last_name%TYPE INDEX BY BINARY_INTEGER; ename_table ename_table_type; ... Declare an INDEX BY table to store names. Example:
  • 15. INDEX BY Table Structure
  • 16. Creating an INDEX BY Table SET SERVEROUTPUT ON DECLARE TYPE ename_table_type IS TABLE OF employees.last_name%TYPE INDEX BY BINARY_INTEGER; TYPE hiredate_table_type IS TABLE OF DATE INDEX BY BINARY_INTEGER; ename_table ename_table_type; hiredate_table hiredate_table_type; BEGIN ename_table(1) := 'CAMERON'; hiredate_table(8) := SYSDATE + 7; IF ename_table.EXISTS(1) THEN DBMS_OUTPUT.PUT_LINE ( ename_table(1) || ‘ – ‘||hiredate_table(2)); END IF; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE (‘Xu ly loi : ‘|| ename_table(1) || ‘ – ‘ ||hiredate_table(8)); END; /
  • 17. Using INDEX BY Table Methods The following methods make INDEX BY tables easier to use: – EXISTS – COUNT – FIRST and LAST – PRIOR – NEXT – TRIM – DELETE
  • 18. INDEX BY Table of Records DECLARE TYPE dept_table_type IS TABLE OF departments%ROWTYPE INDEX BY BINARY_INTEGER; dept_table dept_table_type; -- Each element of dept_table is a record Define a TABLE variable with a permitted PL/SQL data type. Declare a PL/SQL variable to hold department information. Example:
  • 19. Example of INDEX BY Table of Records SET SERVEROUTPUT ON DECLARE TYPE emp_table_type is table of employees%ROWTYPE INDEX BY BINARY_INTEGER; my_emp_table emp_table_type; v_count NUMBER(3):= 10; BEGIN FOR i IN 1..v_count LOOP SELECT * INTO my_emp_table(i) FROM employees WHERE employee_id = 100 + i; END LOOP; FOR i IN my_emp_table.FIRST..my_emp_table.LAST LOOP DBMS_OUTPUT.PUT_LINE(‘Row : ‘||i ||’ – ‘||my_emp_table(i).last_name || ‘ co muc luong ‘ || my_emp_table(i).salary); END LOOP; END; /
  • 20. Example of INDEX BY Table of Records SET SERVEROUTPUT ON DECLARE TYPE emp_table_type is table of employees%ROWTYPE INDEX BY BINARY_INTEGER; my_table emp_table_type; v_count NUMBER(3):= 130; BEGIN FOR i IN 100..v_count LOOP SELECT * INTO my_table(i) FROM employees WHERE employee_id = i; END LOOP; IF my_table.exists(99) THEN DBMS_OUTPUT.PUT_LINE(‘Khong ton tai trong bang’); ELSE DBMS_OUTPUT.PUT_LINE(my_table(106).last_name); END IF; END; /
  • 21. SET SERVEROUTPUT ON DECLARE TYPE emp_table_type is table of employees%ROWTYPE INDEX BY BINARY_INTEGER; my_table emp_table_type; v_count NUMBER(3):= 130; truoc NUMBER(3):= 1; sau NUMBER(3):= 1; BEGIN FOR i IN 100..v_count LOOP SELECT * INTO my_table(i) FROM employees WHERE employee_id = i; END LOOP; DBMS_OUTPUT.PUT_LINE('TEST 1: '||my_table.count); DBMS_OUTPUT.PUT_LINE('Row first: '||my_table.first||' Row last: '||my_table.last); sau := my_table.NEXT(125); truoc := my_table.PRIOR(113); DBMS_OUTPUT.PUT_LINE('Row prior: '||truoc|| ‘ - Row next : ‘||sau); END ;
  • 22. SET SERVEROUTPUT ON DECLARE TYPE emp_table_type is table of employees%ROWTYPE INDEX BY BINARY_INTEGER; my_table emp_table_type; v_count NUMBER(3):= 130; truoc NUMBER(3):= 1; sau NUMBER(3):= 1; BEGIN FOR i IN 100..v_count LOOP SELECT * INTO my_table(i) FROM employees WHERE employee_id = i; END LOOP; DBMS_OUTPUT.PUT_LINE('TEST 1: '||my_table.count); DBMS_OUTPUT.PUT_LINE('Row first: '||my_table.first||' Row last: '||my_table.last); -- my_table.DELETE; -- Xoa tat ca vung nho cap phat cho my_table -- my_table.DELETE(102); -- Xoa dong 102 my_table.DELETE(100,120); -- Xoa dong 100 --> 120 DBMS_OUTPUT.PUT_LINE ('TEST 2: '||my_table.count); DBMS_OUTPUT.PUT_LINE ('Row first: '||my_table.first||' Row last: '||my_table.last); END ;
  • 23. SET SERVEROUTPUT ON DECLARE TYPE CourseList IS TABLE OF VARCHAR2(20); courses CourseList; BEGIN courses := CourseList ( 'Oracle9i SQL', 'Oracle FUNI', 'Oracle FUN II', 'Tuning', 'Oracle PL/SQL', 'Oracle Form', 'Oracle Report','Oracle 10g'); dbms_output.put_line ('1. '||courses.count||' - '||courses(courses.last)); courses.TRIM(2); dbms_output.put_line ('2. '||courses.count||' - '||courses(courses.last)); courses.DELETE(courses.LAST); dbms_output.put_line ('3. '||courses.count||' - '||courses(courses.last)); courses.TRIM(4); dbms_output.put_line('4. '||courses.count||' - '||courses(courses.last)); END;
  • 24. Summary In this lesson, you should have learned to: Define and reference PL/SQL variables of composite data types: – PL/SQL records – INDEX BY tables – INDEX BY table of records Define a PL/SQL record by using the %ROWTYPE attribute
  • 25. Practice 5 Overview This practice covers the following topics: Declaring INDEX BY tables Processing data by using INDEX BY tables Declaring a PL/SQL record Processing data by using a PL/SQL record
  • 26.  

Editor's Notes

  • #2: Schedule: Timing Topic 55 minutes Lecture 55 minutes Practice 110 minutes Total
  • #3: Lesson Aim In this lesson, you learn more about composite data types and their uses.
  • #4: RECORD and TABLE Data Types Like scalar variables, composite variables have a data type. Composite data types (also known as collections) are RECORD, TABLE, NESTED TABLE, and VARRAY. You use the RECORD data type to treat related but dissimilar data as a logical unit. You use the TABLE data type to reference and manipulate collections of data as a whole object. The NESTED TABLE and VARRAY data types are covered in the Advanced PL/SQL course. A record is a group of related data items stored as fields, each with its own name and data type. A table contains a column and a primary key to give you array-like access to rows. After they are defined, tables and records can be reused. For more information, refer to PL/SQL User’s Guide and Reference, “Collections and Records.”
  • #5: PL/SQL Records A record is a group of related data items stored in fields, each with its own name and data type. For example, suppose you have different kinds of data about an employee, such as name, salary, hire date, and so on. This data is dissimilar in type but logically related. A record that contains such fields as the name, salary, and hire date of an employee allows you to treat the data as a logical unit. When you declare a record type for these fields, they can be manipulated as a unit. • Each record defined can have as many fields as necessary. • Records can be assigned initial values and can be defined as NOT NULL. • Fields without initial values are initialized to NULL. • TheDEFAULT keyword can also be used when defining fields. • YoucandefineRECORD types and declare user-defined records in the declarative part of any block, subprogram, or package. • You can declare and reference nested records. One record can be the component of another record.
  • #6: Defining and Declaring a PL/SQL Record To create a record, you define a RECORD type and then declare records of that type. In the syntax: type_name is the name of the RECORD type. (This identifier is used to declare records.) field_name is the name of a field within the record. field_type is the data type of the field. (It represents any PL/SQL data type except REF CURSOR. You can use the %TYPE and %ROWTYPE attributes.) expr is the field_type or an initial value. The NOT NULL constraint prevents assigning nulls to those fields. Be sure to initialize NOT NULL fields.
  • #7: Creating a PL/SQL Record Field declarations are like variable declarations. Each field has a unique name and a specific data type. There are no predefined data types for PL/SQL records, as there are for scalar variables. Therefore, you must create the record type first and then declare an identifier using that type. In the example on the slide, a EMP_RECORD_TYPE record type is defined to hold the values for the last_name, job_id, and salary. In the next step, a record EMP_RECORD, of thetype EMP_RECORD_TYPE is declared. The following example shows that you can use the %TYPE attribute to specify a field data type: DECLARE TYPE emp_record_type IS RECORD (employee_id NUMBER(6) NOT NULL := 100, last_name employees.last_name%TYPE, job_id employees.job_id%TYPE); emp_record emp_record_type; ... Note: You can add the NOT NULL constraint to any field declaration to prevent assigning nulls to that field. Remember, fields declared as NOT NULL must be initialized.
  • #9: PL/SQL Record Structure Fields in a record are accessed by name. To reference or initialize an individual field, use dot notation and the following syntax: record_name.field_name For example, you reference the job_id field in the emp_record record as follows: emp_record.job_id ... You can then assign a value to the record field as follows: emp_record.job_id := 'ST_CLERK'; In a block or subprogram, user-defined records are instantiated when you enter the block or subprogram and cease to exist when you exit the block or subprogram.
  • #10: Declaring Records with the %ROWTYPE Attribute To declare a record based on a collection of columns in a database table or view, you use the %ROWTYPE attribute. The fields in the record take their names and data types from the columns of the table or view. The record can also store an entire row of data fetched from a cursor or cursor variable. In the following example, a record is declared using %ROWTYPE as a data type specifier. DECLARE emp_record employees%ROWTYPE; ... The emp_record record will have a structure consisting of the following fields, each representing a column in the EMPLOYEES table. Note: This is not code, but simply the structure of the composite variable. (employee_id NUMBER(6), first_name VARCHAR2(20), last_name VARCHAR2(20), email VARCHAR2(20), phone_number VARCHAR2(20), hire_date DATE, salary NUMBER(8,2), commission_pct NUMBER(2,2), manager_id NUMBER(6), department_id NUMBER(4)) Syntax DECLARE identifier reference%ROWTYPE; where: identifier is the name chosen for the record as a whole. reference is the name of the table, view, cursor, or cursor variable on which the record is to be based. The table or view must exist for this reference to be valid. To reference an individual field, you use dot notation and the following syntax: record_name.field_name For example, you reference the commission_pct field in the emp_record record as follows: emp_record.commission_pct You can then assign a value to the record field as follows: emp_record.commission_pct:= .35; Assigning Values to Records You can assign a list of common values to a record by using the SELECT or FETCH statement. Make sure that the column names appear in the same order as the fields in your record. You can also assign one record to another if they have the same data type. A user-defined record and a %ROWTYPE record never have the same data type.
  • #11: Advantages of Using %ROWTYPE The advantages of using the %ROWTYPE attribute are listed on the slide. Use the %ROWTYPE attribute when you are not sure about the structure of the underlying database table. Using this attribute also ensures that the data types of the variables declared using this attribute change dynamically, in case the underlying table is altered. This attribute is particularly useful when you want to retrieve an entire row from a table. In the absence of this attribute, you would be forced to declare a variable for each of the columns retrieved by the SELECT * statement.
  • #12: The %ROWTYPE Attribute The first declaration on the slide creates a record with the same field names and field data types as a row in the DEPARTMENTS table. The fields are DEPARTMENT_ID, DEPARTMENT_NAME, MANAGER_ID, and LOCATION_ID. The second declaration creates a record with the same field names, field data types, and order as a row in the EMPLOYEES table. The fields are EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER, HIRE_DATE, JOB_ID, SALARY, COMMISSION_PCT, MANAGER_ID, DEPARTMENT_ID. In the following example, an employee is retiring. Information about a retired employee is added to a table that holds information about retired employees. The user supplies the employee’s number. The record of the employee specified by the user is retrieved from the EMPLOYEES and stored into the emp_rec variable, which is declared using the %ROWTYPE attribute. DEFINE employee_number = 124 DECLARE emp_rec employees%ROWTYPE; BEGIN SELECT * INTO emp_rec FROM employees WHERE employee_id = &employee_number; INSERT INTO retired_emps(empno, ename, job, mgr, hiredate, leavedate, sal, comm, deptno) VALUES (emp_rec.employee_id, emp_rec.last_name, emp_rec.job_id, emp_rec.manager_id, emp_rec.hire_date, SYSDATE, emp_rec.salary, emp_rec.commission_pct, emp_rec.department_id); COMMIT; END; / The record that is inserted into the RETIRED_EMPS table is shown below: SELECT * FROM RETIRED_EMPS;
  • #14: INDEX BY Tables Objects of the TABLE type are called INDEX BY tables. They are modeled as (but not the same as) database tables. INDEX BY tables use a primary key to provide you with array-like access to rows. A INDEX BY table: • Is similar to an array • Must contain two components: – A primary key of data type BINARY_INTEGER that indexes the INDEX BY table – A column of a scalar or record data type, which stores the INDEX BY table elements • Can increase dynamically because it is unconstrained
  • #15: Creating a INDEX BY Table There are two steps involved in creating a INDEX BY table. 1. Declare a TABLE data type. 2. Declare a variable of that data type. In the syntax: The NOT NULL constraint prevents nulls from being assigned to the PL/ SQL table of that type. Do not initialize the INDEX BY table. INDEX-BY tables can have the following element types: BINARY_INTEGER, BOOLEAN, LONG, LONG RAW, NATURAL, NATURALN,PLS_INTEGER, POSITIVE, POSITIVEN, SIGNTYPE, and STRING. INDEX-BY tables are initially sparse. That enables you, for example, to store reference data in an INDEX-BY table using a numeric primary key as the index. type_name is the name of the TABLE type. (It is a type specifier used in subsequent declarations of PL/SQL tables.) column_type is any scalar (scalar and composite) data type such as VARCHAR2, DATE, NUMBER or %TYPE. (You can use the %TYPE attribute to provide the column datatype.) identifier is the name of the identifier that represents an entire PL/SQL table.
  • #16: INDEX BY Table Structure Like the size of a database table, the size of a INDEX BY table is unconstrained. That is, the number of rows in a INDEX BY table can increase dynamically, so that your INDEX BY table grows as new rows are added. INDEX BY tables can have one column and a unique identifier to that one column, neither of which can be named. The column can belong to any scalar or record data type, but the primary key must belong to type BINARY_INTEGER. You cannot initialize an INDEX BY table in its declaration. An INDEX BY table is not populated at the time of declaration. It contains no keys or no values. An explicit executable statement is required to initialize (populate) the INDEX BY table.
  • #17: Referencing an INDEX BY Table Syntax: INDEX_BY_table_name(primary_key_value) where: primary_key_value belongs to type BINARY_INTEGER. Reference the third row in an INDEX BY table ENAME_TABLE: ename_table(3) ... The magnitude range of a BINARY_INTEGER is -2147483647 ... 2147483647, so the primary key value can be negative. Indexing does not need to start with 1. Note: The table .EXISTS(i) statement returns TRUE if a row with index i is returned. Use the EXISTS statement to prevent an error that is raised in reference to a nonexisting table element.
  • #18: Using INDEX BY Table Methods A INDEX BY table method is a built-in procedure or function that operates on tables and is called using dot notation. Syntax : table_name.method_name [ ( parameters ) ] Method Description EXISTS( n ) Returns TRUE if the n th element in a PL/SQL table exists COUNT Returns the number of elements that a PL/SQL table currently contains FIRST LAST Returns the first and last (smallest and largest) index numbers in a PL/SQL table. Returns NULL if the PL/SQL table is empty. PRIOR( n ) Returns the index number that precedes index n in a PL/SQL table NEXT( n ) Returns the index number that succeeds index n in a PL/SQL table TRIM TRIM removes one element from the end of a PL/SQL table. TRIM(n) removes n elements from the end of a PL/SQL table. DELETE DELETE removes all elements from a PL/SQL table. DELETE(n) removes the n th element from a PL/SQL table. DELETE(m, n) removes all elements in the range m ... n from a PL/SQL table.
  • #19: INDEX BY Table of Records At a given point of time, a INDEX BY table can store only the details of any one of the columns of a database table. There is always a necessity to store all the columns retrieved by a query. The INDEX BY table of records offer a solution to this. Because only one table definition is needed to hold information about all of the fields of a database table, the table of records greatly increases the functionality of INDEX BY tables. Referencing a Table of Records In the example given on the slide, you can refer to fields in the DEPT_TABLE record because each element of this table is a record. Syntax: table(index).field Example: dept_table(15).location_id := 1700; LOCATION_ID represents a field in DEPT_TABLE. Note: You can use the %ROWTYPE attribute to declare a record that represents a row in a database table. The difference between the %ROWTYPE attribute and the composite data type RECORD is that RECORD allows you to specify the data types of fields in the record or to declare fields of your own.
  • #20: Example INDEX BY Table of Records The example on the slide declares a INDEX BY table of records emp_table_type to temporarily store the details of the employees whose EMPLOYEE_ID lies between 100 and 104. Using a loop, the information of the employees from the EMPLOYEES table is retrieved and stored in the INDEX BY table. Another loop is used to print the information regarding the last names from the INDEX BY table. Observe the use of the FIRST and LAST methods in the example.
  • #25: Summary A PL/SQL record is a collection of individual fields that represent a row in a table. By using records you can group the data into one structure and then manipulate this structure as one entity or logical unit. This helps reduce coding, and keeps the code easier to maintain and understand. Like PL/SQL records, the table is another composite data type. INDEX BY tables are objects of a TABLE type and look similar to database tables but with a slight difference. INDEX BY tables use a primary key to give you array-like access to rows. The size of a INDEX BY table is unconstrained. INDEX BY tables can have one column and a primary key, neither of which can be named. The column can have any data type, but the primary key must be of the BINARY_INTEGER type. A INDEX BY table of records enhances the functionality of INDEX BY tables, because only one table definition is required to hold information about all the fields. The following collection methods help generalize code, make collections easier to use, and make your applications easier to maintain: EXISTS, COUNT, LIMIT, FIRST and LAST, PRIOR and NEXT, TRIM , and DELETE The %ROWTYPE is used to declare a compound variable whose type is the same as that of a row of a database table.
  • #26: Practice 5 Overview In this practice, you define, create, and use INDEX BY tables and a PL/SQL record.