Upload
apekshachaturvedi
View
132
Download
1
Tags:
Embed Size (px)
Citation preview
Oracle 10g
•New Data types/Operators
•Time Zone Functions
•Flashback_query_clause
•Flashback table
•Regular Expressions
New Features in SQL for Oracle Database 10g
New Data types/Operators
Oracle Database provides two numeric data types exclusively for floating-point numbers.
BINARY_FLOAT : BINARY_FLOAT is a 32-bit ,single –precision floating-point number data type. Each BINARY_FLOAT value requires 5 bytes, including a length byte.
BINARY_DOUBLE : BINARY_DOUBLE is a 64-bit, double-precision floating-point number data type. Each BINARY_DOUBLE value requires 9 bytes, including a length byte.
Rounding of BINARY_FLOAT and BINARY_DOUBLE values to integer-valued BINARY_FLOAT and BINARY_DOUBLE values is provided by the SQL functions ROUND,TRUNC,CEIL and FLOOR.
Rounding of BINARY_FLOAT/BINARY_DOUBLE to decimal and decimal to BINARY_FLOAT/ BINARY_DOUBLE is provided by the SQL functions TO_CHAR,TO_NUMBER,TO_NCHAR,TO_BINARY_FLOAT,TO_BINARY_DOUBLE.
Time Zone Functions
SESSIONTIMEZONEDBTIMEZONECURRENT_DATECURRENT_TIMESTAMPV$TIMEZONE_NAMES dynamic ViewUser can set the session time zone with time_zone parameter.
SESSIONTIMEZONE returns the time zone of the current session.
ALTER SESSION SET TIME_ZONE=‘+5:30’;
ALTER SESSION SET NLS_DATE_FORMAT=‘DD-MON-YYYY HH24:MI:SS’;
Time Zone Functions
Time Zone Functions
For the listing of valid time zone names, query the V$TIMEZONE_NAMES Dynamic performance view.
To see the list of Time zones select TZNAME column from V$TIMEZONE_NAMES.
Time Zone Functions
FLASHBACK_QUERY_CLAUSE
Use the flashback_query_clause to retrieve the past data from a table , view or materialized view.
The Salary of“SCOTT” is updatedin the query.
FLASHBACK_QUERY_CLAUSE
To learn whatthe value was Before the update,you can user thisquery
To revert to the earlier value,Use the Flashback QueryAs the sub query of anotherUPDATE statement.
DROP TABLE …PURGE
DROP TABLE <TABLENAME> PURGE;
Oracle Database 10g introduces a new feature for dropping tables. When you drop a table, the database does not immediately release the space associated with the table. Rather, the database renames the table and places it in a recycle bin, where it can later be recovered with the FLASHBACK TABLE statement if you find that you dropped the table in error. If you want immediately release the space associated with the table at the time you use the DROP Table, then include the PURGE clause .
Here EMP table Is recovered after executing FLASHBACK TABLE statement
DROP TABLE …PURGE
Once PURGE is usedalong with DROP TABLEwe cannot recover tablelater.
DROP TABLE …PURGE
Regular Expression Overview
Oracle Database 10g introduce support for Regular Expressions. The implementation compiles with the Portable Operating System for UNIX (POSIX) standard, controlled by the Institute of Electrical and Electronics Engineers (IEEE), for ASCII data matching semantics and syntax.
REGULAR EXPRESSION FUNCTIONS
Function Name Description
REGEXP_LIKE Similar to the LIKE operator, but performs regular expression matching instead of simple pattern matching
REGEXP_REPLACE Searches for regular expression pattern and replaces it with a replacement string
REGEXP_INSTR Searches for a given string for a regular expression pattern and return the position where the match is found
REGEXP_SUBSTR Searches for a regular expression pattern within a given string and returns the matched substring
META CHARACTERS
Symbol Description
Matches zero or more occurrences.
Alteration operator for specifying alternative matches.
*
|
^/$ Matches the start-of-line/end-of-line
[] Bracket expression for a matching list matching any one of the expressions represented in the list
{m} Matches exactly m time
META CHARACTERS
{m,n} Matches at least m time but no more than n times
Specifies a character class and matches any character in that class[::]
\ Can have 4 different meanings: 1. Stand for itself. 2. Quote the next character. 3. Introduce an operator. 4. Do nothing
+ 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()
THE REGEXP_LIKE FUNCTION SYNTAX
REGEXP_LIKE (srcstr, pattern [,match_option])
pattern is the regular expression. It is usually a text literal and can be of any of the datatypes CHAR, VARCHAR2, NCHAR, or NVARCHAR2.
'i' specifies case-insensitive matching.
'n' allows the period (.), to match the newline character .
'm' treats the source string as multiple lines .
'x' ignores whitespace characters.
'c' specifies case-sensitive matching..
match_parameter is a text literal that lets you change the default matchingbehavior of the function. You can specify one or more of the following values for match_parameter:
Example :
The first_name starting with‘Ste’ followed by ‘v’ or ‘ph’Ending with ‘en’ are displayed
THE REGEXP_LIKE FUNCTION SYNTAX
THE REGEXP_INSTR FUNCTION SYNTAX
REGEXP_INSTR (srcstr, pattern [,position [, occurrence [, return_option [, match_option]]]])
pattern is the regular expression. It is usually a text literal and can be of any of the datatypes CHAR, VARCHAR2, NCHAR, or NVARCHAR2.
position is a positive integer indicating the character of source_char where Oracle should begin the search. The default is 1, meaning that Oracle begins the search at the first character of source_char
occurrence is a positive integer indicating which occurrence of pattern in source_char Oracle should search for. The default is 1, meaning that Oracle searches for the first occurrence of pattern.
return_option lets you specify what Oracle should return in relation to the occurrence:
If you specify 0, then Oracle returns the position of the first character of the occurrence. This is the default.If you specify 1, then Oracle returns the position of the character following the occurrence.
match_parameter is a text literal that lets you change the default matching behavior of the function. You can specify one or more of the following values for match_parameter:
'i' specifies case-insensitive matching.
'c' specifies case-sensitive matching.
'n' allows the period (.), to match the newline character .
'm' treats the source string as multiple lines .
'x' ignores whitespace characters.
THE REGEXP_INSTR FUNCTION SYNTAX
Examples :
. Oracle begins searching at the first character in the string and returns
the starting position (default) of the sixth occurrence of one or more
non-blank characters.
THE REGEXP_INSTR FUNCTION SYNTAX
THE REGEXP_REPLACE FUNCTION SYNTAX
REGEXP_REPLACE (srcstr, pattern [,replacestr [, position [, occurrence [, match_ptiion]]]])
pattern is the regular expression. It is usually a text literal and can be of any of the datatypes CHAR, VARCHAR2, NCHAR, or NVARCHAR2.
replace_string can be of any of the datatypes CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB . The replace_string can contain up to 500 backreferences to subexpressions in the form \n, where n is a number from 1 to 9.
position is a positive integer indicating the character of source_char where Oracle should begin the search. The default is 1, meaning that Oracle begins the search at the first character of source_char
occurrence is a nonnegative integer indicating the occurrence of the replace operation: If you specify 0, then Oracle replaces all occurrences of the match.
If you specify a positive integer n, then Oracle replaces the nth occurrence
match_parameter is a text literal that lets you change the default matching behavior of the function. You can specify one or more of the following values for match_parameter:
'i' specifies case-insensitive matching.
'c' specifies case-sensitive matching.
'n' allows the period (.), to match the newline character .
'm' treats the source string as multiple lines .
'x' ignores whitespace characters.
THE REGEXP_REPLACE FUNCTION SYNTAX
. This example examines
first_name. Oracle puts a space after each non-null character in the string..
THE REGEXP_REPLACE FUNCTION SYNTAX
Example:
THE REGEXP_SUBSTR FUNCTION SYNTAX
pattern is the regular expression. It is usually a text literal and can be of any of the datatypes CHAR, VARCHAR2, NCHAR, or NVARCHAR2
position is a positive integer indicating the character of source_string where Oracle should begin the search. The default is 1, meaning that Oracle begins the search at the first character of source_string.
occurrence is a positive integer indicating which occurrence of pattern in source_string Oracle should search for. The default is 1, meaning that Oracle searches for the first occurrence of pattern.
REGEXP_SUBSTR (srcstr, pattern [,positio[, occurrence [, match_option]]])
match_parameter is a text literal that lets you change the default matching behavior of the function. You can specify one or more of the following values for match_parameter:
'i' specifies case-insensitive matching.
'c' specifies case-sensitive matching.
'n' allows the period (.), to match the newline character .
'm' treats the source string as multiple lines .
'x' ignores whitespace characters.
THE REGEXP_SUBSTR FUNCTION SYNTAX
This example examines the string, looking for the first substring bounded by commas.
Oracle Database searches for a comma followed by one or more occurrences of
non-comma characters followed by a comma
THE REGEXP_SUBSTR FUNCTION SYNTAX
Example:
ORACLE 10g New features PL/SQL
ORACLE 10g New features PL/SQL
• Native and Interpreted Compilation
• PL/SQL Warning Messages
Native and Interpreted Compilation
Natively compiled CodeTranslated C and compiledCopied to a code library
Interpreted CodeCompiled to m-codeStored in he Database
Features and Benefits of Native Compilation
Native compilation:
•Uses a generic makefile (spnc_makefile.mk, located in the $oracle_home/plsql directory) that uses he following operating system software.
•The C compiler•The linker•The Make utility
•Generates shared libraries that are copied to the file system and loaded at run time.•Provides better performance, up to 30% faster than interpreted code, for computation-intensive procedural operations.
Consideration When Using Native Compilation
•Debugging tools for PL/SQL cannot debug natively compiled code.•Natively compiled code is slower to compile than Interpreted code.•Large amounts of natively compiled subprograms can affect performance due to operating system-imposed limitations when handling shared libraries. OS directory limitations can be managed by setting database initialization parameters:
•PLSQL_NATIVE_LIBRARY_SUBDIR_COUNT •PLSQL_NATIVE_LIBRARY_DIR
•System Parameters
Set in the initSID.ora file PLSQL_NATIVE_LIBRARY_DIR = full-directory-path-namePLSQL_NATIVE_LIBRARY_SUBDIR_COUNT = count of
directories
•System or session parameters
PLSQL_COMPILE_FLAGS = ‘NATIVE’ OR ‘INTERPRETED’
Parameters Influencing Compilation
Switching Between Native and Interpreted Compilation
Setting Native Compilation :
•For the systemALTER SYSTEM SET plsql_compiler_flags = ‘NATIVE’;
•For the sessionALTER SESSION SET plsql_compiler_flags = ‘NATIVE’;
Switching Between Native and Interpreted Compilation
Setting interpreted compilation
•For the system levelALTER SYSTEM SET plsql_compiler_flags = ‘INTERPRETED’;
•For the sessionALTER SESSION SET plsql_compiler_flags = ‘INTERPRETED’;
Viewing Compilation Information in the Data Dictionary
Query information in the following views:
•USER_STORED_SETTINGS•USER_PLSQL_OBJECTS
SELECT param_value FROM user_stored_settings WHERE param_name = ‘plsql_compiler_flag’AND object_name = ‘PLW06002’;
Using Native Compilation
To enable native compilation, perform the following steps:
• Edit the supplied orafile and enter appropriate paths and other values for your system.
• Set the parameter PLSQL_COMPILER_FLAGS (at system or session level) to the value NATIVE. The default is INTERPRETED.
• Compile the procedures, functions, and packages.• Query the data dictionary to see that a procedure is compiled for native execution.
Compiler Warning Infrastructure
The PL/SQL compiler in Oracle Database 10g has been enhanced to produce warnings for subprograms.
Warning levels Can be set:
•Declaratively with the PLSQL_WARNINGS initialization parameter.•Programmatically using the DBMS_WARNINGS package.
•Are arranged in three categories: severe, performance, and informational•Can be enabled and disabled by category or a specific message.
Examples of warning messages:•SP2-0804: Procedure created with compilation warnings•PLW-07203: Parameter ‘IO_TBL’ may benefit from use of the NOCOPY compiler hint.
Setting Compiler Warning Levels
Set the PLSQL_WARNINGS initialization parameter to enable the database to issue warning messages.
ALTER SESSION SET PLSQL_WARNINGS = ‘ENABLE:SEVERE’, ‘DISABLE:INFORMATIONAL’;
The PLSQL_WARNINGS combine a qualifier value ENABLE, DISABLE, OR ERROR with a comma-separated list of message numbers, or with one of the values.
ALL, SERVER, INFROMATIONAL, OR PERFORMANCE
Setting Compiler Warning Levels
•ENABLE : To enable a specific warning or a set of warnings.•DISABLE : To disable a specific warning or a set of warnings.•ERROR : To treat specific warnings or a set of warnings as errors.
The modifier value ALL applies to all warnings messages,. SERVER,IFORMATIONAL, AND PERFORMANE refers to the category, and an integer list for specific warning message.
•PLSQL_WARNINGS = ‘ENABLE: SERVER’, ‘DISABLE: INFORMATIONAL’;•PLSQL_WARNINGS = ‘DISABLE:ALL’;•PLSQL_WARNINGS = ‘DISABLE: 5000’, ‘ENABLE:5001’,’ERROR:5002;•PLSQL_WARNINGS = ‘‘ENABLE(5000,5001)’, ’DISABLE(6000)’;
Guidelines for Using PLSQL_WARNINGS
The PLSQL_WARNINGS settings:
Can be set to DEFERED at the system level so that it applies to future sessions but not the current one.
Is stored with each compiled subprogram
That is current for the session is used, by default, when recompiling with:A CREATE OR REPLACE statement
An ALTER…COMPILE statementThat is stored with the compiled subprogram is used when REUSE SETTINGS is specified when recompiling with an ALTER…COMPILE statement.
DBMS_WARNING Package
The DBMS_WARNING Package provides a way to programmatically manipulate the behavior of current system or session PL/SQL warnings settings. Using DBMS_WARNING subprograms, you can:
•Query existing settings•Modify the settings for specific requirements or restore original settings•Delete the settings
Example : Saving and restoring warnings settings for a development environment that calls your code that compiles PL/SQL subprograms, and suppresses warnings due to business requirements.
Using DBMS_WARNING Package
•All parameters are IN parameters and have the VARCHAR2 data type. However, the w_number parameter is a number data type.•Parameter string values are not case sensitive.•The w_value parameters values are ENABLE, DISABLE, and ERROR.•The w_category values are ALL, INFORMATIONAL, SEVERE, AND PERFORMANCE.•The scope value is either SESSION OR SYSTEM. Using SYSTEM requires the ALTER SYSEM privilege
•PL/SQL warning messages are divided into categories, so that you can suppress or display groups of similar warnings during compilation. The categories are:•Severe : Messages for conditions that might cause unexpected behavior or wrong results, such as aliasing problems with parameters.•Performance : Messages for conditions that might cause performance problems, such as passing a VARCHAR2 value to a NUMBER column in a INSERT statement.•Informational: Messages for conditions that do not have an effect on performance or correctness, but that you might want to change to make the code more maintainable, such as dead code that can never be executed.The keyword All is a shorthand way to refer to all warning messages.
Using DBMS_WARNING Package
Get_warning_setting_string : Returns the current setting
Add_warning_setting_num (w_number,w_value,scope) : sets warning value for specified warning number.
Using DBMS_WARNING Package
ADD_WARNING_SETTING_CAT(w_category,w_value,scope) : Modifies the current session or system warnings settings of the warning category previously supplied.
The session is altered in the same way using the subprogram ADD_WARNING_SETTING_CAT as
Using DBMS_WARNING Package
Get_warning_setting_num(w_number) : Returns the status of the specified warning number.
Get_category(w_number) : Returns the warning category of the specified warning number.
Using DBMS_WARNING Package
Get_warning_setting_cat(w_category) : Returns the warning value for the specifiedCategory.
Getting_warning_setting_num(w_number): Returns the warning value for the specifiedCategory.
Using DBMS_WARNING Package