Advanced Oracle PL/SQL Developer's Guide - Second Edition
4/5
()
About this ebook
About This Book
- Learn advanced application development features of Oracle Database 12c and prepare for the 1Z0-146 examination
- Build robust and secure applications in Oracle PL/SQL using the best practices
- Packed with feature demonstrations and illustrations that will help you learn and understand the enhanced capabilities of Oracle Database 12c
Who This Book Is For
This book is for Oracle developers responsible for database management. Readers are expected to have basic knowledge of Oracle Database and the fundamentals of PL/SQL programming. Certification aspirants can use this book to prepare for 1Z0-146 examination in order to be an Oracle Certified Professional in Advanced PL/SQL.
What You Will Learn
- Learn and understand the key SQL and PL/SQL features of Oracle Database 12c
- Understand the new Multitenant architecture and Database In-Memory option of Oracle Database 12c
- Know more about the advanced concepts of the Oracle PL/SQL language such as external procedures, securing data using Virtual Private Database (VPD), SecureFiles, and PL/SQL code tracing and profiling
- Implement Virtual Private Databases to prevent unauthorized data access
- Trace, analyze, profile, and debug PL/SQL code while developing database applications
- Integrate the new application development features of Oracle Database 12c with the current concepts
- Discover techniques to analyze and maintain PL/SQL code
- Get acquainted with the best practices of writing PL/SQL code and develop secure applications
In Detail
Oracle Database is one of the most popular databases and allows users to make efficient use of their resources and to enhance service levels while reducing the IT costs incurred. Oracle Database is sometimes compared with Microsoft SQL Server, however, Oracle Database clearly supersedes SQL server in terms of high availability and addressing planned and unplanned downtime. Oracle PL/SQL provides a rich platform for application developers to code and build scalable database applications and introduces multiple new features and enhancements to improve development experience.
Advanced Oracle PL/SQL Developer's Guide, Second Edition is a handy technical reference for seasoned professionals in the database development space. This book starts with a refresher of fundamental concepts of PL/SQL, such as anonymous block, subprograms, and exceptions, and prepares you for the upcoming advanced concepts. The next chapter introduces you to the new features of Oracle Database 12c, not limited to PL/SQL. In this chapter, you will understand some of the most talked about features such as Multitenant and Database In-Memory. Moving forward, each chapter introduces advanced concepts with the help of demonstrations, and provides you with the latest update from Oracle Database 12c context. This helps you to visualize the pre- and post-applications of a feature over the database releases. By the end of this book, you will have become an expert in PL/SQL programming and will be able to implement advanced concepts of PL/SQL for efficient management of Oracle Database.
Style and approach
The book follows the structure of the Oracle Certification examination but doesn't restrict itself to the exam objectives. Advanced concepts have been explained in an easy-to-understand style, supported with feature demonstrations and case illustrations.
Read more from Gupta Saurabh K.
Oracle Advanced PL/SQL Developer Professional Guide: Master advanced PL/SQL concepts along with plenty of example questions for 1Z0-146 examination with this book and ebook Rating: 0 out of 5 stars0 ratingsAdvanced Oracle PL/SQL Developer's Guide - Second Edition Rating: 0 out of 5 stars0 ratings
Related to Advanced Oracle PL/SQL Developer's Guide - Second Edition
Related ebooks
Oracle Advanced PL/SQL Developer Professional Guide Rating: 4 out of 5 stars4/5Oracle Quick Guides: Part 1 - Oracle Basics: Database and Tools Rating: 0 out of 5 stars0 ratingsOracle Quick Guides: Part 3 - Coding in Oracle: SQL and PL/SQL Rating: 0 out of 5 stars0 ratingsIntroduction to Oracle Database Administration Rating: 5 out of 5 stars5/5Starting Database Administration: Oracle DBA Rating: 3 out of 5 stars3/5ORACLE PL/SQL Interview Questions You'll Most Likely Be Asked Rating: 5 out of 5 stars5/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5SQL Server 2014 Development Essentials Rating: 5 out of 5 stars5/5Oracle Database 12c Quickstart Rating: 5 out of 5 stars5/5The SQL Workshop: Learn to create, manipulate and secure data and manage relational databases with SQL Rating: 0 out of 5 stars0 ratingsOracle Database 12c Backup and Recovery Survival Guide Rating: 5 out of 5 stars5/5Concise Oracle Database For People Who Has No Time Rating: 0 out of 5 stars0 ratingsOracle Database 11g - Underground Advice for Database Administrators: Beyond the basics Rating: 0 out of 5 stars0 ratingsOCA Oracle Database 11g Database Administration I: A Real-World Certification Guide Rating: 4 out of 5 stars4/5SQL Tutorial For Beginners Rating: 0 out of 5 stars0 ratingsSQL Server Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsLearn MongoDB in 24 Hours Rating: 5 out of 5 stars5/5Learning PostgreSQL Rating: 1 out of 5 stars1/5Oracle GoldenGate 11g Implementer's guide Rating: 5 out of 5 stars5/5MySQL for Python Rating: 5 out of 5 stars5/5OCP Oracle Certified Professional Java SE 11 Programmer II Study Guide: Exam 1Z0-816 and Exam 1Z0-817 Rating: 5 out of 5 stars5/5What Is Sql ?: Fundamentals of Sql,T-Sql,Pl/Sql and Datawarehousing. Rating: 0 out of 5 stars0 ratingsMicrosoft SQL Server 2012 Administration: Real-World Skills for MCSA Certification and Beyond (Exams 70-461, 70-462, and 70-463) Rating: 0 out of 5 stars0 ratingsOracle Information Integration, Migration, and Consolidation Rating: 0 out of 5 stars0 ratingsMastering MariaDB Rating: 0 out of 5 stars0 ratingsMastering PL/SQL Through Illustrations: From Learning Fundamentals to Developing Efficient PL/SQL Blocks (English Edition) Rating: 0 out of 5 stars0 ratingsSQL Server: Tips and Tricks - 2 Rating: 4 out of 5 stars4/5Oracle SQL and PL/SQL Rating: 5 out of 5 stars5/5Oracle APEX Tips and Tricks Rating: 0 out of 5 stars0 ratings
Databases For You
Learn Git in a Month of Lunches Rating: 0 out of 5 stars0 ratingsAccess 2019 For Dummies Rating: 0 out of 5 stars0 ratingsGrokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5CompTIA DataSys+ Study Guide: Exam DS0-001 Rating: 0 out of 5 stars0 ratingsHadoop Essentials Rating: 5 out of 5 stars5/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Blockchain Basics: A Non-Technical Introduction in 25 Steps Rating: 4 out of 5 stars4/5Mastering Blockchain Rating: 5 out of 5 stars5/5COMPUTER SCIENCE FOR ROOKIES Rating: 0 out of 5 stars0 ratingsManaging Data Using Excel Rating: 5 out of 5 stars5/5Practical Data Analysis Rating: 4 out of 5 stars4/5Python Projects for Everyone Rating: 0 out of 5 stars0 ratingsQuery Store for SQL Server 2019: Identify and Fix Poorly Performing Queries Rating: 0 out of 5 stars0 ratingsStar Schema The Complete Reference Rating: 5 out of 5 stars5/5Data Analysis with R Rating: 5 out of 5 stars5/5Raspberry Pi Server Essentials Rating: 0 out of 5 stars0 ratingsDark Data: Why What You Don’t Know Matters Rating: 3 out of 5 stars3/5Excel 2021 Rating: 4 out of 5 stars4/5Blockchain For Dummies Rating: 5 out of 5 stars5/5Learn SQL Server Administration in a Month of Lunches Rating: 3 out of 5 stars3/5Simply SQL: The Fun and Easy Way to Learn Best-Practice SQL Rating: 4 out of 5 stars4/5PostgreSQL Administration Essentials Rating: 0 out of 5 stars0 ratingsNode.js Design Patterns - Second Edition Rating: 4 out of 5 stars4/5Go in Action Rating: 5 out of 5 stars5/5Tableau Cookbook – Recipes for Data Visualization Rating: 0 out of 5 stars0 ratings
Reviews for Advanced Oracle PL/SQL Developer's Guide - Second Edition
3 ratings0 reviews
Book preview
Advanced Oracle PL/SQL Developer's Guide - Second Edition - Gupta Saurabh K.
Table of Contents
Advanced Oracle PL/SQL Developer's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Instant updates on new Packt books
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Overview of PL/SQL Programming Concepts
Introduction to PL/SQL
PL/SQL program fundamentals
Cursors – an overview
The cursor execution cycle
Cursor attributes
Cursor FOR loop
Exception handling in PL/SQL
System-defined exceptions
User-defined exceptions
The RAISE_APPLICATION_ERROR procedure
Exception propagation
Creating stored procedures
Executing a procedure
Functions
Functions – execution methods
Restrictions on calling functions from SQL expressions
A PL/SQL package
Oracle Database 12c enhancements to PL/SQL subprograms
Managing database dependencies
Displaying the direct and indirect dependencies
Dependency metadata
Dependency issues and enhancements
Reviewing Oracle-supplied packages
Oracle SQL Developer
Oracle SQL Developer for DBA, Developers, and Application Architects
SQL Developer 4.0
Summary
Practice exercise
2. Oracle 12c SQL and PL/SQL New Features
Database consolidation and the new Multitenant architecture
The Oracle Database 12c Multitenant architecture – features
Multitenant for Consolidation
Plug/unplug
Manage Many as One
Rapid provisioning
CDB Resource Management
Common users and local users
Oracle 12c SQL and PL/SQL new features
IDENTITY columns
Default column value to a sequence in Oracle 12c
The DEFAULT ON NULL clause
Support for 32K VARCHAR2
Row limiting using FETCH FIRST
Invisible columns
Temporal databases
In-Database Archiving
Defining a PL/SQL subprogram in the SELECT query and PRAGMA UDF
Test setup
Comparative analysis
The PL/SQL program unit white listing
Granting roles to PL/SQL program units
Test setup
Miscellaneous PL/SQL enhancements
The Oracle Database 12c (12.1.0.2) In-Memory option
The challenge
The problem statement and Oracle Database 12c In-Memory
Oracle Database 12c In-Memory option features
The Oracle Database 12c In-Memory Architecture
Controlling the In-Memory column store
The INMEMORY clause
Performance optimizations
In-Memory Advisor
Oracle Database In-Memory benefits
Summary
3. Designing PL/SQL Code
Cursor structures
Cursor execution cycle
Cursor attributes
Implicit cursors
Explicit cursors
Cursor variables
Strong and weak ref cursor types
Working with cursor variables
SYS_REFCURSOR
Cursor variables as arguments
Cursor variables – restrictions
Cursor design considerations
Cursor design–guidelines
Implicit statement results in Oracle Database 12c
Subtypes
Subtype classification
Type compatibility with subtypes
Summary
Practice exercise
4. Using Collections
Introduction to collections
Collection types
Associative arrays
Nested tables
Modify and drop a nested table object type
Design considerations of a nested table
Nested table storage
Nested table in an index - organized table
Nested table locators
Nested table as the schema object
Operations on a nested table type column
Create a nested table instance
Querying a nested table column
Nested table collection type in PL/SQL
Collection initialization
Querying the nested table metadata
Nested table comparison functions
Multiset operations on nested tables
Varray
Varray as a schema object
Operations on varray type columns
Inserting varray collection type instance
Querying varray column
Updating the varray instance
Varray in PL/SQL
Comparing the collection types
Selecting the appropriate collection type
Oracle 12c enhancements to collections
PL/SQL collection methods
EXISTS
COUNT
LIMIT
FIRST and LAST
PRIOR and NEXT
EXTEND
TRIM
DELETE
Summary
Practice exercise
5. Using Advanced Interface Methods
Overview of External Procedures
External Procedures
Components of external procedure execution flow
The extproc agent
The Library object
Callout and Callback
Call Specification
How an External Procedure executes
Environment setup
TNSNAMES.ora
EXTPROC.ora
Executing external C programs from PL/SQL
Securing External Procedures with Oracle Database 12c
Executing Java programs from PL/SQL
Loading a Java class into a database
Steps to execute a Java class from an Oracle PL/SQL unit
Summary
Practice exercise
6. Virtual Private Database
Oracle Database Security overview
Fine-Grained Access Control
How FGAC works
Virtual Private Database
How does Virtual Private Database work?
Column-level Virtual Private Database
Virtual Private Database with Oracle Database 12c Multitenant
Virtual Private Database components
Application Context
Virtual Private Database policy function
Policy types
The DBMS_RLS package
Demonstration
Virtual Private Database features and best practices
Virtual Private Database metadata
Policy utilities—refresh and drop
Oracle Database 12c Security enhancements
Oracle Database 12c Data Redaction
Data Redaction exemptions and miscellaneous features
Data Redaction function types
Demonstration
The Data Redaction metadata
Summary
Practice exercise
7. Oracle SecureFiles
Introduction to Large Objects
Classification of Large Object datatypes
Internal LOB
Persistent and Temporary LOB
External LOB
LOB restrictions
LOB data types in Oracle
BLOB and CLOB
BFILE
Some more related stuff
The LOB locator
LOB instance initialization
The DBMS_LOB package
The DBMS_LOB constants
The DBMS_LOB data types
The DBMS_LOB subprograms
LOB usage notes
Oracle SecureFiles
Deduplication and compression
Encryption
File System Logging
Write Gather Cache
Free space management
BasicFiles and SecureFiles
The db_securefile parameter
Working with LOBs
LOB metadata
Enabling the advanced features of a SecureFile
Populating the LOB data
Temporary LOB operations
Managing temporary LOBs
Working with a temporary LOB
Migrating LONG to LOBs
Use the ALTER TABLE command
Using the TO_LOB function
Online Table Redefinition
Migrating BasicFiles to SecureFiles
Oracle Database 12c enhancements to SecureFiles
Summary
Practice exercise
8. Tuning the PL/SQL Code
The PL/SQL Compiler
Subprogram inlining in PL/SQL
PRAGMA INLINE
PLSQL_OPTIMIZE_LEVEL
Case 1: When PLSQL_OPTIMIZE_LEVEL = 0
Case 2: When PLSQL_OPTIMIZE_LEVEL = 1
Case 3: When PLSQL_OPTIMIZE_LEVEL = 2
Case 4: When PLSQL_OPTIMIZE_LEVEL = 3
Native and interpreted compilation techniques
Oracle Database 11g Real Native Compilation
Selecting the appropriate compilation mode
Setting the compilation mode
Querying the compilation settings
Compiling a program unit for native or interpreted compilation
Recompiling a database for a PL/SQL native or interpreted compilation
Tuning PL/SQL code
Build secure applications using bind variables
Call parameters by reference
Avoiding an implicit data type conversion
Understanding the NOT NULL constraint
Selection of an appropriate numeric data type
Bulk processing in PL/SQL
BULK COLLECT
FORALL
FORALL and exception handling
Summary
Practice exercise
9. Result Cache
Oracle Database 11g Result Cache
What is the Server Result Cache?
Configuring the Server Result Cache
Result Cache versus Buffer Cache
Result Cache versus Oracle 12c Database In-Memory
Result Cache versus In-Memory Database Cache
SQL query Result Cache
Monitoring the SQL Result Cache
Invalidation of the SQL Result Cache
Read consistency of the SQL Result Cache
Limitations
PL/SQL Function Result Cache
Does it sound similar to deterministic functions?
Differences between Result Cache and other caching techniques
Illustration
Monitoring the PL/SQL Result Cache
Invalidation of the PL/SQL Result Cache
Limitation
OCI Client results cache
The DBMS_RESULT_CACHE package
Displaying the result cache memory report
Oracle Database 12c enhancements to the PL/SQL function Result Cache
Result cache in Real Application Clusters
Summary
Practice exercise
10. Analyzing, Profiling, and Tracing PL/SQL Code
A sample PL/SQL program
Tracking PL/SQL coding information
USER_ARGUMENTS
USER_OBJECTS
USER_OBJECT_SIZE
USER_SOURCE
USER_PROCEDURES
USER_PLSQL_OBJECT_SETTINGS and USER_STORED_SETTINGS
USER_DEPENDENCIES
The DBMS_DESCRIBE package
Tracking the program execution subprogram call stack
Tracking propagating exceptions in PL/SQL code
Determining identifier types and usages
USER_IDENTIFIERS
The PL/Scope tool
The PLSCOPE_SETTINGS parameter
The DBMS_METADATA package
DBMS_METADATA data types and subprograms
Parameter requirements
The DBMS_METADATA transformation parameters and filters
Demonstration
Tracing PL/SQL programs using DBMS_TRACE
Installing the DBMS_TRACE package
DBMS_TRACE subprograms
Compiling a PL/SQL program for debugging
Viewing the PL/SQL trace information
Steps to trace PL/SQL program execution
Profiling PL/SQL code
The DBMS_HPROF package
Differences between DBMS_PROFILER and DBMS_HPROF
DBMS_HPROF subprograms
Collecting raw profile data
Interpreting the raw profiler data
Analyzing profiler data
Creating the profiler tables
Analyzing the profiler output
Querying the profiler tables
The plshprof utility
What do these reports reveal?
Summary
Practice exercise
11. Safeguarding PL/SQL Code against SQL injection
What is SQL injection?
SQL injection targets
How to exploit the PL/SQL code?
Preventing SQL injection attacks
Sanitizing inputs using DBMS_ASSERT
Choose the right subprogram for the right identifier
Unquoted identifiers
Quoted identifiers
Literals
DBMS_ASSERT – limitations
Use of bind variables to prevent injection attacks
Best practices to avoid SQL injection
Testing the code for SQL injection flaws
Test strategy
An effective code review
Static code analysis
Fuzz tools
Generating test cases
Summary
Practice exercise
12. Working with Oracle SQL Developer
An overview of SQL Developer
Key differentiators
History and background
SQL Developer for Developers
SQL Developer for Database Administrators
SQLcl – The new SQL command line
Getting started with SQL Developer
Creating a database connection
Using the SQL worksheet
Core features of SQL Developer
Object Browser
PL/SQL Editor and Debugger
DBA Panel
Database Utilities
The Data Modeler
SQL Developer reports
Version control
The SQL Translation Framework
SQL Developer 4.0 and 4.1 New Features
Summary
Index
Advanced Oracle PL/SQL Developer's Guide Second Edition
Advanced Oracle PL/SQL Developer's Guide Second Edition
Copyright © 2016 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: May 2012
Second edition: February 2016
Production reference: 1080216
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78528-480-9
www.packtpub.com
Credits
Author
Saurabh K. Gupta
Reviewers
Kamran Aghayev A
Patrick Barel
Nassyam Basha
Ramakrishna Kandula
Wissem EL Khlifi
Sean Stacey
Davor Zelic
Commissioning Editor
Priya Singh
Acquisition Editor
Tushar Gupta
Content Development Editor
Arwa Manasawala
Technical Editor
Rohan Uttam Gosavi
Copy Editor
Stephen Copestake
Project Coordinator
Shweta H Birwatkar
Proofreader
Safis Editing
Indexer
Monica Ajmera Mehta
Graphics
Abhinash Sahu
Production Coordinator
Nilesh R. Mohite
Cover Work
Nilesh R. Mohite
About the Author
Saurabh K. Gupta is a seasoned database technologist with extensive experience in designing high performance and highly available database applications. His technology focus has been centered around Oracle Database architecture, Oracle Cloud platform, Database In-Memory, Database Consolidation, Multitenant, Exadata, Big Data, and Hadoop. He has authored the first edition of this book. He is an active speaker at technical conferences from Oracle Technology Network, IOUG Collaborate'15, AIOUG Sangam, and Tech Days. Connect with him on his twitter handle (or SAURABHKG) or through his technical blog www.sbhoracle.wordpress.com, with comments, suggestions, and feedback regarding this book.
About the Reviewers
Patrick Barel is a PL/SQL developer for AMIS Services (http://www.amis.nl/) in the Netherlands. Besides working with SQL and PL/SQL, he co-developed CodeGen together with Steven Feuerstein, and has written different plugins (http://bar-solutions.com/) for PL/SQL developer at (http://www.allroundautomations.com/). He publishes articles on AMIS Technology Blog (http://technology.amis.nl/blog) and on his own blog (http://blog.bar-solutions.com).
He has been a reviewer for several books including Oracle PL/SQL Programming by Steven Feuerstein. He has been an Oracle ACE since 2011.
Nassyam Basha is a database administrator and an Oracle ACE Director. He holds a master's degree in Computer Applications from the University of Madras. He is an Oracle 11g Certified Master and Exadata implementation specialist, and has good knowledge of Oracle technologies, such as Data Guard, RMAN, RAC, and Exadata. He actively participates in Oracle-related forums, such as OTN, where he has superhero status. He maintains an Oracle-technology-related blog (www.oracle-ckpt.com) and has coauthored Oracle Data Guard 11gR2 administration beginners guide, Packt Publishing. He actively writes many articles on OTN in various languages. He is a speaker at OTN, IOUG, and SANGAM, and he is the co-founder of Oraworld-team (www.oraworld-team.com). He is part of the AIOUG community on Twitter, where he occasionally expresses his views via the Twitter handle @AIOUG. He is currently working with Pythian as an Oracle database consultant.
Nassyam Basha has written Oracle Data Guard 11gR2 Beginner's Guide, Packt Publishing.
I want to thank the almighty Allah and my parents, Abdul Aleem and Rahimunnisa, for their support and blessings all the time—without them, nothing is possible. Special thanks to my wife and 9-month-old daughter Yashfeen Fathima, who've shared a lot of fun and crazy things with me while I worked on this book, and, as always, I would also like to thank my brother, Nawaz, and my cousins, for their great support. Finally, thanks to Saurabh Gupta for referring me as a technical reviewer, which was not an easy task for me, as this is my first assignment as a reviewer. He did a great job on this book.
Wissem El Khlifi is the first Oracle ACE in Spain and an Oracle Certified Professional DBA with over 12 years of IT experience.
He earned his Computer Science engineering degree from FST Tunisia, his master's degree in Computer Science from the UPC, Barcelona, and another master's degree in Big Data Science from the UPC, Barcelona.
His areas of interest are Linux System Administration, Oracle ERP and Databases (RAC and Dataguard), big data NoSQL database management, and big data analysis.
His career has included the roles of Oracle and Java analyst/programmer, Oracle DBA, architect, team leader, and big data scientist. He currently works as senior database and application engineer for Schneider Electric/APC.
He writes numerous articles on his website, http://www.oracle-class.com, and you can contact him via Twitter at @orawiss.
Davor Zelic is an IT professional with more than 15 years of experience in designing, developing, and implementing IT systems.
After getting his master's degree in Electrical Engineering, he began his professional career working with Oracle technology in the Croatian IT company TEB Informatika. For more than 10 years, Davor worked on IT projects related to road management, where he gained extensive experience working as an Oracle SQL, PLSQL, Forms, and Reports and Spatial developer. He has proved his knowledge by becoming an Oracle Certified Professional issued by the Oracle Corporation.
Apart from Oracle technology, Davor has gained expertise in design and development of geographic information systems for collection, storage, transformation, analysis, and visualization of geo-referenced data. He originally worked with Intergraph technology, but later his focus moved to open source GIS technologies, such as Geoserver and OpenLayers.
Davor currently works as a software architect at the IT department of Croatian Central Bank, designing software solutions for Croatian financial market data collection and analysis.
I want to thank my parents for the support that they gave me in choosing my educational path, which allowed me to find a job that is not just a routine, but also a source of satisfaction and constant challenge.
www.PacktPub.com
Support files, eBooks, discount offers, and more
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com, and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Free access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.
Instant updates on new Packt books
Get notified! Find out when new books are published by following @PacktEnterprise on Twitter or the Packt Enterprise Facebook page.
Preface
How many of us would believe that PL/SQL was introduced as a scripting language for executing a bunch of SQL scripts? Well, that's true. With the growing need to build computational logic and condition-based constructs, and to manage exception rules within databases, Oracle Corporation first released PL/SQL along with Oracle Database Version 6.0 with a limited set of capabilities. Within its capacity, PL/SQL was capable of creating program units that could not be stored inside the database. Eventually, Oracle's release in the application line, SQL *Forms version V3.0, included the PL/SQL engine and allowed developers to implement the application logic through procedures. Back then, PL/SQL used to be part of the transaction processing option in Oracle 6 and the procedural option in Oracle 7. Since the time of its ingenuous beginning, PL/SQL has matured immensely as a standard feature of Oracle Database. It has been enthusiastically received by the developer community, and the credit goes to its support for advanced elements such as modular programming, encapsulation, support for objects and collections, program overloading, native and dynamic SQL, and exception handling.
PL/SQL is loosely derived from Ada (named after Ada Lovelace, an English mathematician who is regarded as the first computer programmer), a high-level programming language, which complies with the advanced programming elements. Building a database backend for an application demands the ability to design the database architecture, skills to code complex business logics, and expertise in administering and protecting the database environment. One of the principal reasons why PL/SQL is a key enabler in the development phase is its tight integration with Oracle's SQL language. In addition to this, it provides a rich platform for implementing the business logic in the Oracle Database layer and store them as procedures or functions for subsequent use. As a procedural language, PL/SQL provides a diverse range of datatypes, iterative and control constructs, conditional statements, and exception handlers.
In a standard software development space, an Oracle database developer is expected to get involved in schema design; code business logics on the server side by using functions, procedures, or packages; implement action rules by using triggers; and support client-side programs in setting up the application interface. While building the server-side code, developers should understand that their code contributes to the application's performance and scalability. Language basics are expected to be resilient, but while building robust and secure applications using PL/SQL, developers must take advantage of best practices and try to use advanced language features. This book focuses on the advanced features of PL/SQL validated up to the latest Oracle Database 12c.
Learning by example has always been a well-attested approach for diving deep into a concept. This book will enable you to master the latest enhancements and new features of Oracle Database 12c. For efficient reading, you just have to be familiar with the PL/SQL fundamentals so that you can relate to the evolution of an advanced feature from its ever-expanding roots.
This book closely follows the outline of the Oracle University certification; that is, the Oracle Certified Advanced PL/SQL Developer Professional (1Z0-146) exam. One of the most sought after certifications in the developer community, the 1Z0-146 certification's objectives are quite comprehensive, and touch upon the various progressive areas of PL/SQL. To name a few, PL/SQL code performance, maintenance, bulk processing techniques, PL/SQL collections, security implementation, and the handling of large objects. For certification aspirants, this book will serve as a one-stop exam guide. At many stages, this book goes beyond the certification objectives and attempts to build a deep understanding of the concepts. Therefore, mid-level database developers will find this book a handy language reference and would be keen to have it on their bookshelves.
My last work on the same subject will remain close to my heart, but this one is straight from my experience. I hope that this book will help you improve your PL/SQL development skills and gain confidence in using advanced features, along with meticulous familiarization of Oracle Database 12c.
What this book covers
Chapter 1, Overview of PL/SQL Programming Concepts, provides an overview of PL/SQL fundamentals. It refreshes the basic concepts, such as PL/SQL language features, the anonymous block structure, exception handling, and stored subprograms.
Chapter 2, Oracle 12c SQL and PL/SQL New Features, talks about the new features of Oracle Database 12c. It starts with the idea of consolidation of databases on a cloud and how the Oracle 12c Multitenant architecture addresses the requirements. It consolidates the new features in Oracle 12c SQL and PL/SQL, and explains each of them with examples. It will help you to feel the essence of Oracle Database 12c and understand what the driving wheel of innovation is. A section on the Oracle Database 12c In-memory option will familiarize you with the breakthrough feature in the analytics and warehouse space.
Chapter 3, Designing PL/SQL Code, primarily focuses on the PL/SQL cursor's design and handling. You will get to learn the basics of cursor design, cursor types and cursor variables, handling cursors in PL/SQL, and design guidelines. This chapter will also include the enhancements made by Oracle Database 12c with respect to cursors.
Chapter 4, Using Collections, introduces you to the world of collections; namely, associative arrays, nested tables, and varrays. Taking you all the way from their creation in SQL and PL/SQL to design considerations, this chapter makes you wise enough to choose the right collection type in a given situation. A section on Oracle Database 12c enhancements to collections introduces a very handy feature that will allow you to join a table and collection.
Chapter 5, Using Advanced Interface Methods, focuses on a powerful feature of PL/SQL: how to execute external procedures in PL/SQL. You will learn and understand the specifics of executing a C or Java program in PL/SQL as an external procedure through step-by-step demonstration. This chapter also mentions the Oracle Database 12c enhancement which allows you to secure external procedures through an additional safety net.
Chapter 6, Virtual Private Database, provides a detailed overview of the Oracle Database Security Defense-in-depth architecture and focuses on one of the developer-centric features, known as the Virtual Private Database. Oracle Database 12c security enhancements and a demonstration of data redaction will make you understand Oracle's security offerings.
Chapter 7, Oracle SecureFiles, provides a thorough understanding of handling large objects in Oracle and focuses on storage optimizations made by SecureFiles. Introduced in Oracle 11g, SecureFiles is the new storage mechanism that scores high on its advanced features, such as compression, encryption, and deduplication. This chapter also helps you with the recommended migration methods from older LOBs to SecureFiles.
Chapter 8, Tuning the PL/SQL Code, introduces the best practices for tuning PL/SQL code. It starts with the PL/SQL optimizer and rolls through the benefits of native compilation, PL/SQL code writing skills, and code evaluation design. This chapter includes the changes in Oracle 12c with respect to large object handling.
Chapter 9, Result Cache, explains the result caching feature in Oracle Database. It is a powerful caching mechanism that enhances the performance of SQL queries and PL/SQL functions that are repeatedly executed on the server. This chapter also discusses the enhancements made to the feature in Oracle Database 12c.
Chapter 10, Analyzing, Profiling, and Tracing PL/SQL Code, details the techniques used to analyze, profile, and trace PL/SQL code. If you are troubleshooting PL/SQL code for performance, you must learn the profiling and tracing techniques. In an enterprise application environment, these practices are vital weapons in a developer's arsenal.
Chapter 11, Safeguarding PL/SQL Code against SQL injection, describes ways to protect your PL/SQL from being attacked. A vulnerable piece of code is prone to malicious attacks and runs the risk of giving away sensitive information. Efficient code writing and proofing the code from external attacks can help to minimizing the attack surface area. In this chapter, you will learn the practices for safeguarding your code against external threats.
Chapter 12, Working with Oracle SQL Developer, describes the benefits of the Oracle SQL Developer for developers, database administrators, and architects. This chapter not only helps you get started with SQL Developer, but also helps you gain a better understanding of the new features of SQL Developer 4.0 and 4.1.
What you need for this book
If you are good with PL/SQL development basics, I'm sure you will enjoy reading this book. You will learn new ways to program efficiently in PL/SQL.
Who this book is for
This book is for Oracle developers who are responsible for database management. Readers are expected to have basic knowledge of the Oracle Database and the fundamentals of PL/SQL programming. Certification aspirants can use this book to prepare for the 1Z0-146 examination in order to be come an Oracle Certified Professional in Advanced PL/SQL.
Conventions
In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: The modified SELECT query is then executed in the HR schema of the database.
A block of code is set as follows:
/*Create the stored procedure to set the context attribute*/
CREATE OR REPLACE PROCEDURE p_app_context (p_val VARCHAR2)
IS
BEGIN
/*Create a namespace DEMO_CONTEXT*/
DBMS_SESSION.SET_CONTEXT(
NAMESPACE => 'DEMO_CONTEXT',
ATTRIBUTE => 'COUNTRY',
VALUE => P_VAL);
END;
/
New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: This is how interpreted compilation works. In the case of native compilation,, a sharable dynamic linked library (DLL) is generated instead of a machine code.
Note
Warnings or important notes appear in a box like this.
Tip
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <[email protected]>, and mention the book title via the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
Questions
You can contact us at <[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.
Chapter 1. Overview of PL/SQL Programming Concepts
Structured Query Language (SQL) is a language that has been widely accepted and adopted for accessing relational databases. This language allows users to perform database operations such as reading, creating, modifying, and deleting the data. Since the summer of 1970, when Dr. E.F. Codd published the paper A Relational Model of Data for Large Shared Data Banks for the ACM journal, the language has matured comprehensively as an industry standard. With its broad range of features and easy adaptation to enterprise environments, the SQL language has been typically regarded as the most reliable language for interacting with relational databases.
PL/SQL was developed in 1991 by Oracle Corporation as a procedural language extension to SQL. Its ability to integrate seamlessly with SQL makes it a powerful language to construct the data access layer and the rich procedural extensions help in translating business logic within the Oracle Database. This first chapter introduces you to the PL/SQL language and refreshes some of the key programming concepts. The chapter is outlined as follows:
Introduction to PL/SQL
Recapitulate