advanced features of oracle sql
TRANSCRIPT
-
8/14/2019 Advanced Features of Oracle SQL
1/48
DCopyright 2004, Oracle. All rights reserved.
Advanced Features of Oracle SQL
-
8/14/2019 Advanced Features of Oracle SQL
2/48
D-2 Copyright 2004, Oracle. All rights reserved.
Objectives
After completing this appendix, you should be able to:
Explain and create LOBs
Explain the need and benefits of PL/SQL
Identify the different types of PL/SQL blocks Use regular expression support in SQL to search,
match, and replace strings all in terms of regular
expressions
Employ proactive tuning methodologies
-
8/14/2019 Advanced Features of Oracle SQL
3/48
D-3 Copyright 2004, Oracle. All rights reserved.
Understanding Large Object Types
Types:
CLOB: Character
NCLOB: Multibyte character
BLOB: Binary BFILE: Binary file
-
8/14/2019 Advanced Features of Oracle SQL
4/48
D-4 Copyright 2004, Oracle. All rights reserved.
Differentiating Between LOB, LONG, andLONG RAWTypes
LONG and LONG RAW data canonly be accessed sequentially
LOBs can be accessed inrandom order
A table can have only one LONGorLONG RAW column
A table can contain multiple LOBcolumns
Stores up to 2 GB of dataStores up to 128 TB of data
LONG and LONG RAWLOB
-
8/14/2019 Advanced Features of Oracle SQL
5/48
-
8/14/2019 Advanced Features of Oracle SQL
6/48
D-6 Copyright 2004, Oracle. All rights reserved.
Using BFILES
Creating a table having a BFILE column:
Creating a directory object:
Inserting values into a BFILE:
CREATE TABLE department(dep_id INTEGER,dep_details BFILE);
CREATE OR REPLACE DIRECTORY file_locationAS C:\dept\files;
INSERT INTO department(dep_id,dep_details)VALUES(10,BFILENAME(file_location,details.doc));
-
8/14/2019 Advanced Features of Oracle SQL
7/48D-7 Copyright 2004, Oracle. All rights reserved.
What Is PL/SQL?
PL/SQL:
Stands for Procedural Language extension to SQL
Is Oracle Corporations standard data access
language for relational databases Seamlessly integrates procedural constructs with
SQL
-
8/14/2019 Advanced Features of Oracle SQL
8/48
-
8/14/2019 Advanced Features of Oracle SQL
9/48D-9 Copyright 2004, Oracle. All rights reserved.
PL/SQL Environment
PL/SQL engine
Oracle database server
SQL statement
executor
Procedural
statement
executor
procedural
SQL
PL/SQL
block
-
8/14/2019 Advanced Features of Oracle SQL
10/48D-10 Copyright 2004, Oracle. All rights reserved.
Benefits of PL/SQL
SQLIF...THEN
SQLELSE
SQLEND IF;
SQL
Integration of procedural constructs with SQL
Improved performance
SQL 1
SQL 2
-
8/14/2019 Advanced Features of Oracle SQL
11/48D-11 Copyright 2004, Oracle. All rights reserved.
Benefits of PL/SQL
Modularized program development
Integration with Oracle tools
Portability
Exception handling
-
8/14/2019 Advanced Features of Oracle SQL
12/48D-12 Copyright 2004, Oracle. All rights reserved.
Benefits of PL/SQL
-
8/14/2019 Advanced Features of Oracle SQL
13/48D-13 Copyright 2004, Oracle. All rights reserved.
PL/SQL Block Structure
DECLARE (Optional)
Variables, cursors, user-defined exceptions
BEGIN (Mandatory)
- SQL statements- PL/SQL statements
EXCEPTION (Optional)
Actions to perform
when errors occurEND; (Mandatory)
-
8/14/2019 Advanced Features of Oracle SQL
14/48D-14 Copyright 2004, Oracle. All rights reserved.
PL/SQL Block Structure
-
8/14/2019 Advanced Features of Oracle SQL
15/48D-15 Copyright 2004, Oracle. All rights reserved.
Block Types
Anonymous Procedure
Function[DECLARE]
BEGIN--statements
[EXCEPTION]
END;
PROCEDURE nameIS
BEGIN--statements
[EXCEPTION]
END;
FUNCTION nameRETURN datatypeISBEGIN--statementsRETURN value;
[EXCEPTION]
END;
-
8/14/2019 Advanced Features of Oracle SQL
16/48D-16 Copyright 2004, Oracle. All rights reserved.
Block Types
-
8/14/2019 Advanced Features of Oracle SQL
17/48D-17 Copyright 2004, Oracle. All rights reserved.
Program Constructs
Application triggers
Application packages
Application proceduresor functions
Anonymous blocks
Tools Constructs
Object types
Database triggers
Stored packages
Stored procedures orfunctions
Anonymous blocks
Database Server
Constructs
Object types
-
8/14/2019 Advanced Features of Oracle SQL
18/48D-18 Copyright 2004, Oracle. All rights reserved.
Program Constructs
-
8/14/2019 Advanced Features of Oracle SQL
19/48D-19 Copyright 2004, Oracle. All rights reserved.
Creating an Anonymous Block
Enter the anonymous block in the iSQL*Plus
workspace:
-
8/14/2019 Advanced Features of Oracle SQL
20/48D-20 Copyright 2004, Oracle. All rights reserved.
Executing an Anonymous Block
Click the Execute button to execute the anonymous
block:
PL\SQL procedure successfully completed.
-
8/14/2019 Advanced Features of Oracle SQL
21/48D-21 Copyright 2004, Oracle. All rights reserved.
Test the Output of a PL/SQL Block
Enable output in iSQL*Plus with the commandSET SERVEROUTPUT ON.
Use a predefined Oracle package and itsprocedure:
DBMS_OUTPUT.PUT_LINE
SET SERVEROUTPUT ONDBMS_OUTPUT.PUT_LINE(' The First Name of the
Employee is ' || f_name);
-
8/14/2019 Advanced Features of Oracle SQL
22/48D-22 Copyright 2004, Oracle. All rights reserved.
Test the Output of a PL/SQL Block
-
8/14/2019 Advanced Features of Oracle SQL
23/48D-23 Copyright 2004, Oracle. All rights reserved.
Regular Expression Overview
ABC
A method ofdescribing bothsimple and complex
patterns forsearching andmanipulating
Several new
functions to
support regular
expressions
A multilingual
regular expressionsupport for SQL
and PLSQL string
types
-
8/14/2019 Advanced Features of Oracle SQL
24/48D-24 Copyright 2004, Oracle. All rights reserved.
Meta Characters
Specifies equivalence classes[==]
Matches one or more occurrence+
Matches zero or one occurrence?
Matches any character in the supported character set, except NULL.Grouping expression, treated as a single subexpression()
Back-reference expression\n
Specifies a character class and matches any character in that class[: :]
Can have four different meanings: 1. Stand for itself. 2. Quote the nextcharacter. 3. Introduce an operator. 4. Do nothing.
\
DescriptionSymbol
Specifies one collation element, such as a multicharacter element
Matches at least m times but no more than n times
Matches exactly m times
Bracket expression for a matching list matching any one of theexpressions represented in the list
Matches the start-of-line/end-of-line
Alteration operator for specifying alternative matches
Matches zero or more occurrences
[..]
{m,n}
{m}
[ ]
^/$
|
*
-
8/14/2019 Advanced Features of Oracle SQL
25/48D-25 Copyright 2004, Oracle. All rights reserved.
Using Meta CharactersProblem: Find 'abc' within a string:Solution: 'abc' Matches: abcDoes not match: 'def'
Problem: To find 'a' followed by any character, followedby 'c'
Meta Character: any character is defined by '.'Solution: 'a.c'
Matches: abc Matches: adc Matches: alc Matches: a&cDoes not match: abb
Problem: To find one or more occurrences of 'a'
Meta Character: Use'+' sign to match one or more of theprevious charactersSolution: 'a+' Matches: a Matches: aaDoes not match: bbb
1
2
3
-
8/14/2019 Advanced Features of Oracle SQL
26/48
D-26 Copyright 2004, Oracle. All rights reserved.
Notes Only
-
8/14/2019 Advanced Features of Oracle SQL
27/48
D-27 Copyright 2004, Oracle. All rights reserved.
Regular Expression Functions
Searches for a given string for a regularexpression pattern and returns the positionwhere the match is found
REGEXP_INSTR
Searches for a regular expression patternwithin a given string and returns the matchedsubstring
REGEXP_SUBSTR
Searches for a regular expression patternand replaces it with a replacement string
REGEXP_REPLACE
Similar to the LIKE operator, but performsregular expression matching instead ofsimple pattern matching
REGEXP_LIKE
DescriptionFunction Name
-
8/14/2019 Advanced Features of Oracle SQL
28/48
D-28 Copyright 2004, Oracle. All rights reserved.
The REGEXP Function Syntax
REGEXP_LIKE (srcstr, pattern [,match_option])
REGEXP_INSTR (srcstr, pattern [, position [, occurrence
[, return_option [, match_option]]]])
REGEXP_SUBSTR (srcstr, pattern [, position[, occurrence [, match_option]]])
REGEXP_REPLACE(srcstr, pattern [,replacestr [, position[, occurrence [, match_option]]]])
-
8/14/2019 Advanced Features of Oracle SQL
29/48
-
8/14/2019 Advanced Features of Oracle SQL
30/48
D-30 Copyright 2004, Oracle. All rights reserved.
SELECT street_address,REGEXP_INSTR(street_address,'[^[:alpha:]]')
FROM locationsWHERE
REGEXP_INSTR(street_address,'[^[:alpha:]]')> 1;
Checking the Presence of a Pattern
-
8/14/2019 Advanced Features of Oracle SQL
31/48
D-31 Copyright 2004, Oracle. All rights reserved.
SELECT REGEXP_SUBSTR(street_address , ' [^ ]+ ')"Road" FROM locations;
Example of Extracting Substrings
-
8/14/2019 Advanced Features of Oracle SQL
32/48
D-32 Copyright 2004, Oracle. All rights reserved.
SELECT REGEXP_REPLACE( country_name, '(.)','\1 ') "REGEXP_REPLACE"
FROM countries;
Replacing Patterns
-
8/14/2019 Advanced Features of Oracle SQL
33/48
D-33 Copyright 2004, Oracle. All rights reserved.
Regular Expressions and
Check Constraints
ALTER TABLE emp8ADD CONSTRAINT email_addrCHECK(REGEXP_LIKE(email,'@'))NOVALIDATE ;
INSERT INTO emp8 VALUES(500,'Christian','Patel','ChrisP2creme.com', 1234567890,'12-Jan-2004', 'HR_REP', 2000, null, 102, 40) ;
1
2
-
8/14/2019 Advanced Features of Oracle SQL
34/48
D-34 Copyright 2004, Oracle. All rights reserved.
Proactive Tuning Methodology
Simplifying design
Modeling data
Optimizing table and index design
Using views Writing efficient SQL
Sharing cursors
Using bind variables
Using dynamic SQL
-
8/14/2019 Advanced Features of Oracle SQL
35/48
D-35 Copyright 2004, Oracle. All rights reserved.
Simplifying Application Design
Simple tables
Well-written SQL
Indexing only as required
Retrieving optimum information
-
8/14/2019 Advanced Features of Oracle SQL
36/48
D-36 Copyright 2004, Oracle. All rights reserved.
Modeling Data
Accurately represent business practices
Focus on most frequent and important businesstransactions
Use modeling tools Normalize the data
-
8/14/2019 Advanced Features of Oracle SQL
37/48
D-37 Copyright 2004, Oracle. All rights reserved.
Optimizing Table Design
Compromise between flexibility and performance
Principally normalize
Selectively denormalize
Use Oracle performance features Default values
Check constraints
Materialized views
Clusters
Focus on business-critical tables
-
8/14/2019 Advanced Features of Oracle SQL
38/48
D-38 Copyright 2004, Oracle. All rights reserved.
Optimizing Index Design
Index keys
Primary key
Unique key
Foreign keys
Index often-queried data
Use SQL as a guide to index design.
-
8/14/2019 Advanced Features of Oracle SQL
39/48
D-39 Copyright 2004, Oracle. All rights reserved.
-
8/14/2019 Advanced Features of Oracle SQL
40/48
D-40 Copyright 2004, Oracle. All rights reserved.
Using Views
Simplifies application design
Is transparent to the end user
Can cause suboptimal execution plans
-
8/14/2019 Advanced Features of Oracle SQL
41/48
D-41 Copyright 2004, Oracle. All rights reserved.
Increasing the Efficiency of SQL Execution
Good database connectivity
Using cursors
Minimizing parsing
Using bind variables
-
8/14/2019 Advanced Features of Oracle SQL
42/48
D-42 Copyright 2004, Oracle. All rights reserved.
Bind Variable Peeking
During the first invocation of a cursor, the queryoptimizer peeks at user-defined bind variable
values.
During subsequent invocations, no peeking takesplace, and the cursor is shared.
-
8/14/2019 Advanced Features of Oracle SQL
43/48
-
8/14/2019 Advanced Features of Oracle SQL
44/48
D-44 Copyright 2004, Oracle. All rights reserved.
Writing SQL to Share Cursors
Create generic code using the following:
Stored procedures and packages
Database triggers
Any other library routines and procedures Writing to format standards
Case
White space
Comments
Object references
Bind variables
-
8/14/2019 Advanced Features of Oracle SQL
45/48
D-45 Copyright 2004, Oracle. All rights reserved.
Dynamic SQL
Provides the ability to dynamically execute SQLstatements whose complete text is unknown until
execution time
Can place dynamic SQL statements directly into
PL/SQL blocks Consists of:
DML statements Queries PL/SQL anonymous blocks
DDL statements Transaction control statements Session control statements
Should be used cautiously
-
8/14/2019 Advanced Features of Oracle SQL
46/48
D-46 Copyright 2004, Oracle. All rights reserved.
Implementing the Application
Use an appropriate development environment.
Isolate software components.
Check that the application is complete and self
sufficient. Use programming languages for procedural logic.
Cache frequently accessed relatively constantdata.
Ensure seamless integration of applicationcomponents and scalability.
Enforce referential integrity using foreign keys.
-
8/14/2019 Advanced Features of Oracle SQL
47/48
D-47 Copyright 2004, Oracle. All rights reserved.
-
8/14/2019 Advanced Features of Oracle SQL
48/48
Summary
In this appendix, you should have learned how to:
Explain and create LOBs
Explain the need and benefits of PL/SQL
Identify the different types of PL/SQL blocks Use regular expression support in SQL to search,
match, and replace strings all in terms of regular
expressions
Employ proactive tuning methodologies