DBA Chapter 5
DBA Chapter 5
Management
Chapter 4 – Oracle Data Structures
1
Datatypes
Datatype is one of the attributes for a column or a variable in a stored
procedure.
• Describes and limits the type of information stored in a column.
• Can limit the operations that can be performed on columns.
Three basic categories of Oracle datatypes:
1. Character datatypes
2. Numeric datatypes
3. Datatype that represent other kinds of data
2
Character Datatypes
Can store any string value including the string representations of
numeric values.
Assigning a value larger than the length specified or allowed for a
character datatype results in a runtime error.
Types:
• CHAR
• VARCHAR2
• NCHAR and NVARCHAR2
• LONG
• CLOB and NCLOB
3
Character Datatypes
CHAR
• Stores character values with a fixed length.
• Fixed length can be between 1 and 2,000.
• If you don’t explicitly specify a length for a CHAR, it assumes the
default length of 1.
• If you assign a value that’s shorter than the length specified for the
CHAR datatype, Oracle will automatically pad the value with blanks.
• Example:
– CHAR (4) = “AB ”
4
Character Datatypes
VARCHAR2
• Stores variable-length character strings.
• Assigned length to a VARCHAR2 datatype is the maximum length for a
value rather than the required length.
• Values assigned to a VARCHAR2 datatype are not padded with blanks.
• Can require less storage than a CHAR datatype.
• Maximum length is 32000 characters for Oracle 12c and 4000 characters
for earlier releases.
• VARCHAR and VARCHAR2 datatypes are synonymous, but Oracle
recommends the use of VARCHAR2.
• Example:
– VARCHAR2(4) = “AB”
5
Character Datatypes
NCHAR and NVARCHAR2
• Stores a fixed-length or variable-length character data, respectively,
using a different character set from the one used by the rest of the
database.
• At the time of database creation, a secondary character set can be
specified which is known as the National Language Set, or NLS.
• This secondary character set will be used for NCHAR and
NVARCHAR2 columns.
• For example, you may have a description field in which you want to
store Japanese characters while the rest of the database uses English
encoding. You would specify a secondary character set that supports
Japanese characters when you create the database, and then use the
NCHAR or NVARCHAR2 datatype for the columns in question. 6
Character Datatypes
LONG
• Can hold up to 2 GB of character data.
• A legacy datatype from earlier versions of Oracle which is not
recommended to be used.
• Oracle recommends to use the CLOB and NCLOB datatypes.
CLOB and NCLOB
• CLOB (Character Large Object) datatype can store up to 128 TBs of
character data, depending on the block size of the database.
• CLOB is used to store Unicode character-based data.
• NCLOB datatype stores the National Language character set data.
7
Numeric Datatype
The Oracle NUMBER data type is used to store numeric values.
NUMBER stores numeric values with a precision of up to 38 digits.
NUMBER datatype can accept two qualifiers:
column NUMBER( precision, scale )
• Precision specifies the total number of significant digits in the number (1-38).
Default value is 38 if no value is specified for precision.
• Scale represents the number of digits to the right of the decimal point. Default
value is 0 if no value is specified for scale.
If you assign a negative number to the scale, Oracle will round the number up
to the designated place to the left of the decimal point.
Example:
column_round NUMBER(10,−2) 8
Date Datatype
Oracle stores all dates and times in a standard internal format.
Standard input for date: DD-MON-YY HH:MI:SS
• DD represents up to two digits for the day of the month
• MON is a three-character abbreviation for the month
• YY is a two-digit representation of the year
• HH, MI, and SS are two-digit representations of hours, minutes, and seconds.
• The default time values are all zeros in the internal storage.
Oracle SQL supports date arithmetic in which integers represent days and
fractions represent the fractional component represented by hours, minutes,
and seconds.
Example:
• 12-DEC-07 + 10 = 22-DEC-07
9
• 31-DEC-2007:23:59:59 + .25 = 1-JAN-2008:5:59:59
Other Datatypes
Aside from the basic character, number, and date datatypes, Oracle
supports a number of specialized datatypes:
• RAW and LONG RAW
• ROWID
• ORA_ROWSCN
• LOB
• BFILE
• XMLType
• Identity datatype
10
Type Conversion
Oracle implicitly converts some datatypes to other datatypes.
• Assigning a character value such as 10 to a NUMBER column results in
an automatic data conversion.
Supports explicit conversions on data, using a variety of conversion
functions.
• to_char()
• to_date()
• to_number()
11
Concatenation
Concatenation operator is two vertical lines (||).
Concatenation is performed with two character values.
• Automatic type conversion allows to concatenate two numeric values,
resulting in a character string.
• Example:
NUM1 || NUM2 || NUM3 = "123"
NUM1 || NUM2 + NUM3 = "15" (1 || 2 + 3)
NUM1 + NUM2 || NUM3 = "33" (1+ 2 || 3)
12
Comparisons
Comparisons between values of the same datatype work as:
• Date that occurs later in time is larger than an earlier date.
• 0 or any positive number is larger than any negative number.
• You can use relational operators
• Comparisons of single characters are based on the underlying code pages
for the characters.
• Multicharacter strings are compared until the first character that differs
between the two strings appears.
– Strings of different lengths are compared using two different comparison
semantics: blank-padded comparisons and nonpadded comparisons.
13
NULLs
Represents the lack of a value.
One of the key features of the relational database.
NULL can be assigned as a value for any datatype.
NOT NULL specifies that a column must have a value.
Three-state logic to SQL operators
• Comparison that involves a NULL value, results in three logical states:
TRUE, FALSE, and neither.
• NULL value is not equal to 0 or any other value.
• IS NULL tests for the presence of a NULL value.
14
Basic Data Structures
Three basic Oracle data structures:
1. Tables
2. Views
3. Indexes
15
Tables
The basic data structure used in a relational database.
A table is a collection of rows with one or more columns.
External tables
• Data is stored outside the database in a file.
• Data in external tables cannot be updated, they are read-only.
• Are used to load/unload data to files
16
Views
A view is an Oracle data structure defined through a SQL statement.
The SQL statement is stored in the database.
When you use a view in a query, the stored query is executed and the
base table data is returned to the user.
Views do not contain data, but represent ways to look at the base table
data in the way the query specifies.
A view is built on a collection of base tables, which can be either
actual tables in an Oracle database or other views.
17
Views
Views are useful for several purposes:
• Simplify access to data stored in multiple tables.
• Implement security for the data in a table.
• Isolates an application from the specific structure of the underlying
tables.
Materialized views
• Are not really views, but are physical tables that hold pre-summarized
data.
• Provides significant performance improvements in a data warehouse.
18
Indexes
An index is a data structure that speeds up access to particular rows in
a database.
An index is associated with a particular table.
Index contains data from one or more columns in the table.
DBMS automatically modifies the values in the index when the values
in the corresponding columns are modified.
In addition to the data for an index, an index entry stores the ROWID
for its associated row.
Example - SQL syntax for creating an index:
CREATE INDEX emp_idx1 ON emp (ename, job);
19
Indexes
Indexes are faster to read
• Index contains less data than the complete row in the table.
• Indexes are stored in a special structure that are faster to read.
• Most indexes are stored in sorted order.
• Index entry stores the ROWID for its associated row, which enables fast
retrieval of any row in a database.
20
Indexes
Unique index
• No two rows in the table can have the same index value
• If the table contains rows with duplicate key values, the index creation process
fails.
Non-unique Index
• Rows in the table can have the same index value
If the column or columns on which an index is based contain NULL values,
the row is not included in the index.
• An index in Oracle refers to the physical structure used within the database.
• A key is a term for a logical entity, typically the value stored within the index.
21