Professional Documents
Culture Documents
Fundamentals of PL/SQL
y Full-featured programming language y Interpreted language y Execute using Oracle 10g utilities y SQL*Plus y Forms Builder y Combines SQL queries with procedural commands y Reserved words
y PL/SQL has other data types that do not correspond to database data types
OCI/OCCI
CREATE TABLE t (f BINARY_FLOAT, d BINARY_DOUBLE); INSERT INTO t VALUES (2.0f, 2.0d); INSERT INTO t VALUES (1.5f/0.5f, 1.5d/0.5d);
NCHAR Datatype
y The NCHAR datatype is a Unicode-only datatype. y Define the column length in characters. y Define the national character set when creating
database. y Width specifications of character datatype NCHAR refer to the number of characters. y The maximum column size allowed is 2000 bytes. y If we insert a value that is shorter than the column length, then Oracle blank-pads the value to column length. y we cannot insert a CHAR value into an NCHAR column, nor can we insert an NCHAR value into a CHAR column.
Composite Variables
y Data object made up of multiple individual data
elements y Data structure contains multiple scalar variables y Composite variable data types include:
A y RECORD (multiple scalar values similar to a table record) R y TABLE (tabular structure with multiple columns R and rows) A Y y VARRAY (variable-sized array. Tabular structure
10
Varrays
y An array is an ordered set of data elements. y All elements of a given array are of the same datatype. y Each element has an index, which is a number corresponding to the position of the element in the array. y The number of elements in an array is the size of the array. y Oracle arrays are of variable size, which is why they are called varrays.
Varrays
y we must specify a maximum size when we declare the varray. y When we declare a varray, it does not allocate space. It defines a type, which we can use as: y The datatype of a column of a relational table y An object type attribute y A PL/SQL variable, parameter, or function return type
Nested Tables
y A nested table type models an unordered set of
elements. types. We
y The elements may be built-in types or user-defined y can view a nested table as a single-column table or,
if the nested table is an object type, as a multicolumn table, with a column for each attribute of the object type.
Nested Tables
y It defines a type, which we can use to declare: y The datatype of a column of a relational table y An object type attribute y A PL/SQL variable, parameter, or function return type y When a nested table appears as the type of a column in a relational table or as an attribute of the underlying object type of an object table, Oracle stores all of the nested table data in a single table, which it associates with the enclosing relational or object table. BACK
y y y y
POWERMULTISET_BY_CARDINALITY MULTISET EXCEPT operators CARDINALITY, Operator SET, IS A SET operators MEMBER OF operator IS EMPTY operator
MultiSet Operators
y Except y INTERSECT y UNION
from one nested table that aren't found in a second nested table in a query. For example the y Following query expression returns a nested table of the original value, except for 1. y select num multiset except num_tab_typ(1) num from num_tab;
NUM -------------------------NUM_TAB_TYP(2, 3, 4, 5) NUM_TAB_TYP(3, 5) NUM_TAB_TYP(2, 4, 6)
values which are common between two nested tables. Using a nested table with just the value 1 will return an empty set if 1 doesn't exist, and a nested table with the value 1 if it does exist. y select num multiset intersect num_tab_typ(1) num from num_tab
NUM -------------------------NUM_TAB_TYP(1) NUM_TAB_TYP(1) NUM_TAB_TYP()
values from both nested tables merged into a single nested table. y select num multiset union num_tab_typ(1) num from num_tab;
NUM -------------------------NUM_TAB_TYP(1, 2, 3, 4, 5, 1) NUM_TAB_TYP(1, 3, 5, 1) NUM_TAB_TYP(2, 4, 6, 1)
Reference Variables
y Directly reference specific database column or row y Assume data type of associated column or row y %TYPE data declaration syntax: variable_name tablename.fieldname%TYPE; y %ROWTYPE data declaration syntax: variable_name tablename%ROWTYPE;
Large Object (LOB) DatatypesCLOB, and NCLOB y The built-in LOB datatypes BLOB,
(stored internally) can store large and unstructured data such as text, image, video, and spatial data.
y BLOB, CLOB, and NCLOB data can be up to (4 gigabytes -1) * (database block size) in size. y When creating a table, we can optionally specify different tablespace and storage characteristics for LOB columns or LOB object attributes from those specified for the table. y LOB columns contain LOB locators that can refer to inline (in the database) or out-of-line (outside the database) LOB values.
datatype). y The LOB locator is stored in the table column, either with or without the actual LOB value. y BLOB, NCLOB, and CLOB values can be stored in separate tablespaces.
another LOB row-column intersection or LOB attribute. also delete the LOB value.
y we can delete a row containing a LOB column or LOB attribute and thereby y we can access and populate rows of an in-line LOB column (a LOB column
type, we must first initialize the LOB attribute using the EMPTY_CLOB or EMPTY_BLOB function. DBMS_LOB package or some other appropriate interface.
y we can then select the empty LOB attribute and populate it using the
BLOB Datatype
y The BLOB datatype stores unstructured binary large objects. y BLOB objects can be thought of as bitstreams with no character set
semantics.
of binary data.
y BLOB objects have full transactional support. y Changes made through SQL, the DBMS_LOB package, or the
CLOB Datatype
y The CLOB datatype stores single-byte and multibyte
character data.
y Both fixed-width and variable-width character sets are
NCLOB Datatype
y The NCLOB datatype stores Unicode data. y Both fixed-width and variable-width character sets
are supported, and both use the national character set. (database block size) of character text data.
y NCLOB objects can store up to (4 gigabytes-1) * y NCLOB objects have full transactional support.
BFILE Datatype
y The BFILE datatype enables access to binary file LOBs that are stored in file systems outside Oracle Database. y A BFILE column or attribute stores a BFILE locator, which serves as a pointer to a binary file on the server file system. y The locator maintains the directory name and the filename. y we can change the filename and path of a BFILE without affecting the base table by using the BFILENAME function.
BFILE Datatype
y Binary file LOBs do not participate in transactions and are not recoverable. y Rather, the underlying operating system provides file integrity and durability. y The maximum file size supported is 4 gigabytes. y The database administrator must ensure that the external file exists and that Oracle processes have operating system read permissions on the file. y The BFILE datatype enables read-only support of large binary files. You cannot modify or replicate such a file.
LOB Enhancements
y Performance improvements y Terabyte size LOBs y New DBMS_LOB.GET_STORAGE_LIMIT function y OCI now supports LOBs greater than 4GB
QUESTIONS ANSWERS