Answered step by step
Verified Expert Solution
Question
1 Approved Answer
CCIT 4 2 6 : Database - 2 Lab 6 : Exception Handling and Triggers LAB 6 Content PL / SQL - Records In this
CCIT : Database
Lab : Exception Handling and Triggers
LAB Content
PLSQL Records
In this chapter, we will discuss Records in PLSQL Arecordis a data structure that can hold data items of different kinds. Records consist of different fields, similar to a row of a database table.
For example, you want to keep track of your books in a library. You might want to track the following attributes about each book, such as Title, Author, Subject, Book ID A record containing a field for each of these items allows treating a BOOK as a logical unit and allows you to organize and represent its information in a better way.
PLSQL can handle the following types of records
Tablebased
Cursorbased records
Userdefined records
TableBased Records
The ROWTYPE attribute enables a programmer to createtablebasedandcursorbasedrecords
The following example illustrates the concept oftablebasedrecords We will be using the CUSTOMERS table we had created and used in the previous chapters
DECLARE
customerrec customersrowtype;
BEGIN
SELECT into customerrec
FROM customers
WHERE id ;
dbmsoutput.putlineCustomer ID: customerrec.id;
dbmsoutput.putlineCustomer Name: customerrec.name;
dbmsoutput.putlineCustomer Address: customerrec.address;
dbmsoutput.putlineCustomer Salary: customerrec.salary;
END;
When the above code is executed at the SQL prompt, it produces the following result
Customer ID:
Customer Name: Hardik
Customer Address: Bhopal
Customer Salary:
PLSQL procedure successfully completed.
CursorBased Records
The following example illustrates the concept ofcursorbasedrecords We will be using the CUSTOMERS table we had created and used in the previous chapters
DECLARE
CURSOR customercur is
SELECT id name, address
FROM customers;
customerrec customercurrowtype;
BEGIN
OPEN customercur;
LOOP
FETCH customercur into customerrec;
EXIT WHEN customercurnotfound;
DBMSOUTPUT.putlinecustomerrec.id customerrec.name;
END LOOP;
END;
When the above code is executed at the SQL prompt, it produces the following result
Ramesh
Khilan
kaushik
Chaitali
Hardik
Komal
PLSQL procedure successfully completed.
UserDefined Records
PLSQL provides a userdefined record type that allows you to define the different record structures. These records consist of different fields. Suppose you want to keep track of your books in a library. You might want to track the following attributes about each book
Title
Author
Subject
Book ID
Defining a Record
The record type is defined as
TYPE
typename IS RECORD
fieldname datatypeNOT NULL: DEFAULT EXPRESSION
fieldname datatypeNOT NULL: DEFAULT EXPRESSION
fieldnameN datatypeN NOT NULL: DEFAULT EXPRESSION;
recordname typename;
The Book record is declared in the following way
DECLARE
TYPE books IS RECORD
title varchar
author varchar
subject varchar
bookid number;
book books;
book books;
Accessing Fields
To access any field of a record, we use the dotoperator The member access operator is coded as a period between the record variable name and the field that we wish to access. Following is an example to explain the usage of record
DECLARE
type books is record
title varchar
author varchar
subject varchar
bookid number;
book books;
book books;
BEGIN
Book specification
booktitle :C Programming';
bookauthor : 'Nuha Ali ;
booksubject :C Programming Tutorial';
bookbookid :;
Book specification
booktitle : 'Telecom Billing';
bookauthor : 'Zara Ali';
booksubject : 'Telecom Billing Tutorial';
bookbookid :;
Print book record
dbmsoutput.putlineBook title : booktitle;
dbmsoutput.putlineBook author : bookauthor;
dbmsoutput.putlineBook subject : booksubject;
dbmsoutput.putlineBook bookid : bookbookid;
Print book record
dbmsoutput.putlineBook title : booktitle;
dbmsoutput.putlineBook author : bookauthor;
dbmsoutput.putlineBook subject : booksubject;
dbmsoutput.putlineBook bookid : bookbookid;
END;
When the above code is executed at the SQL prompt, it produces the following result
Book title : C Programming
Book author : Nuha Ali
Book subject : C Programming Tutorial
Book bookid :
Book title : Telecom Billing
Book author : Zara Ali
Book subject : Telecom Billing Tutor
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started